First, the premise:
1, the Django project file has been placed on the cloud server, the configuration of the operating environment, to operate properly
2, the cloud server can be connected properly
Second, relevant knowledge
1, Python manage.py runserver: This is a suitable for the development phase of the use of the server, can not be processed for a large number of requests, not suitable for running in the real production environment, in the actual production environment using WSGI as an interface, Connect the web and Python code (that is, the Django project);
2, Wsgi:web server Gateway Interface, English is Python Web server, Gateway Interface, abbreviated as WSGI, is a Python application or framework and Web server interface between;
3, Wsgi no official implementation, because WSGI more like a protocol, as long as they comply with these protocols, WSGI application (application) can be run on any server (server);
4, Django in the creation of project, will automatically generate wsgi.py files, which will indicate settings, application objects;
5, Uwsgi: The front said WSGI similar protocol, can be achieved through UWSGI, Uwsgi is written in C language, running fast, is a fast, self-repair, developer and system administrator-friendly server;
6, the main role of Nginx:nginx has two: Load balancing (multiple servers take turns processing requests, as much as possible to make multiple servers evenly across the request pressure); Reverse proxy (hide the real server, more secure);
7, when using NGINX+UWSGI configuration server, Dynamic Data request implementation of the schema (static data directly have Nginx feedback to the user):
Client request Nginx, and then by Nginx request Uwsgi, run the Python code under the Django framework
The general direction of the entire configuration process is: Configure UWSGI, configure Nginx, collect static files.
Iii. modifications to the project prior to deployment
Main changes setting.py:
DEBUG = FalseALLOW_HOSTS=[‘*‘,]表示可以访问服务器的ip
If the project involves a domain name, you also need to change, such as the mailbox verification, reset password link and other features of the
Iv. installation and configuration of UWSGI
Installation:
pip install uwsgi
Create a new Uwsgi.ini file under the project root directory (eg: project directory/var/www/project1) as follows:
[uwsgi]socket=127.0.0.1:8000 # 外网ip:端口(使用nginx连接时,使用socket)# http=外网ip:端口(直接做web服务器,使用http,与socket只能选择一个使用)chdir=/var/www/project1 # 项目根目录,绝对路径wsgi-file=project1/wsgi.py # 项目中wsgi.py文件的目录,相对于项目根目录processes=4threads=2master=Truepidfile=uwsgi.piddaemonize=uswgi.log
Related commands:
Start: Uwsgi--ini Uwsgi.ini
Stop: Uwsgi--stop uwsgi.pid
Restart: Uwsgi--reload uwsgi.pid
You can use PS Ajx|grep uwsgi to view the running status, this time to visit the URL, found that static files can not be loaded, continue below.
Five, installation and configuration of Nginx
Installation:
sudo apt-get nginx
Configuration, locate the installation directory, edit the conf/nginx.conf file under the installation directory, and if not found, use the following command to find it:
sudo find / -namge nginx.conf
To modify the configuration:
sudo vim /ect/nginx/conf/nginx.conf # (安装目录换成自己的)
To configure routing for steering:
location / { # 如果没有这一项,在
server内自行添加)
include uwsgi_params;
uwsgi_pass 127.0.0.1:8000; # 将用户的请求转向的地址
}
To configure a static file directory, create a new static folder:
$sudo chmod 777 /var/www/
proje1 # 权限更改为所有用户均有读写权限
/var/www/
proje1
$mkdir static # 新建静态文件夹,用来收集、存放静态文件
Configuring a static file directory in Nginx
location /static { # 配置静态文件路径 alias /var/www/proje1/static/; # 此目录必须有用户的读写权限}
To modify the setting configuration:
STATIC_ROOT=‘/var/www/project1/static/‘STATIC_URL=‘/static/‘
To collect static files:
$cd /var/www/project1
$python manage.py collectstatic
Related commands:
View version: sudo sbin/nginx-v
Start: sudo sbin/nginx
Stop: sudo sbin/nginx-s stop
Restart: sudo sbin/nginx-s reload
Restart the nginx after the entire configuration is complete and restart the Uwsgi
Deploy Python+django project with NGINX+UWSGI on Amazon Cloud Server full version (ii)--deployment configuration and related knowledge