We develop a django site on the same server with a php
A lot of help came from the official Django documentation . Nginx is very usefull as Apache eats up a lot of memory. You can deploy Django on nginx using the gunicorn web server, while deploying Php using Apache.
Nginx in front of php, apache and django/gunicorn behind
Install nginx
Installing nginx couldn’t be simpler. Latest stable release is provided via a ppa repository.
$ sudo apt-get install python-software-properties -y $ sudo apt-get update $ sudo apt-get install nginx $
Project structure
The user under which the app will run is webapp, so I checkout my app in /home/webapp.
/home/webapp/app /home/webapp/app/static /home/webapp/env
Note that I’m using virtualenv to deploy this app.
Configure nginx and gunicorn
In your app dir create nginx.conf:
############################################################################### # default ############################################################################### # Default by nginx guideline client_header_timeout 3m; client_body_timeout 3m; send_timeout 3m; client_header_buffer_size 1k; large_client_header_buffers 4 4k; client_max_body_size 100m; client_body_buffer_size 128k; # Hash Table server_names_hash_bucket_size 64; # Proxy proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; server { listen 80 default; # Dydamic Content forward to Apache (mostly php) location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://127.0.0.1:8080; } } ############################################################################### # virtualhost ############################################################################### server { server_name www.esempio.com esempio.com; root /var/www/www.esempio.com/www/; # Static Contents location /static { autoindex on; alias /var/www/www.esempio.com/www/; access_log off; expires 30d; } # Dynamic PHP Content forward to Apache location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://127.0.0.1:8081; } } server { server_name www.esempio.com; root /path/to/app/; # Static Django Contents location /static { autoindex on; alias /path/to/app/static/; access_log off; expires 30d; } # Dynamic DJANGO Content forward to Apache location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://127.0.0.1:8001; } }
Next, I symlinked nginx.conf to the server’s sites-enabled directory.
$ sudo ln -s /home/webapp/app/nginx.conf /etc/nginx/sites-enable/webapp.org
This sets up nginx to directly serve the applications’s static files (css, js, etc.). Everything else is proxied to the gunicorn server.
Now gunicorn is a Python HTTP server. It’s super simple and effective. I installed it into the app’s environment.
$ (env) pip install gunicorn
create and edit:
vi gunicorn.conf.py:
bind = "0.0.0.0:8001" logfile = "/home/webapp/gunicorn.log" workers = 3
The config files are simple and easy to read.
Running
I then collected all the static files into the static directory:
$ (env) python manage.py collectstatic
I restarted nginx:
$ sudo /etc/init.d/nginx restart
And finally, I ran the gunicorn server:
$ (env) cd /home/webapp/app $ (env) gunicorn_django -b 0.0.0.0:8001
Then
Apache
edit
/etc/apache2/ports.conf Listen 8080 <IfModule mod_ssl.c> # If you add NameVirtualHost *:443 here, you will also have to change # the VirtualHost statement in /etc/apache2/sites-available/default-ssl # to <VirtualHost *:443> # Server Name Indication for SSL named virtual hosts is currently not # supported by MSIE on Windows XP. Listen 443 </IfModule> <IfModule mod_gnutls.c> Listen 443 </IfModule>
Edit virtualhosts:
in
/etc/apache2/sites-available <VirtualHost www.esempio.com:8080> ServerName www.esempio.com NameVirtualHost www.esempio.com:8080 ....
sudo /etc/init.d/nginx restart
sudo /etc/init.d/apache2 restart
Summarizing = static nginx port 80, dynamic Apache port 8080, dynamic Django port 8001.
Source =
http://honza.ca/2011/05/deploying-django-with-nginx-and-gunicorn
http://www.ibualoy.net/blog/itech/linux/167-how-to-configure-nginx-and-apache-on-debian