配置python版本和ssh启用root参考其它文章
完整项目路径 /root/projects/mysutra_pro
下级目录/root/projects/mysutra_pro/mysutra是源码目录
root@VM-12-2-debian:~/projects/mysutra_pro# ls db.sqlite3 manage.py ms_env mysutra root@VM-12-2-debian:~/projects/mysutra_pro# ls mysutra/ asgi.py __init__.py __pycache__ settings.py urls.py wsgi.py
前置需求
apt install python3-venv apt install curl apt install python3-pip cd /root/projects/mysutra_pro
建立虚拟环境
(读者请根据项目需求确认是否需要虚拟环境,本文章是在虚拟环境下建立的项目)
root@VM-12-2-debian:~/projects/mysutra_pro# python -m venv ms_env root@VM-12-2-debian:~/projects/mysutra_pro# ls ms_env
激活虚拟环境
root@VM-12-2-debian:~/projects/mysutra_pro# source ms_env/bin/activate (ms_env) root@VM-12-2-debian:~/projects/mysutra_pro#
停止虚拟环境
(ms_env) root@VM-12-2-debian:~/projects/mysutra_pro# deactivate
在虚拟环境下安装配套软件
pip install django
创建项目(记住此条命令生成的路径,非常重要)
项目名称后面必须带有.号
(ms_env) root@VM-12-2-debian:~/projects/mysutra_pro# django-admin startproject mysutra . (ms_env) root@VM-12-2-debian:~/projects/mysutra_pro# ls manage.py ms_env mysutra (ms_env) root@VM-12-2-debian:~/projects/mysutra_pro# pwd /root/projects/mysutra_pro
创建数据库
(ms_env) root@VM-12-2-debian:~/projects/mysutra_pro# python manage.py migrate Operations to perform: Apply all migrations: admin, auth, contenttypes, sessions Running migrations: Applying contenttypes.0001_initial... OK Applying auth.0001_initial... OK Applying admin.0001_initial... OK Applying admin.0002_logentry_remove_auto_add... OK Applying admin.0003_logentry_add_action_flag_choices... OK Applying contenttypes.0002_remove_content_type_name... OK Applying auth.0002_alter_permission_name_max_length... OK Applying auth.0003_alter_user_email_max_length... OK Applying auth.0004_alter_user_username_opts... OK Applying auth.0005_alter_user_last_login_null... OK Applying auth.0006_require_contenttypes_0002... OK Applying auth.0007_alter_validators_add_error_messages... OK Applying auth.0008_alter_user_username_max_length... OK Applying auth.0009_alter_user_last_name_max_length... OK Applying auth.0010_alter_group_name_max_length... OK Applying auth.0011_update_proxy_permissions... OK Applying auth.0012_alter_user_first_name_max_length... OK Applying sessions.0001_initial... OK
运行项目
方法1:
(ms_env) root@VM-12-2-debian:~/projects/mysutra_pro# python manage.py runserver 8081
方法2:
(ms_env) root@VM-12-2-debian:~/projects/mysutra_pro# python manage.py runserver 8081 &
测试运行情况
1.测试方法1运行情况
(在上一个ssh未关闭的情况下,另外开启ssh)
root@VM-12-2-debian:~# curl http://127.0.0.1:8081
测试成功后,在上个SSH终端Ctrl+c退出测试命令
2.测试方法2运行情况
(无需单独开启ssh终端)
(ms_env) root@VM-12-2-debian:~/projects/mysutra_pro# curl http://127.0.0.1:8081
测试成功后,需要按以下命令关闭runserver命令,端口号根据实际情况填写
(ms_env) root@VM-12-2-debian:~/projects/mysutra_pro# lsof -i :8081 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME python 26727 root 4u IPv4 116472 0t0 TCP localhost.localdomain:tproxy (LISTEN) (ms_env) root@VM-12-2-debian:~/projects/mysutra_pro# kill 26727
退出虚拟环境
(ms_env) root@VM-12-2-debian:~/projects/mysutra_pro# deactivate
安装uwsgi
root@VM-12-2-debian:~# python -m pip install uwsgi --upgrade
使用uwsgi运行上面的项目
参考格式:
uwsgi --http :8001 --chdir /path/to/project --home=/path/to/env --module project.wsgi
--home 指定virtualenv 路径,如果没有可以去掉。project.wsgi 指的是 project/wsgi.py 文件,将project改成项目名称即可。项目名称由上面"django-admin startproject mysutra ."这条命令生成。
root@VM-12-2-debian:~/projects/mysutra_pro# uwsgi --http :8081 --chdir /root/projects/mysutra_pro/ --home=/root/projects/mysutra_pro/ms_env/ --module mysutra.wsgi *** Starting uWSGI 2.0.20 (64bit) on [Sat Dec 18 12:14:28 2021] *** compiled with version: 8.3.0 on 18 December 2021 04:08:22 os: Linux-4.19.0-11-amd64 #1 SMP Debian 4.19.146-1 (2020-09-17) nodename: VM-12-2-debian machine: x86_64 clock source: unix detected number of CPU cores: 1 current working directory: /root/projects/mysutra_pro detected binary path: /usr/local/bin/uwsgi !!! no internal routing support, rebuild with pcre support !!! uWSGI running as root, you can use --uid/--gid/--chroot options *** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** chdir() to /root/projects/mysutra_pro/ *** WARNING: you are running uWSGI without its master process manager *** your processes number limit is 7343 your memory page size is 4096 bytes detected max file descriptor number: 1024 lock engine: pthread robust mutexes thunder lock: disabled (you can enable it with --thunder-lock) uWSGI http bound on :8081 fd 4 spawned uWSGI http 1 (pid: 2205) uwsgi socket 0 bound to TCP address 127.0.0.1:32843 (port auto-assigned) fd 3 uWSGI running as root, you can use --uid/--gid/--chroot options *** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** Python version: 3.7.3 (default, Jan 22 2021, 20:04:44) [GCC 8.3.0] PEP 405 virtualenv detected: /root/projects/mysutra_pro/ms_env/ Set PythonHome to /root/projects/mysutra_pro/ms_env/ *** Python threads support is disabled. You can enable it with --enable-threads *** Python main interpreter initialized at 0x55c47c27f670 uWSGI running as root, you can use --uid/--gid/--chroot options *** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** your server socket listen backlog is limited to 100 connections your mercy for graceful operations on workers is 60 seconds mapped 72904 bytes (71 KB) for 1 cores *** Operational MODE: single process *** WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x55c47c27f670 pid: 2204 (default app) uWSGI running as root, you can use --uid/--gid/--chroot options *** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** *** uWSGI is running in multiple interpreter mode *** spawned uWSGI worker 1 (and the only) (pid: 2204, cores: 1)
通过另外的ssh终端进行测试
root@VM-12-2-debian:~# curl http://127.0.0.1:8081
关闭uwsgi
按照端口进行查询:
lsof -i :8002
可以查出:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME uwsgi 2208 tu 4u IPv4 0x53492abadb5c9659 0t0 TCP *:teradataordbms (LISTEN) uwsgi 2209 tu 4u IPv4 0x53492abadb5c9659 0t0 TCP *:teradataordbms (LISTEN)
这时根据 PID 可以用下面的命令 kill 掉相关程序:
kill -9 2208 2209
按照程序名称查询:
ps aux | grep uwsgi
补充内容:
使用 gunicorn 代替 uwsgi 的方法
python -m pip install gunicorn
#在项目目录下运行下面的命令进行测试:
gunicorn -w4 -b0.0.0.0:8001 zqxt.wsgi
-w 表示开启多少个worker,-b 表示要使用的ip和port,我们这里用的是 8001,0.0.0.0代表监控电脑的所有 ip。
如果使用了 virtualenv 可以这样
/path/to/env/bin/gunicorn --chdir /path/to/project --pythonpath /path/to/env/ -w4 -b0.0.0.0:8017 project.wsgi:application
用 --pythonpath 指定依赖包路径,多个的时候用逗号,隔开,如:'/path/to/lib,/home/tu/lib'
安装nginx
root@VM-12-2-debian:~# apt install nginx
安装supervisor
root@VM-12-2-debian:~# python -m pip install supervisor Looking in indexes: http://mirrors.tencentyun.com/pypi/simple Collecting supervisor Downloading http://mirrors.tencentyun.com/pypi/packages/de/c9/50b1575b46ad9802e1b8d444c1ba0db842a1006ff7c9bea9d5e0ea700f2d/supervisor-4.2.2-py2.py3-none-any.whl (743kB) 100% |████████████████████████████████| 747kB 16.8MB/s Installing collected packages: supervisor Successfully installed supervisor-4.2.2
生成 supervisor 默认配置文件,比如我们放在 /etc/supervisord.conf 路径中:
(sudo) echo_supervisord_conf > /etc/supervisord.conf
打开 supervisor.conf 在最底部添加(每一行前面不要有空格,防止报错):
参考格式:
[program:zqxt] command=/path/to/uwsgi --http :8003 --chdir /path/to/zqxt --module zqxt.wsgi directory=/path/to/zqxt startsecs=0 stopwaitsecs=0 autostart=true autorestart=true
command 中写上对应的命令,这样,就可以用 supervisor 来管理了。
本人实际文档
[program:mysutra] command=/usr/local/bin/uwsgi --http :8081 --chdir /root/projects/mysutra_pro/ --home=/root/projects/mysutra_pro/ms_env/ --module mysutra.wsgi directory=/root/projects/mysutra_pro/ startsecs=0 stopwaitsecs=0 autostart=true autorestart=true
启动supervisor
root@VM-12-2-debian:~/projects/mysutra_pro# supervisord -c /etc/supervisord.conf
重启mysutra程序(项目)
root@VM-12-2-debian:~/projects/mysutra_pro# supervisorctl -c /etc/supervisord.conf restart mysutra mysutra: stopped mysutra: started
启动,停止,或重启 supervisor 管理的某个程序 或 所有程序:
(sudo) supervisorctl -c /etc/supervisord.conf [start|stop|restart] [program-name|all]
使用uwsgi的ini配置文件管理
root@VM-12-2-debian:~/projects/mysutra_pro# vim uwsgi.ini root@VM-12-2-debian:~# cat ~/projects/mysutra_pro/uwsgi.ini [uwsgi] ;socket=/root/projects/mysutra_pro/mysutra.sock ;socket=:8081 http=:8081 chdir=/root/projects/mysutra_pro module=mysutra.wsgi ;wsgi-file=mysutra.wsgi.py ;touch-reload=/root/projects/mysutra_pro/reload master=true processes=2 threads=4 ;chmod-socket=664 ;chown-socket= vacuum=true home=/root/projects/mysutra_pro/ms_env ;virtualenv=/root/projects/mysutra_pro/ms_env
修改supervisor配置文件
修改 supervisor 配置文件中的 command 一行:
root@VM-12-2-debian:~/projects/mysutra_pro# cat /etc/supervisord.conf ;...... ;...... ;在此配置文件最后内容...... ;[include] ;files = relative/directory/*.ini [program:mysutra] ;command=/usr/local/bin/uwsgi --http :8081 --chdir /root/projects/mysutra_pro/ --home=/root/projects/mysutra_pro/ms_env/ --module mysutra.wsgi command=/usr/local/bin/uwsgi --ini /root/projects/mysutra_pro/uwsgi.ini directory=/root/projects/mysutra_pro/ startsecs=0 stopwaitsecs=0 autostart=true autorestart=true
重启mysutra应用
root@VM-12-2-debian:~/projects/mysutra_pro# supervisorctl -c /etc/supervisord.conf restart mysutra mysutra: stopped mysutra: started #或者 root@VM-12-2-debian:~/projects/mysutra_pro# supervisorctl -c /etc/supervisord.conf restart all mysutra: stopped mysutra: started
配置nginx
root@VM-12-2-debian:~/projects/mysutra_pro# cat /etc/nginx/sites-available/mysutra.conf
server{
listen 80;
server_name mysutra.xyz;
charset utf-8;
client_max_body_size 75M;
location / {
include /etc/nginx/uwsgi_params;
uwsgi_pass 127.0.0.1:8081;
uwsgi_read_timeout 15;
}
}
参数说明
listen:此处填写nginx默认端口,如果你修改了nginx的默认端口,则这里也要改成相应端口
server_name:域名映射,你申请的域名...省略http//、https//
以下是location /{}内部的参数说明
include:你会需要 uwsgi_params 文件,可用在uWSGI发行版本的 nginx 目录下,或者从 https://github.com/nginx/nginx/blob/master/conf/uwsgi_params 找到。
uwsgi_pass:对应/root/projects/mysutra_pro/uwsgi.ini内的端口配置
激活网站
ln -s /etc/nginx/sites-available/mysutra.conf /etc/nginx/sites-enabled/mysutra.conf
测试配置语法问题
service nginx configtest 或 /path/to/nginx -t
重启 nginx 服务器:
service nginx reload 或service nginx restart 或/path/to/nginx -s reload
修改项目配置文件,使其支持外网访问
root@VM-12-2-debian:~/projects/mysutra_pro# vim /root/projects/mysutra_pro/mysutra/settings.py #....... DEBUG = True #DEBUG=False #修改下面代码 ALLOWED_HOSTS = ['*']
重启应用
root@VM-12-2-debian:~/projects/mysutra_pro# supervisorctl -c /etc/supervisord.conf restart all mysutra: stopped mysutra: started
网页测试,在浏览器地址栏输入http://mysutra.xyz:8081/,可以看到django默认的debug界面,表示配置成功。
注意:每次开机都要输入以下命令激活自定义的网页
supervisord -c /etc/supervisord.conf supervisorctl -c /etc/supervisord.conf restart all
域名+端口访问项目的方式是不能满足实际项目需求的,你需要将nginx默认界面改成你的默认网页界面。



