最强最全入门部署_01:阿里云Centos 7.5 Django 2.x + uWsgi + Nginx 要是失败我跪下

开发和部署运维,都很重要,作为强者,你必须都会,哪怕从0开始...

阿里云 centos 7.5,处理好防火墙和安全组设置规则 

以下以root 用户操作

yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel

#首先将在本地开发的django 程序拷贝到阿里云服务器您自己指定目录下
#pip3 下载好依赖
#测试启动收集静态文件统一到项目根目录下的static文件夹下

python3 manage.py collectstatic

python3 manage.py runserver
python3 manage.py check

以上正常

python3 服务器部署
需要注意修改以下几项 
setting.py 文件里面,注释掉

#DEBUG = True


其次,处理好

ALLOWED_HOSTS = ['www.aprmxxx.cn', '127.0.0.1', 'localhost','www.shunzi666.cn','你的ip']


也可以直接如下

ALLOWED_HOSTS = [*]

#测试启动

python3 manage.py runserver
python3 manage.py check


django 没问题,接下来进行 uwsgi  nginx 的部署安装

pip3 -m install  uwsgi
pip3 install uwsgi --upgrade
ln -s /usr/local/python3/bin/uwsgi /usr/local/bin/uwsgi
uwsgi --version

#测试部署

uwsgi --http :9000 --wsgi-file 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

pkill -9 uwsgi

在项目根目录下建立uwsgi.ini 文件

cd /path/to/your/project
vim uwsgi.ini

uwsgi.ini 文件内容

[uwsgi]
socket=127.0.0.1:8000
chdir=/path/to/your/project
#uwsgi-file=project/wsgi.py
module=project.wsgi
master=True
pidfile=/path/to/your/project/project-master.pid
vacuum=True
max-requests=5000
#harakiri=20
processes=5
threads=2
daemonize=/path/to/your/project/uwsgi_baoming.log
disabled-logging=true
env = LANG=en_US.UTF-8
uid=1000
gid=2000
#stats=0.0.0.0:9191

#以上内容写好之后保存好,在项目根目录下启动uwsgi 后台服务器

uwsgi uwsgi.ini


成功或者失败都会有提示,如果失败了,请查看

cat /path/to/your/project/uwsgi_baoming.log


针对具体问题解决具体问题
最有可能出现的问题是文件读写权限(尤其是历史日志文件)permission denied 的问题
如果遇到,那么直接在其文件的问价夹上修改权限
chmod -R 777 文件夹

以上没问题了,及进行nginx的设置

yum -y install nginx
vim /etc/nginx/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;

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

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        #server_name  _;
        root         /usr/share/nginx/html;
        # the port your site will be served on
        #listen      80;
        # the domain name it will serve for
        server_name shunzi666.cn; # substitute your machine's IP address or FQDN
        charset     utf-8;

        # max upload size
        client_max_body_size 75M;   # adjust to taste
        # Load configuration files for the default server block.
        #include /etc/nginx/default.d/*.conf;
        #proxy_set_header X-Real-IP $remote_addr;
        #proxy_set_header Host $host;
        #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        location /media/ {
             #add_header Content-Encoding gzip;
             alias /path/to/your/project/media/;  # 多媒体位置
        }
        location /static/ {
             alias /path/to/your/project/static/;  # 静态资源
        }

        location / {
             uwsgi_pass  127.0.0.1:8000;
             include uwsgi_params;
        }

        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.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        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 / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

保存提交配置文件,启动nginx ,如果有错误可以 nginx t 来查看,或者看日志

/path/to/your/project


# 如果修改了文件记得重启nginx

systemctl restart nginx
systemctl status nginx


从本地浏览器访问云上资源,查看部署结果

如有任何问题,欢迎随时联系

微信:shun18301513217

发布了127 篇原创文章 · 获赞 35 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/shunzi2016/article/details/99062453