nginx uwsgi django supervisord Centos部署

1,建立工程目录css

mkdir /hk 做为主工做目录
cd /hk

2,nginx 安装html

wget  nginx.org/packages/mainline/centos/7/x86_64/RPMS/nginx-1.9.9-1.el7.ngx.x86_64.rpm
rpm -ivh nginx-1.9.9-1.el7.ngx.x86_64.rpm
yum install nginx
systemctl start nginx.service
//在Centos7 下原service 变成systemctl
systemctl enable nginx.service 
//设置nginx 开机启动
// nginx 参数配置
cd /etc/nginx/conf.d
vim myproject.conf
//内容以下
server {
	listen 80;
    server_name 192.168.1.198; //本机IP或者域名
  	charset utf-8;
	
	client_max_body_size 75M;
	
	location /media {
		alias /hk/myproject/media;
	}
     
        location /static {
		alias /hk/myproject/static;
	}

	location / {
		uwsgi_pass 127.0.0.1:8001; //关键参数,这个参数就是nginx与uwsgi链接的套接字
		include /etc/nginx/uwsgi_params;
	}
}

3, 建立django projectpython

django-admin.py startproject myproject
cd myproject
python manage.py startapp mysites
vim myproject/settings.py 
//加入app mysites 保存
python manage.py migrate
# 防火墙开放端口 8001 用于测试,用后须要关闭
firewall-cmd --add-port=8001/tcp --permanent
firewall-cmd reload
python runserver 0.0.0.0:8001
//在其它电脑输入IP:8001就是django默认的首页了

// 这是一个最基本的工程,就只是django的默认的首页
// 通常状况下须要在/hk/myproject 下还要增长如 static 静太文件, templates 这些文件夹
// 在这种状况下,django的后台默认的css是不能加载,须要collection static

4,uwsgi 安装mysql

pip install uwsgi
# 防火墙开放端口 8001 用于测试,用后须要关闭
firewall-cmd --add-port=8001/tcp --permanent
firewall-cmd reload
cd /hk/myproject
# 建立测试uwsgi服务器
vim test_uwsgi.py
//内容以下
def application(env, start_response):
    start_response('200 OK', [('Content-Type', 'text/html')])
    return 'Hello World'
//保存 执行以下命令
uwsgi --http :8001 --wsgi-file test_uwsgi.py
//这个时修改在其它电脑输入IP地址:8001就会出现
//Hello World
//表示uwsgi服务良好
//但这并非咱们想要的目的
//咱们须要在uwsgi 是配制文件式
//因此建立 /hk/uwsgi_myproject.ini
//并在文件里加入
[uwsgi]
socket = 127.0.0.1:8001
chdir = /hk/myprject/
wsgi-file = myprject/wsgi.py
master = true
processes = 2
threads = 2
limit-as = 512
deamonize = /var/log/uwsgi/uwsgi8001.log
//保存
uwsgi --ini /hk/hkwebsite/uwsgi_myproject.ini & 
// 这个是已经后台进程运行了

5 运行nginx

//咱们先运uwsgi 
uwsgi --ini /hk/hkwebsite/uwsgi_myproject.ini & 
// 因为防火墙的存这个nginx与uwsgi是不能建链接的,也就是转发如要开启这项功能,代码以下
setsebool -P httpd_can_network_connect 1
//而后 从新加载
systemctl reload nginx.service
// 在其它电脑输入IP地址就,nginx默认端口80就转发uwsgi 8001上了
// 关闭外网直接访问8001端口, 开启防火墙
firewall-cmd --zone=public --remove-port=8001/tcp --permanent
firewall-cmd --reload

6 加入supervisorgit

//保证开机自启动, 通常状况下还会有mysql redis 这样的服务须要启动,在centos下默认为启动就能够了
//对于程序,还须要更多的一些内容
//如django 里加入celery 作定任务,
//如后台tcp服务器,都须要作成supervisor管理的任务,当出现问题的时候能快束的被发现
//supervisor 配置
[program:myproject]
command=uwsgi --ini /hk/uwsgi_myproject.ini             ; the program (relative uses PATH, can take args)
stopsignal=QUIT
autostart=true
autrestart=true

//supervisord.conf开机启动
vim /lib/systemd/system/supervisord.service
# dservice for systemd (CentOS 7.0+) 
# by ET-CS (https://github.com/ET-CS) 
[Unit]
Description=Supervisor daemon

[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /hk/supervisord.conf
ExecStop=/usr/bin/supervisorctl shutdown
ExecReload=/usr/bin/supervisorctl reload
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target

# 保存后
开启开机启动
systemctl enable supervisord.service 

systemctl start/restart/stop supervisord.service
# reboot

开机就自行启动,若是咱们须要在增长supervisor来管理程序,只须要在supervisord.conf 方件里增长相关程序就能够了
相关文章
相关标签/搜索