I'm trying to configure Apache with Django. Everything is working except the admin panel. It's static files are not loading. The documentation talks about different ways of doing it but none are working for me.
Here is my 0000-default.conf:
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin maahd#meddy.co
DocumentRoot /var/www/html
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/html>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
Alias /static /var/www/html/sp-django-master/meddy1/static
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
include sites-available/meddy.co.conf
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
Here is my meddy.co.conf where meddy.co is my website:
<VirtualHost *:80>
ServerName ec2-54-254-141-40.ap-southeast-1.compute.amazonaws.com
ServerAlias www.ec2-54-254-141-40.ap-southeast-1.compute.amazonaws.com
WSGIScriptAlias / /var/www/html/sp-django-master/meddy.wsgi
DocumentRoot /var/www/html/sp-django-master
#Alias /static /var/www/html/sp-django-master/meddy1/static
<Location "/static/">
Options -Indexes
</Location>
#AliasMatch ^/([^/]*\.css) /var/www/html/sp-django-master/meddy1/static/meddy1/css/$1
AliasMatch ^/([^/meddy1]*\.css) /var/www/html/sp-django-master/meddy1/static/meddy1/css/$1
AliasMatch ^/([^/admin]*\.css) /var/www/html/sp-django-master/static/admin/css/$1
Alias /static/ /var/www/html/sp-django-master/meddy1/static/
<Directory /var/www/html/sp-django-master/meddy1/static>
Require all granted
</Directory>
<Directory /var/www/html/sp-django-master/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
Alias /media/ /var/www/html/sp-django-master/uploads/
<Directory /var/www/html/sp-django-master/uploads>
Require all granted
</Directory>
Alias /static/admin/ /var/www/html/sp-django-master/static/admin/
</VirtualHost>
Any help would be appreciated.
Related
my django website hosted on AWS EC2 apache ubuntu has ssl certificate obtained and setup through certbot but it is not opening on port 443(HTTPS).It is showing error Forbidden 403.
There might be some permission issues. I have executed sudo chmod -R 775 /path/to/webroot/directory, sudo chown -R user:group /path/to/webroot/directory commands.
apache2.conf
<Directory />
Options FollowSymLinks
#Options Indexes FollowSymLinks
AllowOverride None
#Order deny,allow
Require all denied
</Directory>
<Directory /usr/share>
AllowOverride None
Require all granted
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<Directory "/var/www">
AllowOverride None
Require all granted
</Directory>
#<Directory /srv/>
#Options Indexes FollowSymLinks
#AllowOverride None
#Require all granted
#</Directory>
000-default.conf
<VirtualHost *:80>
#ServerName logyanlo.com
#ServerAlias www.logyanlo.com
ServerAdmin webmaster#localhost
DocumentRoot /home/ubuntu/blog/gyanlo
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /static /home/ubuntu/blog/gyanlo/static
<Directory /home/ubuntu/blog/gyanlo/static>
Require all granted
</Directory>
<Directory /home/ubuntu/blog/gyanlo/gyanlo>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess gyanlo python-path=/home/ubuntu/blog/gyanlo/ python-home=/home/ubuntu/blog/gyanlo_env
WSGIProcessGroup gyanlo
WSGIScriptAlias / /home/ubuntu/blog/gyanlo/gyanlo/wsgi.py
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.logyanlo.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
My site is working on http
I now want it to work using a SSl certificate. I've generated the keys and set up the virtualhost:
<VirtualHost *:80>
WSGIDaemonProcess pms python-path=/home/ubuntu/myapp/myapp:/home/ubuntu/myapp/env/lib/python3.4$
WSGIProcessGroup myapp
WSGIScriptAlias / /home/ubuntu/myapp/myapp/myapp/wsgi.py
<Directory /home/ubuntu/myapp/myapp/myapp>
<Files wsgi.py>
Require all granted
</Files>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
ServerAdmin support#myapp.com
</VirtualHost>
<VirtualHost *:443>
SSLEngine On
SSLCertificateFile /etc/apache2/ssl/ca.crt
SSLCertificateKeyFile /etc/apache2/ssl/ca.key
ServerName leanhotelsystem.com
ServerAlias *.leanhotelsystem.com
DocumentRoot /home/ubuntu/myapp/myapp/myapp
<Directory /home/ubuntu/myapp/myapp/myapp>
<Files wsgi.py>
Require all granted
</Files>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ServerAdmin support#myapp.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
I've also set in wsgi.py:
os.environ['HTTPS'] = "on"
and in settings.py:
WSGI_APPLICATION = 'myapp.wsgi.application'
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
The problem is that when i access the url with https it shows my folder structure, instead of rendering the homepage.
If I access via http it works fine.
Thanks!
Looks like you forget your WSGI settings section in your new VirtualHost.
After upgrade to jessie my django stops working. I found that problem is in mod_python, so I decided that this is good reason to migrate to mod_wsgi.
I read this but i'm not sure what to do if i got more than one project:
I got few projects in /home folder:
/home/project1
/home/project2
/home/project3
my apache's http.conf (not migrated to 2.4 yet, so please ignore Order/allow etc)
<VirtualHost *:80>
ServerAdmin webmaster#mydomain.com
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
<Location "/project1">
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE project1.settings
PythonInterpreter project1
PythonOption django.root /project1
PythonDebug On
PythonPath "['/home', '/home/project1'] + sys.path"
</Location>
<Location "/project2">
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE project2.settings
PythonInterpreter project2
PythonOption django.root /project2
PythonDebug On
PythonPath "['/home', '/home/project2'] + sys.path"
</Location>
<Location "/project3">
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE project3.settings
PythonInterpreter project3
PythonOption django.root /project3
PythonDebug On
PythonPath "['/home', '/home/project3'] + sys.path"
</Location>
Alias /admin_media/ /usr/share/pyshared/django/contrib/admin/media/
Alias /static/ /home/common/
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
</VirtualHost>
I'm thinking about daemon mode, but how to use it on different projects???
Update
According to #GrahamDumpleton's answer my new apache looks like:
ServerAdmin webmaster#mydomain.com
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Require all granted
</Directory>
WSGIDaemonProcess project1
WSGIDaemonProcess project2
WSGIDaemonProcess project3
WSGIScriptAlias /project1/ /home/project1/wsgi.py process-group=project1
WSGIScriptAlias /project2/ /home/project2/wsgi.py process-group=project2
WSGIScriptAlias /project3/ /home/project3/wsgi.py process-group=project3
<Directory /home/*>
Require all granted
</Directory>
Alias /admin_media/ /usr/share/pyshared/django/contrib/admin/media/
Alias /static/ /home/common/
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
</VirtualHost>
Now it works - Thanks
Since your projects are all at separate sub URLs, simply use multiple WSGIScriptAlias directives, one for each sub URL. And definitely use a daemon process group for each distinct Django instance.
For some additional reading see:
http://blog.dscpl.com.au/2012/10/why-are-you-using-embedded-mode-of.html
http://blog.dscpl.com.au/2012/10/requests-running-in-wrong-django.html
http://blog.dscpl.com.au/2014/09/using-python-virtual-environments-with.html
There are also the mod_wsgi docs, although they are in a slight mess right now.
I have an online shop which contains a main page and a shop page. now this main page and shop page are actually two different projects, so in order to have them online, I have to run two instances of django.
now the things is, i want to have the main page on www.setakshop.ir, and the shop on shop.setakshop.ir. The thing is, after setting up the necessary settings, both shop.setakshop.ir and setakshop.ir point to the main page! and I can only see the shop page through setakshop.ir:8000, which I expected apache to proxy it to shop.setakshop.ir
I serve the DNS myself and here are my DNS settings:
;
; BIND data file for setakshop.ir
;
$TTL 3h
# IN SOA ns1.setakshop.ir. admin.setakshop.ir. (
1 ; Serial
3h ; Refresh after 3 hours
1h ; Retry after 1 hour
1w ; Expire after 1 week
1h ) ; Negative caching TTL of 1 day
;
# IN NS ns1.setakshop.ir.
# IN NS ns2.setakshop.ir.
setakshop.ir. IN MX 10 mail.setakshop.ir.
setakshop.ir. IN A xx.xx.xx.xx
ns1 IN A xx.xx.xx.xx
ns2 IN A xx.xx.xx.xx
www IN CNAME setakshop.ir.
mail IN A xx.xx.xx.xx
ftp IN CNAME setakshop.ir.
shop IN A xx.xx.xx.xx
and when run
nslookup shop.setakshop.ir
i get a valid response. So I think the DNS setup is actually fine.
Now the other thing I suspect, is my apache settings. I suspect that I haven't set the proxy settings right. here it is:
<VirtualHost *:80>
WSGIDaemonProcess main python-path=/var/www/setak:/var/www/setak/setakenv/lib/python2.7/site-packages
WSGIProcessGroup main
WSGIScriptAlias / /var/www/setak/setakenv/main/ashop/ashop/ashop/wsgi.py
ServerAdmin admin#setakshop.ir
ServerName www.setakshop.ir
ProxyPass / http://www.setakshop.ir:8001/
ProxyPassReverse / http://www.setakshop.ir:8001/
Alias /media/ /var/www/setak/setakenv/main/ashop/ashop/static/media/
Alias /static/ /var/www/setak/setakenv/main/ashop/ashop/static/
<Directory /var/www/setak/setakenv/main/ashop/ashop/static>
Order allow,deny
allow from all
</Directory>
<Directory /var/www/setak/setakenv/main/ashop/ashop/static/media>
Order allow,deny
allow from all
</Directory>
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
</VirtualHost>
<VirtualHost *:8080>
WSGIDaemonProcess setak python-path=/var/www/setak:/var/www/setak/setakenv/lib/python2.7/site-packages
WSGIProcessGroup setak
WSGIScriptAlias / /var/www/setak/setakenv/setakmain/django-oscar/sites/sandbox/wsgi.py
ServerAdmin admin#setakshop.ir
ServerName shop.setakshop.ir
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass / http://shop.setakshop.ir:8000
ProxyPassReverse / http://shop.setakshop.ir:8000
Alias /robots.txt /var/www/setak/setakenv/setakmain/django-oscar/sites/sandbox/static/robots.txt
Alias /media/ /var/www/setak/setakenv/setakmain/django-oscar/sites/sandbox/public/media/
Alias /static/ /var/www/setak/setakenv/setakmain/django-oscar/sites/sandbox/public/static/
<Directory /var/www/setak/setakenv/setakmain/django-oscar/sites/sandbox/public/static>
Order allow,deny
allow from all
</Directory>
<Directory /var/www/setak/setakenv/setakmain/django-oscar/sites/sandbox/public/media>
Order allow,deny
allow from all
</Directory>
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
</VirtualHost>
Now I dont get what I'm doing wrong here!
I run both the projects with the following command :
./manage.py runserver 0.0.0.0:8000
./manage.py runserver 0.0.0.0:8001
Can anybody tell me what I'm doing wrong?
thanks in advance.
That really is not how you run Django applications via Apache. runserver is not designed for or appropriate for production; it makes no sense to use Apache as a reverse proxy to runserver.
Confusingly it seems that you have correctly set up WSGIScriptAlias for one of your sites, but you are still proxying it. You need to remove the proxy stuff altogether, forget about runserver, and use mod_wsgi throughout.
The way to get Apache to serve two sites on two domains is to use NamedVirtualHosts. You simply set up two separate ones, each with the correct server name.
First of all you should not use ./manage.py runserver along with apache and you should not use ./manage.py runserver when you move your website to production. runserver is only for development environment, it can handle only one request at a time.
You should use mod_wsgi for running your project using Apache. Look the docs here
Or else gunicorn and nginx can be used for running your project. Have a look at this.
I have apache2 installed in ubuntu lucid ,and have enabled ssl .Now I am running a django app (lets say myapp1 )on it using mod_wsgi.
I have configured the /etc/apache2/sites_enabled/ssl file and /etc/apache2/sites-available/ssl as below.
Now I can run my app using the url
https://127.0.0.1/myapp1
I need to run another django app (say myapp2 )in the same server,and that also uses SSL.So,how should I configure it?Can somebody please help me?
<VirtualHost *:443>
ServerAdmin webmaster#localhost
DocumentRoot /home/me/dev/python/django/myapp1
SSLEngine on
SSLOptions +StrictRequire
SSLCertificateFile /etc/ssl/certs/server.crt
SSLCertificateKeyFile /etc/ssl/private/server.key
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog /var/log/apache2/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /var/log/apache2/access.log combined
WSGIScriptAlias /myapp1 /home/me/dev/python/django/myapp1/myapp1.wsgi
Alias /site_media/ /home/me/dev/python/django/myapp1/media/
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
</VirtualHost>
By adding:
WSGIScriptAlias /myapp2 /home/me/dev/python/django/myapp2/myapp2.wsgi
You will need to resolve any conflict though if they can't share the same static media files. That is, have each have media in different locations and configure settings for each Django project appropriately.