CentOS6 部署Django+Nginx+uwsgi

CentOS安装Django + Nginx + uwsgi

软件环境

  1. MySQL
  2. CentOS6
  3. Nginx

安装

第一步,安装Python3

下载python3.5.6源码并编译安装

wget https://www.python.org/ftp/python/3.5.6/Python-3.5.6.tgz
tar -zxvf Python-3.5.6.tgz
cd Python-3.5.6
./configure --prefix=/usr/local/python3
make && make altinstall 

第二步,创建django运行用户

useradd jarvis
passwd jarvis
su - jarvis

第三步,pip安装virtualenv和virtualwrapper

## 修改pip源,使用国内源,安装迅速
su - jarvis
cd ~
mkdir -p .pip
##创建pip.conf,写入以下配置
[global]
timeout = 6000 
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host = pypi.tuna.tsinghua.edu.cn
###
cd /usr/local/python3/bin
./pip3 install virtualenv virtualenvwrapper

## 编辑.bashrc
## 指定要用的python解释器版本
export VIRTUALENVWRAPPER_PYTHON=/usr/local/python3/bin/python3
## 指定virtualenv
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/python3/bin/virtualenv
export WORKON_HOME=~/Env
export PROJECT_HOME=$HOME/jarvis
source /usr/local/python3/bin/virtualenvwrapper.sh

第四步, 安装Django uwsgi

mkvirtualenv jarvis
workon jarvis
pip install Django==2.0.6
pip install uwsgi

第五步,创建Django 项目

django-admin.py startproject jarvis
cd jarvis
## 测试是否已经创建成功
python manage.py migrate
python manage.py runserver 0.0.0.0:8000
## 如果出现禁止访问类字样,编辑settings.py,修改如下项:
#由
ALLOWED_HOSTS = []
#改为
ALLOWED_HOSTS = ['*']

在浏览器输入网址,看能否访问

第六步,使用uwsgi运行Django

uwsgi --http :8000 --home /home/jarvis/Env/jarvis --chdir /home/jarvis/jarvis -w jarvis.wsgi

再次在浏览器中进行访问,查看是否能够访问。
如果可以访问,说明uwsgi安装成功,下面使用配置文件的方式启动uwsgi

mkdir -p /home/jarvis/conf
vim uwsgi.ini
###写入以下内容
[uwsgi]
project = jarvis
base = /home/jarvis

chdir = %(base)/%(project)
home = %(base)/Env/%(project)
module = %(project).wsgi:application

master = true
processes = 5
socket = %(base)/%(project)/%(project).sock
chmod-socket = 666
vacuum = true

### 使用以下命令启动uwsgi
nohup uwsgi --emperor /home/jarvis/conf/jarvis.ini &

第七步, 配置Nginx,和uwsgi使用socket通信

修改nginx.conf

worker_processes  1;
### 使得nginx对uwsgi socket有访问权限,也可以改为user jarvis;
user root;
events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  10.0.0.1;

        location / {
        include uwsgi_params;
        uwsgi_pass unix:/home/jarvis/jarvis/jarvis.sock;
        }

    location /static/ {
        root /home/jarvis/static;
    }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }
}

在浏览器访问ip地址,看是否已经成功。后续工作只需要把django工程文件放到/home/jarvis/jarvis。

你可能感兴趣的:(CentOS6 部署Django+Nginx+uwsgi)