In the previous chapter, we used python manage.py runserver To run the server. This applies only to test environments.
For officially released services, we need a stable and sustainable server, such as apache, Nginx, lighttpd, etc. This article will take Nginx as an example.
You can also refer directly to: Python uwsgi 安装配置
7.19.1. Install the basic development package ¶
The installation steps under 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 install Python2.7.5 again:
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
7.19.2. 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 and pip uninstall to manage Python packages. Easy_install does not have this feature, only uninstall.
7.19.3. Install uwsgi ¶
Uwsgi: https://pypi.python.org/pypi/uWSGI
Details of uwsgi parameters: http://uwsgi-docs.readthedocs.org/en/latest/Options.html
pip install uwsgi
uwsgi --version # 查看 uwsgi 版本
Test if the uwsgi is working:
Create a new test.py file as follows:
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return "Hello World"
Then run it on the terminal:
uwsgi --http :8001 --wsgi-file test.py
Type: http://127.0.0.1:8001 in the browser to see if there is a “Hello World” output. If there is no output, please check your installation process. To test whether the django is working, run: Type: http://127.0.0.1:8002 in the browser to check that django is working properly.Install Django ¶
pip install django
django-admin.py startproject demosite
cd demosite
python2.7 manage.py runserver 0.0.0.0:8002
7.19.4. Install Nginx ¶
The 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 安装配置 Learn more.
7.19.5. Uwsgi configuration ¶
Uwsgi supports ini, xml and other configuration methods. Take ini as an example, create a new uwsgi9090.ini under the / etc/ directory and add the following configuration:
[uwsgi]
socket = 127.0.0.1:9090
master = true //主进程
vhost = true //多站模式
no-site = true //多站模式时不设置入口模块和文件
workers = 2 //子进程数
reload-mercy = 10
vacuum = true //退出、重启时清理文件
max-requests = 1000
limit-as = 512
buffer-size = 30000
pidfile = /var/run/uwsgi9090.pid //pid文件,用于下面的脚本启动、停止该进程
daemonize = /website/uwsgi9090.log
7.19.6. Nginx configuration ¶
找到nginx的安装目录(如:/usr/local/nginx/),打开conf/nginx.conf文件,修改server配置:
server {
listen 80;
server_name localhost;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:9090; //必须和uwsgi中的设置一致
uwsgi_param UWSGI_SCRIPT demosite.wsgi; //入口文件,即wsgi.py相对于项目根目录的位置,“.”相当于一层目录
uwsgi_param UWSGI_CHDIR /demosite; //项目根目录
index index.html index.htm;
client_max_body_size 35m;
}
}
You can read. Nginx 安装配置 Learn more.
After the setup is complete, run on the terminal:
uwsgi --ini /etc/uwsgi9090.ini &
/usr/local/nginx/sbin/nginx
Type: http://127.0.0.1 in the browser, and you can see the “It work” of django.