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 uwsgi
ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx
[uwsgi]
socket = :8000
chdir = /projects/startblog
wsgi-file = startblog/wsgi.py
module = startblog.wsgi:application
master = True
processes = 2
threads = 2
chmod-socket = 666
vacuum = True
pidfile = /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.gz
tar -zxvf nginx-1.13.7.tar.gz
cd nginx-1.13.7
./configure
make
make install
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
cd /usr/local/nginx/conf
cp nginx.conf nginx.conf.back
vim 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/;
}
}
# 移走老的80
server {
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/conf
vim nginx.conf`
...
pid logs/nginx.pid;
…
取消注释上述pid行
ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx
vim /usr/lib/systemd/system/nginx.service
添加文件内容
#nginx服务配置到该文件中
#服务描述性的配置
[Unit]
Description=nginx - high performance web server
Documentation=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 $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
保存后杀掉后台nginx、用systemctl控制服务
pkill nginx
systemctl daemon-reload
systemctl start nginx
systemctl stop nginx
systemctl enable nginx
参考连接:https://my.oschina.net/taoluoluo/blog/749134
类似地,uwsgi启动django项目也可以写入systemctl:
vim /projects/startblog/uwsgi.ini
[uwsgi]
socket = :8000
chdir = /projects/startblog
wsgi-file = startblog/wsgi.py
module = startblog.wsgi:application
master = True
processes = 2
threads = 2
chmod-socket = 666
vacuum = True
pidfile = /projects/startblog/uwsgi.pid # 添加pid文件
末尾一行行就是uwsgi运行时的进程id文件, 注意一定要用绝对路径
cd /projects
vim blogd
添加文件内容
#!/bin/bash
if [ ! -n "$1" ];then #$1:指该脚本后跟的第一个参数,-n:判断$1是否为非空,!:取相反
echo "Usages: blogd [start|stop|restart]"
exit 0
fi
pro_root=/projects/startblog
do_start(){
nohup /usr/bin/uwsgi --ini $pro_root/uwsgi.ini > $pro_root/uwsgi.log 2>&1 &
echo "Start uwsgi service [OK]"
sleep 3
ps -aux | grep uwsgi
}
do_stop(){
ps -aux | grep uwsgi
pid=`cat $pro_root/uwsgi.pid`
if [ ! $pid ]; then
echo "Pid not found"
else
echo $pid
kill -INT $pid
echo "Stop uwsgi service [OK]"
fi
}
if [ $1 = start ];then
do_start
elif [ $1 = stop ];then
do_stop
elif [ $1 = restart ];then
do_stop
sleep 3
do_start
else
echo "Usages: blogd [start|stop|restart]"
fi
chmod 777 blogd
ln -s /projects/blogd /usr/bin/blogd
ln -s /projects/blogd /usr/sbin/blogd
测试好不好使:
blogd start
blogd stop
blogd restart
ps -aux | grep uwsgi
ln -s /usr/local/python3/bin/uwsgi /usr/sbin/uwsgi
vim /usr/lib/systemd/system/blog.service
添加文件内容
#uwsgi服务配置到该文件中
#服务描述性的配置
[Unit]
Description=uwsgi web server
Documentation=google.com
After=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 stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
chmod 777 /usr/lib/systemd/system/blog.service
systemctl daemon-reload
测试好不好使
systemctl start blog
systemctl stop blog
systemctl restartblog
ps -aux | grep uwsgi
https://www.cnblogs.com/hanhy/articles/7278336.html
http://uwsgi-docs-zh.readthedocs.io/zh_CN/latest/Management.html