centos上部署django项目

centos上部署django项目

下载python3

yum -y groupinstall "Development tools"
yum -y install openssl-devel sqlite-devel bzip2-devel ncurses-devel gdbm-devel readline-devel tcl-devel tk-devel xz-devel zlib-devel db4-devel libpcap-devel
cd /usr/local/src
wget https://www.python.org/ftp/python/3.5.1/Python-3.5.1.tgz
tar -zxf /usr/local/src/Python-3.5.1.tgz && cd Python-3.5.1
./configure --prefix=/opt/python3.5 --enable-shared
make && make install
ln -s /opt/python3.5/bin/python3 /usr/bin/python3
echo "/opt/python3.5/lib" > /etc/ld.so.conf.d/python3.5.conf
ldconfig
python3 --version
/opt/python3.5/bin/pip3 install --upgrade pip
ln -s /opt/python3.5/bin/pip /usr/bin/pip3
wget https://bootstrap.pypa.io/ez_setup.py -O - | python3
ln -s /opt/python3.5/bin/easy_install /usr/bin/easy_instal

安装django

pip3 install django

下载uwsgi

pip3 install uwsgi

注: 在项目settings.py路径下同级目录创建uwsgi.ini

[uwsgi]
#http=0.0.0.0:80
#用于与nginx通信的端口
socket=:8000
#项目路径
chdir=/root/cslg/ddhostel
#chmod-socket=664
master=true
processes=8
threads=4
module=ddhostel.wsgi
#wsgi-file=uwsgi_test.py
#stats=127.0.0.1:9000

下载nginx

rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum install -y nginx
cd /etc/nginx
vim nginx.conf

编辑nginx.conf

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
    server {
        listen 80; #暴露给外部访问的端口
        #域名或者主机名
        server_name stu.ncct.link;
            charset utf-8;
        location / {
            include uwsgi_params;
            uwsgi_pass 0.0.0.0:8000; #外部访问80就转发到内部8000
        }
        location /static/ {
            alias /root/cslg/ddhostel/static; #项目静态路径设置
        }
    }


        #error_page 404 /404.html;
        #    location = /40x.html {
        #}}

        #error_page 500 502 503 504 /50x.html;
        #    location = /50x.html {
        #}

# Settings for a TLS enabled server.
#
    #ssl证书认证,开启
    server {
        listen       443 ssl;# ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
        server_name  stu.ncct.link;
        root         /root/cslg/ddhostel;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_certificate "/etc/nginx/cert/1_stu.ncct.link_bundle.crt";
        ssl_certificate_key "/etc/nginx/cert/2_stu.ncct.link.key";
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout  5m;
        ssl_ciphers HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers on;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
	    proxy_pass http://stu.ncct.link;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;

        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
    #server {
    #    listen                  80;
    #    server_name             stu.ncct.link;  #http访问入口
    #    location / {
    #        rewrite ^ https://$http_host$request_uri? permanent; #强制跳转到HTTPS上
    #    }
    #}

}

开启服务

进入uwsgi.ini的路径下

uwsgi --ini uwsgi.ini    》启动uwsgi
nginx -s reload          》启动nginx

服务器关闭时,uwsgi也不运行的解决方法:

uwsgi -d --ini uwsgi.ini 》后台启动uwsgi

猜你喜欢

转载自blog.csdn.net/qq_41860162/article/details/89231652
今日推荐