Admin
操作系统:Centos7 x64 1804
服务:Nginx + uwsgi + djaongo项目
项目文件大致结构,整个项目是放在/projects/目录下:
startblog #项目根目录 上级目录/projects/--startblog #项目同名目录----setting.py----wsgi.py--app_main #app…--app_2……
1.Python环境:python3环境搭建
2.数据库:Mysql-community-server安装
3.防火墙:打开8000端口、80端口
4.代码拷贝到服务器,安装requirement.txt下所有依赖
5.修改setting.py中:ALLOWED_HOSTS = ['*']
6.尝试运行:
python3 manage.py runserver 8000
7.不出意外,应该就能成功运行,并能从浏览器通过 IP地址:8000 访问
8.提取static文件到项目根目录
python3 manage.py collectstatic
使用uwsgi来承载运行django项目,因为manage.py方式运行效率很低。
pip3 install uwsgiln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx
[uwsgi]socket = :8000chdir = /projects/startblogwsgi-file = startblog/wsgi.pymodule = startblog.wsgi:applicationmaster = Trueprocesses = 2threads = 2chmod-socket = 666vacuum = Truepidfile = /projects/startblog/uwsgi.pid
uwsgi --ini uwsgi.ini
不出意外,也能通过浏览器访问 IP:8000
最后将“uwsgi.ini”文件中socket行取消注释,注释http行(因为uwsgi和nginx通过socked通讯)
至此,uwsgi端就绪,通过命令“uwsgi --ini uwsgi.ini”启动并保持
不要使用yum仓库中的nginx,配置文件完全不一样,一直配不成功
wget http://nginx.org/download/nginx-1.13.7.tar.gztar -zxvf nginx-1.13.7.tar.gzcd nginx-1.13.7./configuremakemake installln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
cd /usr/local/nginx/confcp nginx.conf nginx.conf.backvim nginx.conf
user root; # 修改root运行#user nobody;worker_processes 1;#error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#pid logs/nginx.pid;events {worker_connections 1024;}http {include mime.types;default_type application/octet-stream;#log_format main '$remote_addr - $remote_user [$time_local] "$request" '# '$status $body_bytes_sent "$http_referer" '# '"$http_user_agent" "$http_x_forwarded_for"';#access_log logs/access.log main;sendfile on;#tcp_nopush on;#keepalive_timeout 0;keepalive_timeout 65;#gzip on;# 添加新的80端口server {listen 80;server_name localhost;charset utf-8;location / {include uwsgi_params;uwsgi_pass 127.0.0.1:8000;}location /static/ {alias /projects/startblog/static/;}location /media/ {alias /projects/startblog/media/;}}# 移走老的80server {listen 8080;server_name localhost;#charset koi8-r;#access_log logs/host.access.log main;location / {root html;index index.html index.htm;}……
一共三处修改,自上而下分别是修改nginx运行身份为root、添加80端口转发给本机8000端口并且静态文件和Media文件直接通过nginx读取、将之前的80端口项目改成8080(弃用,避免端口冲突)
nginx
因为前面已经打开了80端口,就可以直接访问 IP 应该就能看到页面了
教程中nginx是通过源代码安装的,没有注册系统服务,将nginx加入系统服务,以便使用systemctl控制
cd /usr/local/nginx/confvim nginx.conf`
...pid logs/nginx.pid;…
取消注释上述pid行
ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginxvim /usr/lib/systemd/system/nginx.service
添加文件内容
#nginx服务配置到该文件中#服务描述性的配置[Unit]Description=nginx - high performance web serverDocumentation=http://nginx.org/en/docs/After=network.target remote-fs.target nss-lookup.target#服务关键配置[Service]Type=forking#pid文件位置#要与nginx配置文件中的pid配置路径一致,这个很重要,否则会服务启动失败PIDFile=/usr/local/nginx/logs/nginx.pid#启动前检测 nginx配置文件 是否正确ExecStartPre=/usr/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf#启动ExecStart=/usr/sbin/nginx -c /usr/local/nginx/conf/nginx.conf#重启ExecReload=/bin/kill -s HUP $MAINPID#关闭ExecStop=/bin/kill -s QUIT $MAINPIDPrivateTmp=true[Install]WantedBy=multi-user.target
保存后杀掉后台nginx、用systemctl控制服务
pkill nginxsystemctl daemon-reloadsystemctl start nginxsystemctl stop nginxsystemctl enable nginx
参考连接:https://my.oschina.net/taoluoluo/blog/749134
类似地,uwsgi启动django项目也可以写入systemctl:
vim /projects/startblog/uwsgi.ini
[uwsgi]socket = :8000chdir = /projects/startblogwsgi-file = startblog/wsgi.pymodule = startblog.wsgi:applicationmaster = Trueprocesses = 2threads = 2chmod-socket = 666vacuum = Truepidfile = /projects/startblog/uwsgi.pid # 添加pid文件
末尾一行行就是uwsgi运行时的进程id文件, 注意一定要用绝对路径
cd /projectsvim blogd
添加文件内容
#!/bin/bashif [ ! -n "$1" ];then #$1:指该脚本后跟的第一个参数,-n:判断$1是否为非空,!:取相反echo "Usages: blogd [start|stop|restart]"exit 0fipro_root=/projects/startblogdo_start(){nohup /usr/bin/uwsgi --ini $pro_root/uwsgi.ini > $pro_root/uwsgi.log 2>&1 &echo "Start uwsgi service [OK]"sleep 3ps -aux | grep uwsgi}do_stop(){ps -aux | grep uwsgipid=`cat $pro_root/uwsgi.pid`if [ ! $pid ]; thenecho "Pid not found"elseecho $pidkill -INT $pidecho "Stop uwsgi service [OK]"fi}if [ $1 = start ];thendo_startelif [ $1 = stop ];thendo_stopelif [ $1 = restart ];thendo_stopsleep 3do_startelseecho "Usages: blogd [start|stop|restart]"fi
chmod 777 blogdln -s /projects/blogd /usr/bin/blogdln -s /projects/blogd /usr/sbin/blogd
测试好不好使:
blogd startblogd stopblogd restartps -aux | grep uwsgi
ln -s /usr/local/python3/bin/uwsgi /usr/sbin/uwsgivim /usr/lib/systemd/system/blog.service
添加文件内容
#uwsgi服务配置到该文件中#服务描述性的配置[Unit]Description=uwsgi web serverDocumentation=google.comAfter=network.target remote-fs.target nss-lookup.target#服务关键配置[Service]Type=forking#启动ExecStart=/usr/sbin/blogd start#重启ExecReload=/usr/sbin/blogd restart#关闭ExecStop=/usr/sbin/blogd stopPrivateTmp=true[Install]WantedBy=multi-user.target
chmod 777 /usr/lib/systemd/system/blog.servicesystemctl daemon-reload
测试好不好使
systemctl start blogsystemctl stop blogsystemctl restartblogps -aux | grep uwsgi
https://www.cnblogs.com/hanhy/articles/7278336.html
http://uwsgi-docs-zh.readthedocs.io/zh_CN/latest/Management.html