1

I've followed this tutorial to deploy my django project : https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-debian-8

Why the static files can't be loaded from apache?

I am using Debian9, Python3.5 and Django 1.11

This is my virtual host configuration:

<VirtualHost *:80>

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /static /home/alex/djangoproject/static
    <Directory /home/alex/djangoproject/static>
        Require all granted
    </Directory>

    <Directory /home/alex/djangoproject/cpanel>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    WSGIDaemonProcess djangoproject python-home=/home/alex/djangoproject/djangoprojectenv python-path=/home/alex/djangoproject
    WSGIProcessGroup djangoproject
    WSGIScriptAlias / /home/alex/djangoproject/cpanel/wsgi.py

</VirtualHost>

`

And the apache error from error.log:

AH01630: client denied by server configuration /home/alex/djangoproject/static

And my settings.py config related to static foder

STATIC_URL = '/static/' PROJECT_DIR = os.path.dirname(os.path.abspath(__file__)) STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')

Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134
Alex Lucaci
  • 620
  • 8
  • 18
  • Usually your home directory is not accessible to the user that Apache run as. Check permissions on the home directory and all directories down to where files are located to ensure readable to others. – Graham Dumpleton Jun 07 '17 at 22:10
  • It has access to wsgi file to load the server files, because www-data group is the recursive owner of the project directory.Also tested with 777 permissions on all files and still the same. – Alex Lucaci Jun 08 '17 at 07:05
  • It is not just the project directory and everything under it. If a parent directory (usually /home is problem), has permissions of rwxr-x---, ie., not readable to anyone, it will not work. The only other cause of this is SELinux restrictions if using CentOS/RHEL/Fedora. – Graham Dumpleton Jun 08 '17 at 12:12

2 Answers2

1

Solved this by adding the django project name directory before static so this :

Alias /static /home/alex/djangoproject/static
<Directory /home/alex/djangoproject/static>
    Require all granted
</Directory>

Will become this:

Alias /static /home/alex/djangoproject/"django-project-name(in mycase: cpanel)"/static
<Directory /home/alex/djangoproject/"django-project-name(in mycase: cpanel)"/static>
    Require all granted
</Directory>

Performed ./migrate.py collectstatic , restart apache and all running smoothly.

Alex Lucaci
  • 620
  • 8
  • 18
1

For Ubuntu 20 :

in /etc/apache2/apache2.conf add after line 175:

<Directory /<project_folder_parent_directory>/>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

in /etc/apache2/sites-available/000-default.conf, use this quick setup:

<VirtualHost *:80>
    ServerAlias *
    ErrorLog /<wsgi.py_folder_path>/error.log
    
    CustomLog /<wsgi.py_folder_path>/access.log combine
    
    Alias /static /<static_parent_directory_path>/static
    <Directory /<project_folder_path>/static>
        Order allow,deny
        Allow from all
        Require all granted
    </Directory>
    <Directory /<wsgi.py_folder_path>/>
        <Files wsgi.py>
        Order allow,deny
        Allow from all
        Require all granted
        </Files>
        Order allow,deny
        Allow from all
        Require all granted
    </Directory>

    #Demon process for multiple virtual hosts
    WSGIDaemonProcess projectname  python-path=/<project_folder_path>/
    # if you're using virtual environment
    #WSGIDaemonProcess projectname python-home=/<project_virtual_env_path> python-path=/<project_folder_path>/
    WSGIProcessGroup projectname
    #Pointing wsgi script to config file
    WSGIScriptAlias / /<wsgi.py_folder_path>/wsgi.py

</VirtualHost>

Restart apache :

$ sudo systemctl restart apache2
Nitsh
  • 79
  • 3