ngnix+uwsgi+django+python+mysql环境部署

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ngnix+uwsgi+django+python+mysql环境部署相关的知识,希望对你有一定的参考价值。

django
一个python的web框架
flask和django

uwsgi
一个Python的容器

nginx
一个web服务器 可以做web容器的前端

client--->nginx(80)--->uwsgi(开端口5000)--->web页面


virtualenv  virtualenvwrapper
    python开发虚拟环境

安装python3:
    下载python3.6
        去官网下载
        #xz -d  压缩包.xz
    准备环境:      
        #yum install zlib* openssl* readline* sqlite* -y

    安装python3
        # cd Python-3.6.4
        # ./configure --prefix=/usr/local/python3.6  && make && make install 
        # rm -rf  /usr/bin/python
        # ln -s /usr/local/python3.6/bin/python3  /usr/bin/python

     处理yum(python升级之后yum就不能用了,他默认用的2.7)
        # vim /usr/bin/yum    把解释器改成2.7
        # !/usr/bin/python2.7

    给pip做软连接:    
    # ln -s /usr/local/python3.6/bin/pip3  /usr/bin/pip

    # vim ~/.bash_profile 
        PATH=$PATH:$HOME/bin:/usr/local/python3.6/bin/
     #source ~/.bash_profile

安装django        
    # pip install django==1.10

创建django项目:firstsite
    #cd ~
    #django-admin.py startproject firstsite
    #cd ~/firstsite

同步数据库:
    #./manage.py migrate
    #./manage.py createsuperuser

设置静态文件目录:
    # vim firstsite/settings.py   //先找到STATIC关键字,追加下面一行  
    STATIC_ROOT = os.path.join(BASE_DIR, "static/") 
    #./manage.py collectstatic

测试django是否可用:    
    #./manage.py runserver 0.0.0.0:443
    浏览器访问:   
    http://xiangmu.buy360.xyz:443

    浏览器访问:    
    http://xiangmu.buy360.xyz:443/admin

安装uwsgi:    
    #pip install uwsgi

测试uwsgi:
    # uwsgi --http :443 --chdir /root/firstsite -w firstsite.wsgi
     --http  :端口
     --chdri  项目目录
     -w   wsgi模块

创建uwsgi配置文件:
#mkdir -p /etc/uwsgi/sites
# vim /etc/uwsgi/sites/firstsite.ini 
[uwsgi]
project = firstsite      //项目名称
username = root      //运行用户
base = /%(username)/         //项目目录的上级目录
chdir = %(base)/%(project)         //项目目录
module = %(project).wsgi:application     
master = true
processes = 5
uid = %(username)
socket = /run/uwsgi/%(project).sock
chown-socket = %(username):nginx
chmod-socket = 660
vacuum = true

创建uwsgi启动配置文件:
# vim /etc/systemd/system/uwsgi.service
[Unit]
Description=uWSGI Emperor service

[Service]
ExecStartPre=/usr/bin/bash -c ‘mkdir -p /run/uwsgi; chown root:nginx /run/uwsgi‘
ExecStart=/usr/local/python3.6/bin/uwsgi --emperor /etc/uwsgi/sites
Restart=always
KillSignal=SIGQUIT
Type=notify
NotifyAccess=all

[Install]
WantedBy=multi-user.target

下面两步不做会启动服务不成功:
#mkdir /root/uwsgi
#ln -s /usr/local/python3.6/bin/uwsgi /root/uwsgi/uwsgi

安装nginx:
#yum install nginx

配置nginx:
# vim /etc/nginx/nginx.conf 添加如下虚拟主机配置
server {
    listen 80;
    server_name firstsite.com www.youngfit.cn;

    location = favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /root/firstsite;
    }

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/run/uwsgi/firstsite.sock;
    }
}

测试Nginx配置文件语法正确性:
    # nginx -t

设置权限:
# usermod -a -G root nginx
# chmod 710 /root

启动nginx和uwsgi服务:
    #systemctl start nginx
    #systemctl start uwsgi

测试:
浏览器地址栏:http://xiangmu.buy360.xyz

以上是关于ngnix+uwsgi+django+python+mysql环境部署的主要内容,如果未能解决你的问题,请参考以下文章

Django 和 Python + uWSGI

python django uwsgi nginx安装

Flask网站搭建:Ubuntu 14.04 sever (阿里云64bit) 部署 flask + uWSGI + Ngnix

python--Django+uwsgi+nginx

python3 + Django + uwsgi + nginx 配置部署笔记

Install Python+Django+Nginx+UWSGI