linux+nginx+mysql+python+uwsg布署

ROOT权限执行

 

安装软件管理包和可能使用的依赖

yum update -y
yum -y groupinstall "Development tools"
yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel psmisc

 

下载Pyhton3到/usr/local 目录 ,安装

cd /usr/local
下载 wget https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tgz
解压
tar -zxvf Python-3.6.6.tgz

编译及安装 cd Python-3.6.6 ./configure --prefix=/usr/local/python3 make make install

建立软链接方便使用命令
ln -s /usr/local/python3/bin/python3.6 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3.6 /usr/bin/pip3

安装virtualenv
pip3 install virtualenv
ln -s /usr/local/python3/bin/virtualenv /usr/bin/virtualenv

建立项目目录
mkdir -p /data/env  # 存放虚拟环境
mkdir -p /data/wwwroot  # 存放项目

安装虚拟环境
virtualenv --python=/usr/bin/python3 pyweb
启动虚拟环境
进入/data/env/pyweb/bin 
source activate
pip3 install django
pip3 install uwsgi

给uwsgi建立软链接
ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi

 

测试uwsgi

创建一个名为 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


运行uWSGI:
uwsgi --http :8000 --wsgi-file test.py

浏览器访问:127.0.0.1:8000
如成功则表示:the web client <-> uWSGI <-> Python

将项目移动至/data/wwwroot目录下
测试Django项目
uwsgi --http :8000 --module mysite.wsgi


 

安装nginx,以nginx-1.13.7为例

cd /home/
下载
wget http://nginx.org/download/nginx-1.13.7.tar.gz
解包 tar -zxvf nginx-1.13.7.tar.gz
编译安装 ./configure --prefix=/usr/local/nginx make make install

 

配置uwsig.ini(文件名随便取,后缀ini)

[uwsgi]
chdir=/data/wwwroot/mysite
home=/data/env/pyweb
module=mysite.wsgi:application

master=True
processes=4
threads
harakiri=20
max-requests=5000
socket=127.0.0.1:8001
uid=1000
gid=2000

pidfile=/data/wwwroot/mysite/master.pid
daemonize=/data/wwwroot/mysite/mysite.log
vacuum=True

 

uwsig部分参数参考:

uwsgi --chdir=/path/to/your/project \
    --module=mysite.wsgi:application \
    --env DJANGO_SETTINGS_MODULE=mysite.settings \
    --master --pidfile=/tmp/project-master.pid \
    --socket=127.0.0.1:49152 \      # can also be a file
    --processes=5 \                 # number of worker processes
    --threads=2                     # number of worker threads
    --uid=1000 --gid=2000 \         # if root, uwsgi can drop privileges
    --harakiri=20 \                 # respawn processes taking more than 20 seconds
    --max-requests=5000 \           # respawn processes after serving 5000 requests
    --vacuum \                      # clear environment on exit
    --home=/path/to/virtual/env \   # optional path to a virtualenv
    --daemonize=/var/log/uwsgi/yourproject.log      # background the process

 

配置ngixn

/usr/local/nginx/conf/中先备份一下nginx.conf文件
cp nginx.conf nginx.conf.bak

编辑nginx.conf
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    server {
        listen 80;
        server_name  127.0.0.1:80; #改为自己的域名,没域名修改为127.0.0.1:80
        charset utf-8;
        location / {
           include uwsgi_params;
           uwsgi_pass 127.0.0.1:8001;  #端口要和uwsgi里配置的一样
           uwsgi_param UWSGI_SCRIPT mysite.wsgi;  #wsgi.py所在的目录名+.wsgi
           uwsgi_param UWSGI_CHDIR /data/wwwroot/mysite/; #项目路径

        }
        location /static {
        alias /data/wwwroot/mysite/static/; #静态资源路径
        }
    }
}


进入/usr/local/nginx/sbin/目录
./nginx -t # 命令先检查配置文件是否有错
./nginx # 启动

./nginx -s reload 重启

 

开放对应的端口

防火墙配置文件:
  /etc/sysconfig/iptables

查看防火墙状态
    service iptables status
 
停止防火墙
    service iptables stop
 
启动防火墙
    service iptables start
 
重启防火墙
    service iptables restart
 
永久关闭防火墙
    chkconfig iptables off
 
永久关闭后重启
    chkconfig iptables on

清空防火墙规则
    iptables –F 
    service iptables stop

 

静态文件没有生效的问题

需要将静态文件存放在nginx.conf配置里对应位置

# settings.py
STATIC_ROOT  = os.path.join(BASE_DIR, 'static')#指定样式收集目录

运行命令:
python manage.py collectstatic

 

posted @ 2019-02-22 20:47  李小样  阅读(662)  评论(0编辑  收藏  举报