Linux-CentOS6.9下Nginx+uWSGI+Python3+Django+MySQL开发环境搭建
sudo -s
安装Nginx-1.12.2
首先安装Nginx
https://blog.csdn.net/gocuber/article/details/80613503
安装安装MySQL-5.7.22
安装MySQL-5.7.22 如果不使用MySQL库可以跳过这步
https://blog.csdn.net/gocuber/article/details/80082356
安装Python-3.6.5
安装Python3并配置好pip
https://blog.csdn.net/gocuber/article/details/80011149
pip安装Django2.0.5
pip3 install Django==2.0.5
检查版本
/usr/local/python3/bin/django-admin.py --version
# 2.0.5
pip3 show Django # 查看详细信息
Django创建项目
新建python-dgango项目目录
mkdir -p /data0/python-dgango
cd /data0/python-dgango/
创建应用
/usr/local/python3/bin/django-admin.py startproject app1
生成如下结构
app1/
manage.py
app1/
__init__.py
settings.py # 项目的配置文件
ALLOWED_HOSTS = ['*'] # 可以写ip或域名 *是全部 具体参考官网文档
urls.py
wsgi.py # 项目的运行在 WSGI 兼容的Web服务器上的入口
具体参考Django官网教程1
https://docs.djangoproject.com/zh-hans/2.0/intro/tutorial01/
教程2
https://docs.djangoproject.com/zh-hans/2.0/intro/tutorial02/
启动服务
python3 manage.py runserver 0:8080
访问 http://127.0.0.1:8080 你将会看到一个“祝贺”页面
刚刚启动的是 Django 自带的用于开发的简易服务器,它是一个用纯 Python 写的轻量级的 Web 服务器。这个服务器内置在 Django 中是为了让你能快速的开发出想要的东西
我们继续安装uWSGI
使用pip安装 uWSGI 2.0.17
uWSGI https://pypi.org/project/uWSGI/
pip3 install uWSGI==2.0.17
查看uWSGI包安装信息
pip3 show uWSGI
Name: uWSGI
Version: 2.0.17
Summary: The uWSGI server
Home-page: https://uwsgi-docs.readthedocs.io/en/latest/
Author: Unbit
Author-email: info@unbit.it
License: GPL2
Location: /usr/local/python3/lib/python3.6/site-packages
Requires:
Required-by:
在你的python安装目录下的bin目录下会生成uwsgi脚本文件
检查版本
ll /usr/local/python3/bin/uwsgi
/usr/local/python3/bin/uwsgi --version
# 可以创建一个软连接 为了方便使用uwsgi
# ln -s /usr/local/python3.6/bin/uwsgi /usr/bin/uwsgi
测试uWSGI: 新建文件test.py,写入以下内容
uwsgi官网说明 http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html
cd /data0/python
vi test.py
# test.py
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"] # python3
#return ["Hello World"] # python2
然后在终端运行
/usr/local/python3/bin/uwsgi --http 0.0.0.0:9090 --wsgi-file test.py
访问 http://localhost:9090 输出 Hello World,成功
连接uWSGI与Django
官方说明 https://uwsgi-docs.readthedocs.io/en/latest/Nginx.html
方式一、直接启动
/usr/local/python3/bin/uwsgi --socket 0.0.0.0:9090 --chdir /data0/python-django/app1 --module app1.wsgi
方式二、新建uwsgi.ini 配置文件 更过配置详细说明参考官网
vi /etc/uwsgi9090.ini
[uwsgi]
# 使用动态端口
# socket = /tmp/uwsgi_vhosts.sock
# 手动指定
socket = 127.0.0.1:9090
chdir = /data0/python-django/app1
module = app1.wsgi
# 主进程
master = true
# 多站模式
#vhost = true
# 多站模式时不设置入口模块和文件
#no-stie = true
# 子进程数
#workers = 2
#reload-mercy = 10
# 退出、重启时清理文件
vacuum = true
#max-requests = 1000
#limit-as = 512
#buffer-sizi = 30000
# pid文件,用于脚本启动、停止该进程
pidfile = /var/run/uwsgi9090.pid
# uWSGI log日志文件
daemonize = /data0/logs/uwsgi9090.log
uwsgi 的启动 停止 reload
/usr/local/python3/bin/uwsgi --ini /etc/uwsgi9090.ini # 启动
/usr/local/python3/bin/uwsgi --stop /var/run/uwsgi9090.pid # 停止
/usr/local/python3/bin/uwsgi --reload /var/run/uwsgi9090.pid # reload
新建uwsgi9090文件 添加服务 并设置开机启动
vi /etc/init.d/uwsgi9090
#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for uwsgi webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f uwsgi defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add uwsgi'
### BEGIN INIT INFO
# Provides: uwsgi
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the uwsgi web server
# Description: starts uwsgi using start-stop-daemon
### END INIT INFO
NAME=uwsgi9090
DAEMON=/usr/local/python3/bin/uwsgi
CONFIGFILE=/etc/$NAME.ini
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
set -e
[ -x "$DAEMON" ] || exit 0
do_start() {
$DAEMON $CONFIGFILE || echo -n "uwsgi already running"
}
do_stop() {
$DAEMON --stop $PIDFILE || echo -n "uwsgi not running"
rm -f $PIDFILE
echo "$DAEMON STOPED."
}
do_reload() {
$DAEMON --reload $PIDFILE || echo -n "uwsgi can't reload"
}
do_status() {
ps aux|grep $DAEMON
}
case "$1" in
status)
echo -en "Status $NAME: \n"
do_status
;;
start)
echo -en "Starting $NAME: \n"
do_start
;;
stop)
echo -en "Stopping $NAME: \n"
do_stop
;;
reload|graceful)
echo -en "Reloading $NAME: \n"
do_reload
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|reload}" >&2
exit 3
;;
esac
exit 0
chmod 555 /etc/init.d/uwsgi9090 # 可执行权限
chkconfig --add uwsgi9090 # 添加服务
chkconfig --list uwsgi9090 # 查看服务
chkconfig uwsgi9090 on # 开机自启
service uwsgi9090 start # 启动服务
service uwsgi9090 stop # 停止服务
service uwsgi9090 reload # 平滑重启服务
service uwsgi9090 status # 查看状态
uWSGI 已经配置完毕
可以创建多个uWSGI
vi /etc/uwsgi9091.ini
vi /etc/init.d/uwsgi9091
连接uWSGI与Nginx
Nginx配置增加一个server
vim /usr/local/nginx/conf/nginx.conf
server
{
listen 80;
server_name dev.python.gocuber.com;
index index.html index.htm index;
charset utf-8;
client_max_body_size 75M; # adjust to taste
location /
{
include uwsgi_params;
# uwsgi_pass unix:///tmp/uwsgi_vhosts.sock; # 动态 .sock
uwsgi_pass 127.0.0.1:9090; # 与uwsgi中的一致
uwsgi_param UWSGI_SCRIPT app1.wsgi; # 入口文件,即wsgi.py相对于项目根目录的位置,. 相当于一层目录
uwsgi_param UWSGI_CHDIR /data0/python-django/app1; # 项目根目录
}
# access_log /data0/logs/wwwlogs.log wwwlogs; # nginx日志
}
访问 dev.python.gocuber.com OK
到此环境已经搭建完毕,具体Django的开发说明参考Django官网 https://docs.djangoproject.com/zh-hans/2.0/intro/tutorial01/