在docker中部署Nginx+uWSGI+Django环境
通过centos7镜像创建容器
[root@KayserCloud-ALY-03 ~]# docker search centos
[root@KayserCloud-ALY-03 ~]# docker pull docker.io/centos
[root@KayserCloud-ALY-03 ~]# docker run -t -p 80:9000 -d -i docker.io/centos /bin/bash
查看容器列表
[root@KayserCloud-ALY-03 ~]# docker ps
进入容器
[root@KayserCloud-ALY-03 ~]# docker exec -t -i 218a955e441f /bin/bash
安装Nginx
[root@218a955e441f /]# wget http://nginx.org/download/nginx-1.5.9.tar.gz
[root@218a955e441f /]# tar -xzvf nginx-1.5.9.tar.gz
[root@218a955e441f /]# cd nginx-1.5.9
[root@218a955e441f nginx-1.5.9]# ./configure —-user=nobody —-group=nobody —prefix=/usr/local/nginx --with-http_stub_status_module —-with-cc-opt=‘-o3’ —with-cpu-opt=opteron
[root@218a955e441f nginx-1.5.9]# make && make install
[root@218a955e441f nginx-1.5.9]# cd ..
安装mysql-python
[root@218a955e441f /]# yum install -y mysql-python
安装uwsgi
[root@218a955e441f /]# wget http://projects.unbit.it/downloads/uwsgi-2.0.4.tar.gz
[root@218a955e441f /]# tar -xzvf uwsgi-2.0.4.tar.gz
[root@218a955e441f /]# cd uwsgi-2.0.4
[root@218a955e441f uwsgi-2.0.4]# make
[root@218a955e441f uwsgi-2.0.4]# cp uwsgi /usr/bin
[root@218a955e441f uwsgi-2.0.4]# cd ..
安装django
[root@218a955e441f /]# wget http://www.djangoproject.com/m/releases/1.4/Django-1.4.9.tar.gz
[root@218a955e441f /]# tar -zxvf Django-1.4.9.tar.gz
[root@218a955e441f /]# cd Django-1.4.9
[root@218a955e441f Django-1.4.9]# python setup.py install
在需要创建django项目的目录下创建django项目
[root@218a955e441f optoolsWeb]# django-admin.py startproject optoolsWeb
[root@218a955e441f optoolsWeb]# mkdir static
[root@218a955e441f optoolsWeb]# ls
manage.py optoolsWeb static
创建 optoolsWeb_wsgi.py文件,并编辑如下内容:
[root@218a955e441f optoolsWeb]# vi optoolsWeb_wsgi.py
#coding:utf-8
import os
import sys
reload(sys)
sys.setdefaultencoding('utf8')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "optoolsWeb.settings”) #根据自己目录设置
from django.core.handlers.wsgi import WSGIHandler
application = WSGIHandler()
[root@218a955e441f optoolsWeb]# ls
manage.py optoolsWeb optoolsWeb_wsgi.py static
配置nginx
[root@218a955e441f /]# vi /usr/local/nginx/conf/nginx.conf
增加如下内容:
server {
listen 80;
server_name www.domain.com; #域名或者主机公网ip
location / {
uwsgi_pass 127.0.0.1:9000; #该端口用于内部通信
include /usr/local/nginx/conf/uwsgi_params;
uwsgi_param UWSGI_CHDIR /data/www/optoolsWeb; #实际项目目录
uwsgi_param UWSGI_SCRIPT optoolsWeb_wsgi;
access_log /data/logs/nginx/access.log; #按照自己的实际目录填写
error_log /data/logs/nginx/error.log; #按照自己的实际目录填写
}
location ~* ^.+\.(mpg|avi|mp3|swf|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|txt|tar|mid|midi|wav|rtf|mpeg)$ {
root /data/www/optoolsWeb/static; #按照自己的实际目录填写
access_log /data/logs/nginx/access.log; #按照自己的实际目录填写
}
}
配置uwsgi
[root@218a955e441f /]# vi /usr/local/nginx/conf/uwsgi.ini
[uwsgi]
socket=127.0.0.1:9000 #内部与django通信端口
master = true
pidfile = /usr/local/nginx/uwsgi.pid
processes = 8 #启动进程个数,按照实际需要设置
chdir = /data/www/optoolsWeb #实际项目目录
pythonpath = /data/www
profiler=true
memory-report=true
enable-threads = true
logdate=true
limit-as=6048
daemonize=/data/logs/django.log #log目录,根据实际填写
启动uwsgi
[root@218a955e441f /]# /usr/bin/uwsgi --ini /usr/local/nginx/conf/uwsgi.ini
启动nginx
[root@218a955e441f /]# /usr/local/nginx/sbin/nginx
[root@218a955e441f /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 Jul08 ? 00:00:00 /bin/bash
root 629 0 0 Jul14 ? 00:00:00 /bin/bash
root 1026 0 0 Jul14 ? 00:00:00 /bin/bash
root 1124 0 0 Jul14 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 1126 1124 0 Jul14 ? 00:00:00 nginx: worker process
root 1128 0 0 Jul14 ? 00:00:04 uwsgi --ini /usr/local/nginx/conf/uwsgi.ini
root 1129 1128 0 Jul14 ? 00:00:00 uwsgi --ini /usr/local/nginx/conf/uwsgi.ini
root 1130 1128 0 Jul14 ? 00:00:00 uwsgi --ini /usr/local/nginx/conf/uwsgi.ini
root 1131 1128 0 Jul14 ? 00:00:00 uwsgi --ini /usr/local/nginx/conf/uwsgi.ini
root 1132 1128 0 Jul14 ? 00:00:00 uwsgi --ini /usr/local/nginx/conf/uwsgi.ini
root 1133 1128 0 Jul14 ? 00:00:00 uwsgi --ini /usr/local/nginx/conf/uwsgi.ini
root 1134 1128 0 Jul14 ? 00:00:00 uwsgi --ini /usr/local/nginx/conf/uwsgi.ini
root 1135 1128 0 Jul14 ? 00:00:00 uwsgi --ini /usr/local/nginx/conf/uwsgi.ini
root 1136 1128 0 Jul14 ? 00:00:00 uwsgi --ini /usr/local/nginx/conf/uwsgi.ini
浏览器输入:www.domain.com:9000或者主机公网ip:9000
在docker中部署Nginx+uWSGI+Django环境【补充】
1、启动或者重启服务可以写成脚本
# restartapp.sh
#!/bin/bash
ps -ef|grep nginx|grep -v grep|awk '{print "kill -9 " $2}'|sh
ps -ef|grep uwsgi|grep -v grep|awk '{print "kill -9 " $2}'|sh
/usr/local/nginx/sbin/nginx
uwsgi --ini /usr/local/nginx/conf/uwsgi.ini
2、若多次使用docker exec -t -i 命令进入容器会产生多个进程残留:
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 Jul08 ? 00:00:00 /bin/bash
root 629 0 0 Jul14 ? 00:00:00 /bin/bash
root 1026 0 0 Jul14 ? 00:00:00 /bin/bash
可以编写脚本来清理
# clearbash.sh
#!/bin/bash
psnum=`ps -ef|grep /bin/bash|grep -v grep|wc -l`
killnum=`expr $psnum - 1`
ps -ef|grep /bin/bash|grep -v grep|awk '{print "kill -9 " $2}'|sed -n '1p;'"$killnum"'p'|sh
3、启动服务时报错:
2018/07/14 17:17:12 [emerg] 1105#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2018/07/14 17:17:12 [emerg] 1105#0: still could not bind()
解决方法:
* netstat -anp|grep 80|awk '{print $7}'|tr -cd '[0-9]'|xargs kill -9 #批量删除占用端口的进程
* 重启服务:sh restartapp.sh
至此,整个Nginx+uWSGI+Django环境部署完成。
最后,docker安装可以参考: