前面的章节中我们使用 python manage.py runserver 来运行服务器。这只适用测试环境中使用。
正式发布的服务,我们需要一个可以稳定而持续的服务器,比如apache, Nginx, lighttpd等,本文将以 Nginx 为例。
安装基础开发包
Centos 下安装步骤如下:
yum groupinstall "Development tools"
yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel
CentOS 自带 Python2.7.5,但我们可以再安装Python3.6.8:
更新python千万不要把老版本的删除!新老版本是可以共存的,很多基本的命令、软件包都要依赖预装的老版本python的,比如yum。
cd /opt/software
wget http://python.org/ftp/python/3.6.8/Python-3.6.8.tar.xz
xz -d Python-3.6.8.tar.xz
tar -xvf Python-3.6.8.tar
cd Python-3.6.8
./configure --prefix=/usr/local
make && make altinstall
验证
安装Python包管理
easy_install 包 https://pypi.python.org/pypi/distribute
安装步骤:
pip3 install distribute
会报错
AttributeError: module 'importlib._bootstrap' has no attribute 'SourceFileLoader'
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-_ot70_ck/distribute/
因为目前只支持python3.3及以下
pip 包: https://pypi.python.org/pypi/pip
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3.6 get-pip.py
安装 pip 的好处是可以用 pip list、pip uninstall 管理 Python 包, easy_install 没有这个功能,只有 uninstall。
pip如果报错:安装第三方库,超时报错--Read timed out.
pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.
执行:
pip3 --default-timeout=100 install -U Pillow
安装 uwsgi
uwsgi:https://pypi.python.org/pypi/uWSGI
uwsgi 参数详解:http://uwsgi-docs.readthedocs.org/en/latest/Options.html
安装依赖 yum install python-devel
pip3 install uwsgi
uwsgi --version # 查看 uwsgi 版本
如果报错:plugins/python/uwsgi_python.h:2:20: fatal error: Python.h: No such file or directory
执行:
yum install python-devel
测试 uwsgi 是否正常:
新建 test.py 文件,内容如下:
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"]
然后在终端运行:
uwsgi --http :8001 --wsgi-file test.py
在浏览器内输入:http://127.0.0.1:8001,查看是否有"Hello World"输出,若没有输出,请检查你的安装过程。
安装 Django
pip3 install django
测试 django 是否正常,运行:
django-admin.py startproject demosite
cd demosite
python3.6 manage.py runserver 0.0.0.0:8002
在浏览器内输入:http://127.0.0.1:8002,检查django是否运行正常。
网页400
报错:django_Invalid HTTP_HOST header: '192.168.163.140'. You may need to add '192.168.163.140' to ALLOWED
将项目中的setting.py的ALLOWED_HOSTS=[]改为:
ALLOWED_HOSTS = ["*"]
参考:https://blog.youkuaiyun.com/zuoshenglo/article/details/78404367
安装 Nginx
安装命令如下:
cd /opt/software
wget http://nginx.org/download/nginx-1.5.6.tar.gz
tar zvxf nginx-1.5.6.tar.gz(不要直接解压到/usr/local下,让它去安装)
cd nginx-1.5.6
此时目录/opt/software/nginx-1.5.6
./configure --prefix=/usr/local/nginx-1.5.6 \
--with-http_stub_status_module \
--with-http_gzip_static_module
make && make install
将在/opt/local中创建出nginx-1.5.6,内含conf和sbin
你可以阅读 Nginx 安装配置 了解更多内容。
uwsgi 配置
uwsgi支持ini、xml等多种配置方式,本文以 ini 为例, 在/etc/目录下新建uwsgi8080.ini,添加如下配置:
[uwsgi]
socket = 127.0.0.1:8080
#主进程
master = true
#vhost = true #多站模式
#no-site = true #多站模式时不设置入口模块和文件
#子进程数
workers = 2
reload-mercy = 10
#退出、重启时清理文件
vacuum = true
max-requests = 1000
limit-as = 512
buffer-size = 30000
#pid文件,用于下面的脚本启动、停止该进程
pidfile = /var/run/uwsgi8080.pid
#daemonize = /website/uwsgi8080.log
daemonize = /root/logs/info.log
chdir = /opt/workspace/CountlyAnalysis/
wsgi-file = CountlyAnalysis/wsgi.py
#允许开启新线程
enable-threads = true
Nginx 配置
找到nginx的安装目录(如:/usr/local/nginx/),打开conf/nginx.conf文件,修改server配置:
server {
listen 80;
server_name localhost;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8080; #必须和uwsgi中的设置一致
index index.html index.htm;
client_max_body_size 35m;
}
}
你可以阅读 Nginx 安装配置 了解更多内容。
设置完成后,在终端运行:
uwsgi --ini /etc/uwsgi8080.ini &
/usr/local/nginx/sbin/nginx
在浏览器输入:http://127.0.0.1,你就可以看到 django 的 "It work" 了。
获取所有命令:uwsgi -h
开启uwsgi进程: uwsgi --ini /etc/uwsgi8080.ini
停止uwsgi进程: uwsgi --stop /etc/uwsgi8080.ini
编写启动脚本
创建日志目录 mkdir ~/logs
监控启动日志 tailf ~/logs/info.log