foreword
Compared with learning Django, deployment should be a more difficult item in the whole process, especially for some self-learners. For those who have never contacted the server, I believe that this part has persuaded many people! It was the same when I was studying!
Today, Spicy Tiaojun will give you a detailed disassembly and analysis, so that you can avoid detours and fill in holes~
content
1 Test on the development server
Run the development server test to ensure that the website can be opened normally under the development server.
cd 项目目录
python manage.py runserver
2 Install nginx and required packages
sudo apt-get install python-dev nginx
3 Deploy with uwsgi
install uwsgi
sudo pip install uwsgi --upgrade
Run the project with uwsgi
uwsgi --http :8001 --chdir /path/to/project --home=/path/to/env --module project.wsgi
This can be run, -home specifies the virtualenv path, if not, you can remove it. project.wsgi refers to the project/wsgi.py file
Note: If the prompt port is occupied, we can first find out the process number corresponding to the port, and then kill the process
lsof -i :8001 # 根据端口后进行查询 查询结果中的PID就是进程号,如果相关进程有多个,那就杀多个
sudo kill -9 进程号 #根据进程号杀死进程
4 Use supervisor to manage processes
Install the supervisor package
(sudo) pip install supervisor
Generate the supervisor default configuration file, for example, we put it in the /etc/supervisord.conf path:
(sudo) echo_supervisord_conf > /etc/supervisord.conf
Open supervisor.conf and add at the bottom (do not have spaces before each line to prevent errors):
[program:fmxm]
command=/path/to/uwsgi --http :8003 --chdir /path/to/fuxm --module fmxm.wsgi
directory=/path/to/fmxm
startsecs=0
stopwaitsecs=0
autostart=true
autorestart=true
Write the corresponding command in command, so that it can be managed by supervisor.
start supervisor
(sudo) supervisord -c /etc/supervisord.conf
Restart the project:
(sudo) supervisorctl -c /etc/supervisord.conf restart fmxm
Start, stop, or restart a program or all programs managed by the supervisor:
(sudo) supervisorctl -c /etc/supervisord.conf [start|stop|restart] [program-name|all]
Taking uwsgi as an example, the above one-line command is too long. We use the ini configuration file to do it. For example, the project is in the location of /home/tu/fmxm.
Create a new uwsgi.ini in it and the full path is /home/tu/fmxm/uwsgi.ini
[uwsgi]
socket = /home/tu/fmxm/fmxm.sock
chdir = /home/tu/fmxm
wsgi-file = fmxm/wsgi.py
touch-reload = /home/tu/fmxm/reload
processes = 2
threads = 4
chmod-socket = 664
chown-socket = tu:www-data
vacuum = true
Note the /home/tu/fmxm/fmxm.sock above, we'll associate it with nginx in a moment.
Create a new blank reload file on the project, as long as you touch this file (touch reload) the project will restart.
Note: It is not recommended to put sock files under /tmp, such as /tmp/xxx.sock (not recommended)!
The temporary files of some
sudo mkdir -p /tmp2/ && sudo chmod 777 /tmp2/ #然后可以用 /tmp2/fmxm.sock 这样的路径了
Modify the command line in the supervisor configuration file:
[program:fmxm]
command=/path/to/uwsgi --ini /home/tu/fmxm/uwsgi.ini
directory=/path/to/fmxm
startsecs=0
Then restart the supervisor:
(sudo) supervisorctl -c /etc/supervisord.conf restart fmxm
或者
(sudo) supervisorctl -c /etc/supervisord.conf restart all
5 Configure Nginx
Create a new configuration file
sudo vim /etc/nginx/sites-available/fmxm.conf
write the following
server {
listen 80;
server_name www.ziqiangxuetang.com;
charset utf-8;
client_max_body_size 75M;
location /media {
alias /path/to/project/media;
}
location /static {
alias /path/to/project/static;
}
location / {
uwsgi_pass unix:///home/tu/fmxm/fmxm.sock;
include /etc/nginx/uwsgi_params;
}
}
Activate website
sudo ln -s /etc/nginx/sites-available/fmxm.conf /etc/nginx/sites-enabled/fmxm.conf
Test configuration syntax
sudo service nginx configtest 或 /path/to/nginx -t
Restart the nginx server:
sudo service nginx reload 或 sudo service nginx restart 或 /path/to/nginx -s reload