2019-1-7 16:53:33
uwsgi+nginx+django
- 安装部署uwsgi
- 安装部署nginx
- 代码上传,我的项目名为DjangoWeb
安装服务
#安装uwsgi
pip3 install uwsgi
#安装nginx
yum install -y nginx
-----------测试-----------------------------------
测试uWSGI是否正常,在DjangoWeb目录中创建一个测试用的Python文件uwsgi_test.py
def application(env, start_response):
start_response('200 OK',[('Content-Type', 'text/html')])
#return ['Hello world'] # Python2
return [b'Hello world'] # Python3
#在DjangoWeb项目路径下,基于HTTP协议运行uWSGI,如果uWSGI安装正常的话,可以在浏览器中访问9090端口,看到Hello world字样
#运行命令:
uwsgi --http 127.0.0.1:9090 --wsgi-file uwsgi_test.py
配置文件
- uwsgi.ini配置文件
#uwsgi.ini
# uwsig使用配置文件启动
[uwsgi]
# 项目目录
chdir=/root/DjangoWeb
# 指定项目的application
module=DjangoWeb.wsgi:application
# 指定sock的文件路径
socket=/root/DjangoWeb/django_uwsgi.sock
# 进程个数
workers=5
pidfile=/root/DjangoWeb/uwsgi.pid
# 指定IP端口
http=0.0.0.0:1990
# 指定静态文件
#static-map=/static=/代码路径/项目/静态文件路径
static-map=/static=/root/DjangoWeb/static
# 启动uwsgi的用户名和用户组
uid=root
gid=root
# 启用主进程
master=true
# 自动移除unix Socket和pid文件当服务停止的时候
vacuum=true
# 序列化接受的内容,如果可能的话
thunder-lock=true
# 启用线程
enable-threads=true
# 设置自中断时间
harakiri=30
# 设置缓冲
post-buffering=4096
# 设置日志目录
daemonize=/root/DjangoWeb/django_uwsgi.log
- nginx.conf配置文件
#nginx.conf
upstream django{
#反向代理
server 192.168.127.78:1990;
}
server {
#uwsgi启动django服务
listen 8080;
server_name localhost;
index index.php index.html;
location / {
include uwsgi_params;
uwsgi_pass unix:/root/DjangoWeb/django_uwsgi.sock;
uwsgi_param UWSGI_CHDIR /root/DjangoWeb;
uwsgi_param UWSGI_SCRIPT django_wsgi;
access_log /root/DjangoWeb/access.log;
}
location /static {
alias /root/DjangoWeb/static;
}
}
server {
#配合upstream反向代理
listen 808;
server_name localhost;
index index.php index.html;
location / {
proxy_pass http://django;
access_log /root/DjangoWeb/access_nginx.log;
}
}
}
启动的端口及进程
[root@python DjangoWeb]# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 1083/nginx: master
tcp 0 0 0.0.0.0:1990 0.0.0.0:* LISTEN 14688/uwsgi
tcp 0 0 0.0.0.0:808 0.0.0.0:* LISTEN 1083/nginx: master
[root@python DjangoWeb]# ps -ef|grep uwsgi
root 14688 1 0 16:29 ? 00:00:01 /usr/local/python3/bin/uwsgi --ini uwsgi.ini
root 14726 14688 0 16:32 ? 00:00:00 /usr/local/python3/bin/uwsgi --ini uwsgi.ini
root 14727 14688 0 16:32 ? 00:00:00 /usr/local/python3/bin/uwsgi --ini uwsgi.ini
root 14728 14688 0 16:32 ? 00:00:00 /usr/local/python3/bin/uwsgi --ini uwsgi.ini
root 14729 14688 0 16:32 ? 00:00:00 /usr/local/python3/bin/uwsgi --ini uwsgi.ini
root 14730 14688 0 16:32 ? 00:00:00 /usr/local/python3/bin/uwsgi --ini uwsgi.ini
root 14731 14688 0 16:32 ? 00:00:00 /usr/local/python3/bin/uwsgi --ini uwsgi.ini
root 14818 13963 0 17:11 pts/1 00:00:00 grep --color=auto uwsgi
#安装uwsgi
pip3 install uwsgi
#安装nginx
yum install -y nginx
-----------测试-----------------------------------
测试uWSGI是否正常,在DjangoWeb目录中创建一个测试用的Python文件uwsgi_test.py
def application(env, start_response):
start_response('200 OK',[('Content-Type', 'text/html')])
#return ['Hello world'] # Python2
return [b'Hello world'] # Python3
#在DjangoWeb项目路径下,基于HTTP协议运行uWSGI,如果uWSGI安装正常的话,可以在浏览器中访问9090端口,看到Hello world字样
#运行命令:
uwsgi --http 127.0.0.1:9090 --wsgi-file uwsgi_test.py
- uwsgi.ini配置文件
#uwsgi.ini
# uwsig使用配置文件启动
[uwsgi]
# 项目目录
chdir=/root/DjangoWeb
# 指定项目的application
module=DjangoWeb.wsgi:application
# 指定sock的文件路径
socket=/root/DjangoWeb/django_uwsgi.sock
# 进程个数
workers=5
pidfile=/root/DjangoWeb/uwsgi.pid
# 指定IP端口
http=0.0.0.0:1990
# 指定静态文件
#static-map=/static=/代码路径/项目/静态文件路径
static-map=/static=/root/DjangoWeb/static
# 启动uwsgi的用户名和用户组
uid=root
gid=root
# 启用主进程
master=true
# 自动移除unix Socket和pid文件当服务停止的时候
vacuum=true
# 序列化接受的内容,如果可能的话
thunder-lock=true
# 启用线程
enable-threads=true
# 设置自中断时间
harakiri=30
# 设置缓冲
post-buffering=4096
# 设置日志目录
daemonize=/root/DjangoWeb/django_uwsgi.log
- nginx.conf配置文件
#nginx.conf
upstream django{
#反向代理
server 192.168.127.78:1990;
}
server {
#uwsgi启动django服务
listen 8080;
server_name localhost;
index index.php index.html;
location / {
include uwsgi_params;
uwsgi_pass unix:/root/DjangoWeb/django_uwsgi.sock;
uwsgi_param UWSGI_CHDIR /root/DjangoWeb;
uwsgi_param UWSGI_SCRIPT django_wsgi;
access_log /root/DjangoWeb/access.log;
}
location /static {
alias /root/DjangoWeb/static;
}
}
server {
#配合upstream反向代理
listen 808;
server_name localhost;
index index.php index.html;
location / {
proxy_pass http://django;
access_log /root/DjangoWeb/access_nginx.log;
}
}
}
启动的端口及进程
[root@python DjangoWeb]# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 1083/nginx: master
tcp 0 0 0.0.0.0:1990 0.0.0.0:* LISTEN 14688/uwsgi
tcp 0 0 0.0.0.0:808 0.0.0.0:* LISTEN 1083/nginx: master
[root@python DjangoWeb]# ps -ef|grep uwsgi
root 14688 1 0 16:29 ? 00:00:01 /usr/local/python3/bin/uwsgi --ini uwsgi.ini
root 14726 14688 0 16:32 ? 00:00:00 /usr/local/python3/bin/uwsgi --ini uwsgi.ini
root 14727 14688 0 16:32 ? 00:00:00 /usr/local/python3/bin/uwsgi --ini uwsgi.ini
root 14728 14688 0 16:32 ? 00:00:00 /usr/local/python3/bin/uwsgi --ini uwsgi.ini
root 14729 14688 0 16:32 ? 00:00:00 /usr/local/python3/bin/uwsgi --ini uwsgi.ini
root 14730 14688 0 16:32 ? 00:00:00 /usr/local/python3/bin/uwsgi --ini uwsgi.ini
root 14731 14688 0 16:32 ? 00:00:00 /usr/local/python3/bin/uwsgi --ini uwsgi.ini
root 14818 13963 0 17:11 pts/1 00:00:00 grep --color=auto uwsgi
[root@python DjangoWeb]# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 1083/nginx: master
tcp 0 0 0.0.0.0:1990 0.0.0.0:* LISTEN 14688/uwsgi
tcp 0 0 0.0.0.0:808 0.0.0.0:* LISTEN 1083/nginx: master
[root@python DjangoWeb]# ps -ef|grep uwsgi
root 14688 1 0 16:29 ? 00:00:01 /usr/local/python3/bin/uwsgi --ini uwsgi.ini
root 14726 14688 0 16:32 ? 00:00:00 /usr/local/python3/bin/uwsgi --ini uwsgi.ini
root 14727 14688 0 16:32 ? 00:00:00 /usr/local/python3/bin/uwsgi --ini uwsgi.ini
root 14728 14688 0 16:32 ? 00:00:00 /usr/local/python3/bin/uwsgi --ini uwsgi.ini
root 14729 14688 0 16:32 ? 00:00:00 /usr/local/python3/bin/uwsgi --ini uwsgi.ini
root 14730 14688 0 16:32 ? 00:00:00 /usr/local/python3/bin/uwsgi --ini uwsgi.ini
root 14731 14688 0 16:32 ? 00:00:00 /usr/local/python3/bin/uwsgi --ini uwsgi.ini
root 14818 13963 0 17:11 pts/1 00:00:00 grep --color=auto uwsgi