I've followed the instructions on Django website for configuring Apache with my Django app on a CentOS 7 server. This included building mod_wsgi from sources to work with the installed python3.4.
Apache restarts without errors but when I hit my app with the URL
http://example.com/myapp/
I get a 503 error like:
Service Temporarily Unavailable
The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.
Apache/2.2.15 (CentOS) Server at example.com Port 80
I'm not sure how I troubleshoot what's wrong here. Can anyone help?
Details of the config:
My django app lives at /mnt/net/django/myapp
I've added the file wsgi.conf to my apache conf.d directory and it looks like this:
#LoadModule wsgi_module modules/mod_wsgi.so
# use python34 pip installes mod_wsgi
LoadModule wsgi_module "/usr/lib64/python3.4/site-packages/mod_wsgi/server/mod_wsgi-py34.cpython-34m.so"
#WSGIPythonHome "/usr"
Alias /robots.txt /mnt/net/django/myapp/static/robots.txt
Alias /favicon.ico /mnt/net/django/myapp/static/favicon.ico
Alias /media /mnt/net/django/myapp/media/
Alias /static/ /mnt/net/django/myapp/static/
<Directory /mnt/net/django/myapp/static>
Order deny,allow
Allow from all
</Directory>
<Directory /mnt/net/django/myapp/media>
Order deny,allow
Allow from all
</Directory>
# Allows URLs like example.com/myapp to forward to django
WSGIScriptAlias /myapp /mnt/net/django/myapp/myappsite/wsgi.py process-group=example.com
# Use the virtual env for the myapp site
#WSGIPythonHome /mnt/net/django/myapp/env-myapp-py3-4
# Need to use WSGIDaemon
WSGIDaemonProcess example.com python-home=/mnt/net/django/myapp/env-myapp-py3-4 python-path=/mnt/net/django/myapp
#WSGIPythonPath /mnt/net/django/myapp
<Directory /mnt/net/django/myapp/myappsite>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
How to use Django with Apache and mod_wsgi -- follow this: I have done it myself many times, it is very straight forward.
Tip: Create a document and record everything you do whilst setting this up, this way if it doesn't work then you can retrace your steps, but if it does work... Great, you have your very own guide to setting up an Apache server for Django.
Solved my problem (mostly)
The problem is that mod_wsgi with a daemon process tries to write a socket file into the apache logs directory and permissions are denied.
Solution is to tell apache another place to write the socket like this:
WSGISocketPrefix /var/run/wsgi
Related
I want to deploy multiple django apps on apache on Windows but only know how to deploy one.
Overriding the localhost of the Wamp Server I can deploy the app without problem but I need to deploy more and don't know how. I've sehen virtual hosts and think are good but don't know how to configurate them. Anyone know how can I do this? Thanks in advance.
hosting severel django apps with Apache is possible using virtual hosts (vhosts)
important to care about:
during config of Apache I recommend to start apache from command line as "httpd.exe" as in XAMPP or WAMP you will not see some of the initial start-up error messages in error.log files.
you can only use 1 python version even in different virt.env for each vhost as apache module mod_wsgi compilation needs to fit to it and is loaded once at startup of apache
something like this in httpd.conf (you should have this already in place because of your running single app config):
LoadFile "c:/.../python/python38/python38.dll"
LoadModule wsgi_module "c:/..../mod_wsgi.cp38-win_amd64.pyd"
for those starting from scratch:
activate virt.env.
> pip install mod_wsgi
> mod_wsgi-express module-config
will give above output (LoadFile ....) that you need to copy to httpd.conf
how to set path to virt.env and app folders:
with 1 host you would point to your virt.env by setting WSGIPythonHome and WSGIPythonPath to point to your app folders in httpd.conf:
WSGIPythonHome "d:/..../django_project/env_folder"
WSGIPythonPath "d:/..../django_project/app_name"
but: you can not place WSGIPythonHome/WSGIPythonPath inside the VirtualHost declaration in httpd-vhosts.conf .... it will cause an error message
Solution: set paths in wsgi.py dynamically and remove WSGIPythonHome/WSGIPythonPath from apache *.conf:
wsgi.py:
# replacement for WSGIPythonHome "d:/..../django_project/env_folder"
# choose one:
sys.path.append('d:/.../env_folder/lib/site-packages') # add individual virt.environment packages at the end of sys.path; global env packages have prio
sys.path.insert(0,'d:/.../env_folder/lib/site-packages') # add individual virt.environment packages at the beginning of sys.path; indiv. virt.env packages have prio over global env
# replacement WSGIPythonPath "d:/..../django_project/app_name"
sys.path.append('d:/.../django_project/app_name') # add indiv. app folder to search path
here is example for apache conf:
(why the dummy host: there is a (strange or buggy) behavior of apache ... if none of the virtual host names match the request, then automatically apache will dispatch the request to the first vhost in the config - no matter which server name is defined ther. This can lead to confusion because the total wrong app is called and an error messages will most certainly pop-up from inside django, not indicating that the error is on the Apache conf level. A dummy host with a simple index.html and an error message can make this tranparent)
httpd-vhost.conf:
<VirtualHost *:80>
ServerName Dumme_Host
DocumentRoot "d:/WEBSPACES/Dummy_Host"
<Directory d:/WEBSPACES/Dummy_Host>
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName xxxx1
WSGIScriptAlias / "d:/.... /wsgi.py" application-group=app_name1
Alias /media/ d:/.../media/
Alias /static/ d:/.../static/
<Directory d:/.../app_name1>
Require all granted
</Directory>
<Directory d:/.../media>
Require all granted
</Directory>
<Directory d:/.../static>
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName xxxx2
WSGIScriptAlias / "d:/.... /wsgi.py" application-group=app_name2
Alias /media/ d:/.../media/
Alias /static/ d:/.../static/
<Directory d:/.../app_name2>
Require all granted
</Directory>
.....
</VirtualHost>
I have a django web application which I need to serve from an apache server on CentOS 6.8. In order to get this, I´m using mod_wsgi.
I have my project located in /path/to/myproject.
Right there I have the following directories and files:
- app1/
- db.sqlite3
- myproject/
- myprojectenv/
- manage.py
- static/
I have added to my setting.py this directive: STATIC_ROOT = os.path.join(BASE_DIR, "static/") and I´ve tried to run the django server and I can see my app view correctly.
So, after that I´ve tried to configure apache server in order to send a petition from my browser to my app, but apache rises a 403 Forbidden error.
The steps that I followed are:
- Add to httpd.conf the following information:
<VirtualHost *:80>
Alias /static /path/to/myproject/static
Alias /app1 /path/to/myproject/app1
<Directory /path/to/myproject/static>
Order allow,deny
Allow from all
</Directory>
<Directory /path/to/myproject/app1>
Order allow,deny
Allow from all
</Directory>
<Directory /path/to/myproject/myproject>
<Files wsgi.py>
Order allow,deny
Allow from all
</Files>
</Directory>
WSGIDaemonProcess myproject user=myuser group=mygroup python-path=/path/to/myproject:/path/to/myproject/myprojectenv/lib/python2.7/site-packages
WSGIProcessGroup myproject
WSGIScriptAlias / /path/to/myproject/myproject/wsgi.py
</VirtualHost>
Then, I gave rights to apache user, in order to allow apache to have access to my django project:
- sudo usermod -a -G myuser apache
- chmod 710 /home/myuser
- chmod 664 ~/myproject/db.sqlite3
- sudo chown :apache ~/myproject/db.sqlite3
- sudo chown :apache ~/myproject
Finally, I restarted the htttpd service and tried to send a petition to my app, but I got a "403 Forbidden error".
After that, I tried to copy my django project to the apache directory /var/www/ and doing this, I finnaly could see my django files but just like files, apache couldn´t run my app and show my view correctly.
I think that the problem could be about the permissions of apache user but I don´t know what else I can do.
What am I doing wrong?
What is the actual error in the Apache error log, not the 403 error in the browser?
Most likely the problem is due to your home directory being 710. This means an other user such as the Apache user cannot see into your home directory. It needs to be able to do that as the Apache user to map the URL to the WSGI application.
Either move your whole project outside of your home directory, or create a separate WSGI script file called myproject.wsgi under /var/www/myproject or similar, which has in it:
from myproject.wsgi import application
Also change:
WSGIScriptAlias / /path/to/myproject/myproject/wsgi.py
<Directory /path/to/myproject/myproject>
<Files wsgi.py>
Order allow,deny
Allow from all
</Files>
</Directory>
to:
WSGIScriptAlias / /var/www/myproject/myproject.wsgi
<Directory /var/www/myproject>
<Files myproject.wsgi>
Order allow,deny
Allow from all
</Files>
</Directory>
The Apache user only needs to be able to see the WSGI script file when doing URL mapping. The application code will actually run in your case under daemon mode with user myuser which presumably can read stuff from your home directory.
You didn't need to change ownership of your directory/files to Apache user, again because the code will run as myuser.
I have a Django 1.4.5 project called mp which I'm trying to run on my localhost using Apache 2.4. Following the official tutorial for Django with mod_wsgi
(How to use Django with Apache and mod_wsgi) I managed to display my Django page when I visit http://127.0.0.1:8801/.
My project folder mp is located in /opt/masterportal/mp, and the static files are located in /opt/masterportal/mp/mp/static.
This is my masterportal.conf file in /etc/apache2/sites-available:
Listen 8081
<VirtualHost *:8081>
ServerAdmin my#mail.adress
XSendFilePath /opt/masterportal/mp/mp/uploads/
<Files *.*>
XSendFile On
</Files>
WSGIDaemonProcess masterportal python-path=/opt/masterportal/mp:/opt/masterportal/mp/env/dev/lib/python2.7/site-packages
WSGIProcessGroup masterportal
WSGIScriptAlias / /opt/masterportal/mp/mp/apache/wsgi.py
Alias /static /opt/masterportal/mp/mp/static
<Directory /opt/masterportal/mp/mp/static>
Require all granted
</Directory>
<Location />
WSGIProcessGroup masterportal
Require all granted
</Location>
</VirtualHost>
However, the website at http://127.0.0.1:8801 can't find any of the static files. This is odd, because the exact same project works on the server of my university (where I don't have access to the apache configuration). So there must be something wrong with my Apache configuration, but I can't see what. I'm desperate for help.
Some general information: I'm using Django 1.4.5 (because this is the version on the university server), and Apache 2.4. The project runs in a virtualenv located here /opt/masterportal/mp/env. I also tried it with Alias /static/ instead of Alias /static, but that didn't work either. My apache2.conf is still original - I made no changes there.
Edit: Here's my configuration for the site in /etc/apache2/conf-available/:
<Location "/mp/2015/suse">
ProxyPass https://my-computername:8081/
ProxyPassReverse https://my-computername:8081/
RequestHeader set X-FORWARDED-PROTOCOL ssl
RequestHeader set X-FORWARDED-SSL on
</Location>
Try to move the lines
Alias /static /opt/masterportal/mp/mp/static
<Directory /opt/masterportal/mp/mp/static>
Require all granted
</Directory>
before
WSGIDaemonProcess masterportal python-path=/opt/masterportal/mp:/opt/masterportal/mp/env/dev/lib/python2.7/site-packages
WSGIProcessGroup masterportal
WSGIScriptAlias / /opt/masterportal/mp/mp/apache/wsgi.py
because the link with /static/some_static_files... might be forwared to the wsgi app instead of pointing to the static directory.
I have the following problem:
I cannot remotely access my django app with Apache+mod_wsgi.
But I can remotely access my django app with the django development server with manage.py runserver 0.0.0.0:8000. And I can locally access my django app with Apache+mod_wsgi on my local computer trough port 80.
So I would like to know why I cannot access remotely trough Apache.
This is my httpd.conf (I have just posted what I modified, everything else is as default.)
Listen *:80
LoadModule wsgi_module modules/mod_wsgi.so
<IfModule ssl_module>
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
</IfModule>
WSGIScriptAlias / C:/Users/Ricardo/Dropbox/django_scada/django_scada/apache/wsgi.py
WSGIPythonPath C:/Users/Ricardo/Dropbox/django_scada
Alias /static/ C:/Users/Ricardo/Dropbox/static/
<Directory C:/Users/Ricardo/Dropbox/static/>
Order deny,allow
Allow from all
</Directory>
<Directory C:/Users/Ricardo/Dropbox/django_scada/django_scada/apache>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
I have never configured an Apache server so I am not sure what the problem is. I think the Apache+mod_wsgi integreation works fine because I do not have any problem with the local access, so it must be something about the configuration to remotely acccess Apache. When I try to access from another computer, nothing appears on the access log, however it does when locally.
Which steps should I follow to solve my problems?
Thanks in advance!
Did you check the firewall settings? That port 80 is open to the world?
So actually the problem was related with my router and ISP. I realized this when I set up my network on my University instead of my home.
Hope this helps everyone getting stucked.
I am having trouble setting Django with Apache on Mac OSX Lion (10.7.2). I created a Django project as per the tutorial and can run it on the development server. Now I want to run it locally on my Mac with Apache. I created the two files indicated below.
apache_django_wsgi.conf
WSGIDaemonProcess django
WSGIProcessGroup django
Alias /site_media/ "/Users/David/Dropbox/sites/walble/media/"
<Directory "/Users/David/Dropbox/sites/walble/media">
Order allow,deny
Options Indexes
Allow from all
IndexOptions FancyIndexing
</Directory>
Alias /media/ "/Library/Python/2.7/site-packages/django/contrib/admin/media/"
<Directory "/Library/Python/2.7/site-packages/django/contrib/admin/media">
Order allow,deny
Options Indexes
Allow from all
IndexOptions FancyIndexing
</Directory>
WSGIScriptAlias /walble "/Users/David/Dropbox/sites/walble/apache/walble.wsgi"
<Directory "/Users/David/Dropbox/sites/walble/apache">
Allow from all
</Directory>
walble.wsgi
import os
import sys
sys.path.append('/Users/David/Dropbox/sites/walble')
sys.path.append('/Users/David/Dropbox/sites')
os.environ['DJANGO_SETTINGS_MODULE'] = 'walble.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
In httpd.conf, I enabled
LoadModule wsgi_module libexec/apache2/mod_wsgi.so and then added the following line.
Include /Users/David/Dropbox/sites/walble/apache/apache_django_wsgi.conf
I restarted Apache and then in the browser I typed in http://localhost/walble but I get a forbidden message like so
Forbidden
You don't have permission to access /walble on this server.
Apache/2.2.20 (Unix) DAV/2 mod_wsgi/3.3 Python/2.7.1 PHP/5.3.6 with Suhosin-Patch Server at localhost Port 80
Any suggestions on what I might be doing wrong?
This is a common permissions problem. The Apache user does not have execute permissions on the walble.wsgi file.