centos7下将本地django项目通过nginx+uwsgi部署上线

安装思路整理

  1. centos系统自带python2,但是它不是我们需要的版本。(一定不要移除系统自带的python2,yum依赖的就是python2)安装python3,并且设置软连接
  2. 安装nginx、uwsgi、virtualenv等项目依赖
  3. 关联uwsgi、nginx文件
  4. 踩过的坑(我还记得的)

一、编译安装python3(我自己的安装方法我忘记了,但是网上有很详细的。我只是贴出一个我觉得讲的很好的)

#为centos系统增加编译功能:
yum -y install gcc gcc-c++

#防止编译安装python3出现各种异常:
yum install wget openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel

#下载python3安装包:
cd /home/<username>/Downloads/ #<username>用自己的用户名代替
wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz

#解压:
tar -zxvf Python-3.6.3.tgz

#配置,将python3安装到/usr/local/python3/路径下:
cd Python-3.6.3
./configure --prefix=/usr/local/python3

#编译安装:
make -j2
make install -j2

#建立软链接,方便在终端中直接使用python3和pip3命令:
ln -s /usr/bin/python /usr/bin/python2   #将原来的版本移动到python2
ln -s /usr/local/python3/bin/python3.6 /usr/bin/python
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip  #如果之前有pip就和python2转移方法一样

#安装成功性测试,显示相应版本就表示成功了:
python -V
pip -V

二、安装虚拟环境,为项目安装所有依赖

#更新pip3至最新版本
pip install --upgrade pip

#安装virtualenv
pip install virtualenv

ln -s /usr/local/python3/lib/python3.6/site-packages/virtualenv.py /usr/bin/virtualenv

#进入你自己想要进去的目录下
cd /XXX
mkdir XXX #创建一个存放的目录

cd XXX

virtualenv -p /usr/bin/python3 venv  #-p是为了指定python版本

#激活虚拟环境
source /你的虚拟环境路径/bin/activate

#退出虚拟环境
deactivate


pip install uwsgi 

#添加nginx存储库
yum install epel-release
#安装nginx
yum install nginx

pip install -r requirements.txt安装  (pip freeze >requirements.txt收集依赖)

三、测试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

当浏览器访问8000端口时, 可看到hello world

curl http://127.0.0.1:8000

安装成功就开始项目的配置,在项目目录下添加wsgi.ini。下面是我的文件,具体参数什么作用可以去看官网

[uwsgi]
#the local unix socket file than commnuincate to Nginx
socket = :8001
# the base directory (full path)
chdir = /XXX  #项目目录

# the virtualenv (full path)
home = /XXX  #虚拟环境目录

# master
master = true

# Django's wsgi file
wsgi-file = XXX/wsgi.py  #这个是项目自带的
# maximum number of worker processes
processes = 4
#thread numbers startched in each worker process
threads = 2
 
module = 你的项目名字.wsgi:application

deamonize = /XXX/log/uwsgi.log
logto = /XXX/log/uwsgi.log
disable-logging =  true

# clear environment on exit
vacuum   = true

#Set the internal cache size for uwsgi package parsing. The default is 4k.
buffer-size = 65536

post-buffering = 32768

#Unix socket is a file, so it will be restricted by the Unix system.
chmod-socket = 755


stats=%(chdir)/uwsgi/uwsgi.status

pidfile=%(chdir)/uwsgi/uwsgi.pid

四、配置nginx

主配置文件/etc/nginx/nginx.conf ,我为了方便写就直接贴在这里了。但是最好是创建一个单独的文件

upstream django {
        # server unix:///path/to/your/mysite/mysite.sock; # for a file socket
        server 127.0.0.1:80; # for a web port socket (we'll use this first)
     }

#OW/// 
     server {
         listen       80;
         server_name  www.XXX.com XXX.com;
         #server_name  www.XXX.com
         root         /usr/share/nginx/html;
         client_max_body_size 75M;  #上传文件大小限制
         charset utf-8;
         # Load configuration files for the default server block.
         include /etc/nginx/default.d/*.conf;
 
         location / {
               uwsgi_pass  django;
               proxy_set_header Host $host;
               proxy_set_header X-Forwarded-Host $server_name;
               uwsgi_read_timeout 2;
               root html;
               index index.html index.htm;
               include /etc/nginx/uwsgi_params;
               #add_header Cache-Control public;
               #设置不缓存
               add_header Cache-Control no-cache;
               add_header Pragma no-cache;
               add_header Expires 0;
         }
         location /static{
              # expires 30d;
               add_header Cache-Control no-store;
               autoindex on;
               alias /var/XXX/static;
         }
         location /media{
              alias /var/XXX/media;
         }
 
         error_page 404 /404.html;
             location = /40x.html {
         }
 
         error_page 500 502 503 504 /50x.html;
             location = /50x.html {
         }
     }

                     

注意

 include /usr/share/nginx/modules/*.conf;   #添加这两行
  include /etc/nginx/sites-enabled/*;

将setting.py文件中

#DEBUG = True
DEBUG = False
ALLOWED_HOSTS = [ ] #修改前
ALLOWED_HOSTS = ['*'] #修改后或者改成你的域名和外网ip更为严谨

命令汇总

source /usr/XXX/activate  激活虚拟环境
systemctl restart nginx 运行nginx服务
netstat -lnp|grep 80 查看是否再成功运行
uwsgi -d --init  /var/XXX/wsgi.ini
ps aux | grep uwsgi  查看是否再成功运行
netstat -lntp监听端口
 vim /etc/nginx/nginx.conf配置文件 
cat /var/log/nginx/error.log   查看错误日志 
netstat -apn|grep 80
kill -9 <pid>

还有一些细节我有些想不起了,让我想起来了我再回来添加吧

五、踩过的坑

[error] 1214#0: *64 upstream prematurely closed connection while reading response header from upstream, client:

#[error] 1214#0: *64 upstream prematurely closed connection while reading response header from upstream, client:
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 256k;         #以下四个参数已加大,如果设置太小也会出现timeout 504
fastcgi_buffers 16 256k;
fastcgi_busy_buffers_size 512k;
fastcgi_temp_file_write_size 512k;

403 forbiddenYou don't have permission to access / on this server.

为项目目录添加权限。

参考资料:

1、  https://uwsgi-docs-zh.readthedocs.io/zh_CN/latest/tutorials/Django_and_nginx.html官网

2、 https://www.zhihu.com/question/22850801/answer/656762026  程序员向东的那个回答

3、https://zhuanlan.zhihu.com/p/49200125

如果有不对的地方,欢迎在评论指出

猜你喜欢

转载自blog.csdn.net/hard_days/article/details/96998037