Background: today carried out a project to deploy (N + 1 times the first deployment of the project), so I decided to put together a complete material, directly next CTRL + C , CTRL + V on OK.
table of Contents
A, Django project configuration
Create a project is not to say, and personal recommendations to create a virtual environment.
Change the file setting.py
DEBUG = False
ALLOW_HOSTS = ['*'] #表示可以访问服务器的ip,*表示所有
Two, uWSGI configuration
-
Installation uWSGI
pip install uwsgi
-
Under the new project directory
uwsgi.ini
to fill in the configuration file[uwsgi] socket = 127.0.0.1:8080 # 和nginx通信的端口,直接做web服务器时把socket换成http chdir = /home/nginx_test # 项目路径(我的测试项目叫test) wsgi-file = /home/nginx_test/nginx_test/wsgi.py # wsgi.py文件的路径 processes = 4 threads = 2 master = true pidfile = uwsgi.pid # pid文件,用于脚本启动、停止进程 daemonize = uswgi.log # 日志文件
-
Start uWSGI
Before starting Note:
- Before starting the best to use
python manage.py runserver
to start the test to make sure that there is no problem - uwsgi log will
uwsgi.ini
generate in the relative path, if there is an error can be viewed in this log (uwsgi.log) in
启动:uwsgi --ini uwsgi.ini 停止:uwsgi --stop uwsgi.pid 重启:uwsgi --reload uwsgi.pid
It started successfully expressed as follows:
(venv) [root@localhost nginx_test]# uwsgi --ini uwsgi.ini [uWSGI] getting INI configuration from uwsgi.ini (venv) [root@localhost nginx_test]#
- Before starting the best to use
Three, Nginx configuration
-
Install Nginx
I downloaded the nginx-1.9.9, others please visit: http: //nginx.org/download/
cd /home/ # 找一个目录下载 Nginx yum -y install gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel # 安装依赖包 wget http://nginx.org/download/nginx-1.9.9.tar.gz # 下载源码包 tar -zxf nginx-1.9.9.tar.gz # 解压源码包 cd nginx-1.9.9/ # 进入目录 ./configure --prefix=/usr/local/nginx # 安装路径为/usr/local/nginx make && make install # 编译安装
-
Start Nginx
/usr/local/nginx/sbin/nginx # 启动 /usr/local/nginx/sbin/nginx -s stop # 停止 /usr/local/nginx/sbin/nginx -s reload # 重启
There is no prompt instructions to start successfully
after starting the input virtual machine or server on the browser IP test, following figure illustrates Nginx start successfully:If you can not access may be a firewall problem:
turn off the firewall: systemctl stop firewalld.service
disable the firewall boot from the start: systemctl disable firewalld.service -
Configuration nginx.conf
open the/usr/local/nginx/conf/nginx.conf
file, follow these kinds modify or adduser root; #在文件的开头加上 http { ...... server { listen 80; #需要监控的端口 server_name localhost; location / { include uwsgi_params; uwsgi_pass 127.0.0.1:8080; #与uwsgi中ip:端口相同 uwsgi_send_timeout 600; } #静态文件 location /static { alias /home/nginx_test/static/; #静态文件路径 } } ...... }
Like modified (too much text, did not cut the whole, the red box to modify the contents of):
-
Restart Nginx
/usr/local/nginx/sbin/nginx # 启动 /usr/local/nginx/sbin/nginx -s stop # 停止 /usr/local/nginx/sbin/nginx -s reload # 重启
-
test
80-port it can be omitted
urls.py:
views.py
Fourth, how to troubleshoot configuration after error
- ip is correct
- The correct port (Nginx configuration file settings: the listen 80 )
- Nginx and uwsgi connections are correct (whether ip port nginx.conf and uwsgi.ini connection agreement)
- Whether the project can be started (use
python manage.py runserver
test)