Django Nginx+uwsgi Installation and Configuration
In previous sections, we used python manage.py runserver to run the server. This is only suitable for use in a testing environment.
For a production release, we need a stable and persistent server, such as Apache, Nginx, lighttpd, etc. This article will use Nginx as an example.
>
You can also refer directly to: Python uwsgi Installation and Configuration
Install Basic Development Packages
Installation steps on CentOS are as follows:
yum groupinstall "Development tools"
yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel
CentOS comes with Python 2.4.3, but we can also install Python 2.7.5:
cd ~
wget http://python.org/ftp/python/2.7.5/Python-2.7.5.tar.bz2
tar xvf Python-2.7.5.tar.bz2
cd Python-2.7.5
./configure --prefix=/usr/local
make && make altinstall
Install Python Package Management
easy_install package: https://pypi.python.org/pypi/distribute
Installation steps:
cd ~
wget https://pypi.python.org/packages/source/d/distribute/distribute-0.6.49.tar.gz
tar xf distribute-0.6.49.tar.gz
cd distribute-0.6.49
python2.7 setup.py install
easy_install --version
pip package: https://pypi.python.org/pypi/pip
The advantage of installing pip is that you can use pip list, pip uninstall to manage Python packages. easy_install does not have this functionality, only uninstall.
Install uwsgi
uwsgi: https://pypi.python.org/pypi/uWSGI
uwsgi parameter details: http://uwsgi-docs.readthedocs.org/en/latest/Options.html
pip install uwsgi
uwsgi --version # Check uwsgi version
Test if uwsgi is working properly:
Create a new file named test.py with the following content:
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return "Hello World"
Then run in the terminal:
uwsgi --http :8001 --wsgi-file test.py
Enter http://127.0.0.1:8001 in your browser to see if "Hello World" is output. If there is no output, check your installation process.
Install Django
pip install django
Test if Django is working properly by running:
django-admin.py startproject demosite
cd demosite
python2.7 manage.py runserver 0.0.0.0:8002
Enter http://127.0.0.1:8002 in your browser to check if Django is running correctly.
Install Nginx
Installation commands are as follows:
cd ~
wget http://nginx.org/download/nginx-1.5.6.tar.gz
tar xf nginx-1.5.6.tar.gz
cd nginx-1.5.6
./configure --prefix=/usr/local/nginx-1.5.6 \
--with-http_stub_status_module \ --with-http_gzip_static_module make && make install
You can read [Nginx Installation and Configuration](../linux/nginx-install-setup.html) for more details.
---
## uwsgi Configuration
uwsgi supports multiple configuration formats such as ini, xml, etc. This article uses ini as an example.
Create a new file named `uwsgi9090.ini` in the `/etc/` directory and add the following configuration:
[uwsgi]
socket = 127.0.0.1:9090
master = true // Main process
vhost = true // Multi-site mode
no-site = true // Do not set entry module and file in multi-site mode
workers = 2 // Number of child processes
reload-mercy = 10
vacuum = true // Clean up files on exit and restart
max-requests = 1000
limit-as = 512
buffer-size = 30000
pidfile = /var/run/uwsgi9090.pid // PID file for script to start and stop the process
daemonize = /website/uwsgi9090.log
---
## Nginx Configuration
Locate the Nginx installation directory (e.g., `/usr/local/nginx/`), open the `conf/nginx.conf` file, and modify the server configuration:
server { listen 80; server_name localhost;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:9090; // Must be consistent with uwsgi settings
uwsgi_param UWSGI_SCRIPT demosite.wsgi; // Entry file, i.e., the relative position of wsgi.py to the project root directory, "." represents one level of directory
uwsgi_param UWSGI_CHDIR /demosite; // Project root directory
index index.html index.htm;
client_max_body_size 35m;
}
}
You can read [Nginx Installation and Configuration](../linux/nginx-install-setup.html) for more details.
After setting up, run in the terminal:
uwsgi --ini /etc/uwsgi9090.ini & /usr/local/nginx/sbin/nginx ```
Enter http://127.0.0.1
in your browser, and you will see Django's "It works".