CentOS7用uwsgi+Nginx部署Django2.0超详细步骤

首先: /home/mysite为我的django项目,/home/mysite_env为虚拟环境,需要:Django2.0,uwsgi, Nignx。其实无论你是Django2或者以上,python3.7之类的都可以使用本教程。

第一:安装python3.5.2
环境:

yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make

wget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz

编译安装:

./configure prefix=/usr/local/python3
make && make install

ln -s /usr/local/python3/bin/python3 /usr/bin/python
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip

将下面两个的第一行内容后面都加上“2.7”

vim /usr/bin/yum
vim /usr/libexec/urlgrabber-ext-down

第二:安装虚拟环境
虚拟环境:
pip install virtualenv
安装完成后,执行
cd /home/python
virtualenv venv
会报: bash: virtualenv: command not found
此时,可通过find命令查找到virtualenv安装目录
find / -name virtualenv
得出在:/usr/local/python3/bin/virtualenv目录下
再执行

/usr/local/python3/bin/virtualenv mysite_env

即创建了一个虚拟环境mysite_env

cd mysite_env

source bin/activate

pip install django==2.0 

第三步:安装uwsgi

pip install uwsgi

验证是否能用uwsgi访问:

uwsgi --http 127.0.0.1:8001 --chdir /home/mysite --home /home/mysite_env --module mysite.wsgi:application

uwsgi.ini配置如下:

[uwsgi]
chdir = /home/mysite # 项目
home = /home/mysite_env # 项目虚拟环境
module = mysite.wsgi:application

master = True
processes = 10
harakiri = 60
max-requests = 5000

http = 127.0.0.1:8001 # 指定http端口

vacuum = True
daemonize = /home/mysite/mysite_uwsgi/uwsgi.log
pidfile = /home/mysite/mysite_uwsgi/uwsgi.pid

ini:
正常关闭uwsgi进程:uwsgi --stop /home/mysite_uwsgi/master.pid
强制关闭全部uwsgi进程:ps -aux | grep uwsgi |awk '{print $2}'|xargs kill -9

第四步:静态文件
写好django项目打包上传到服务器,修改setting.py
ALLOWED_HOSTS = [’*’,] #允许所有ip访问django项目
DEBUG = False  #关闭DEBUG模式
STATIC_ROOT=’/home/mysite/static/’ #配置静态文件存放的目录
把django项目中的静态文件分离出来

python manage.py collectstatic

最后将“STATIC_ROOT=’/home/mysite/static/’”从settins删除否则启动会报错。

第五步:安装nginx

wget http://nginx.org/download/nginx-1.14.2.tar.gz
tar -zxvf nginx-1.14.2.tar.gz
cd nginx-1.14.2.tar.gz/
./configure --prefix=/usr/local/nginx
make && make install
cd /usr/local/nginx/sbin/

启动:
./nginx
以下是停止,重载等常用命令:
./nginx -s stop
./nginx -s quit
./nginx -s reload
(注意:如果报端口80被占用:1、yum install net-tools2、ps aux|grep nginx)

修改nginx配置:

 server {
     listen       80;
     server_name  localhost;
    location / {
     root /home/CMS;
     include uwsgi_params;
     proxy_pass http://127.0.0.1:8000;
     uwsgi_read_timeout 30;
        }
location /static {
     autoindex on;
     alias /home/mysite/static/;  #静态文件访问路径
          }

启动django

uwsgi --ini uwsgi.ini

(lsof -i :[:8001]可以找出占用8001端口的进程)
重载nginx:

./nginx -s reload

你可能感兴趣的:(Django)