美文网首页
centos7+nginx+uwsgi+python3+djan

centos7+nginx+uwsgi+python3+djan

作者: 阳光乐山 | 来源:发表于2019-11-01 13:43 被阅读0次

    说明:这是转载[https://www.cnblogs.com/lvye001/p/10631276.html]的博客(https://www.cnblogs.com/lvye001/p/10631276.html)

    CentOS7下部署Django项目详细操作步骤

    前记:购买腾讯云服务器,配置自选,当然新用户免费体验半个月,我选择的系统是centos7系统版本,

    接下来我们来看整个配置项目流程。

    部署是基于:centos7+nginx+uwsgi+python3+django 之上做的

    1,详细步骤(下面步骤都是ROOT权限执行):

    一、更新系统软件包

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">yum update -y</pre>

    二、安装软件管理包和可能使用的依赖

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">yum -y groupinstall "Development tools"
    yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel</pre>

    三、下载Pyhton3到/usr/local 目录

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz</pre>

    解压

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">tar -zxvf Python-3.6.3.tgz</pre>

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">cd Python-3.6.3</pre>

    编译安装到指定路径

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">./configure --prefix=/usr/local/python3</pre>

    /usr/local/python3 路径可以自己指定,自己记着就行,下边要用到。

    安装python3

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">make
    make install</pre>

    安装完成之后 建立软链接 添加变量 方便在终端中直接使用python3

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">ln -s /usr/local/python3/bin/python3.6 /usr/bin/python3</pre>

    Python3安装完成之后pip3也一块安装完成,不需要再单独安装
    一样建立软链接

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">ln -s /usr/local/python3/bin/pip3.6 /usr/bin/pip3</pre>

    四、查看Python3和pip3安装情况

    timg.jpg

    五、安装virtualenv ,建议大家都安装一个virtualenv,方便不同版本项目管理。

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">pip3 install virtualenv</pre>

    建立软链接

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">ln -s /usr/local/python3/bin/virtualenv /usr/bin/virtualenv</pre>

    安装成功在根目录下建立两个文件夹,主要用于存放env和网站文件的。(个人习惯,其它人可根据自己的实际情况处理)

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">mkdir -p /data/env
    mkdir -p /data/wwwroot</pre>

    六、切换到/data/env/下,创建指定版本的虚拟环境。

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">virtualenv --python=/usr/bin/python3 pyweb</pre>

    然后进入/data/env/pyweb/bin

    启动虚拟环境:

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">source activate</pre>

    timg.jpg

    留意我标记的位置,出现(pyweb),说明是成功进入虚拟环境。

    CentOS7安装MySQL(完整版)

    1 下载并安装MySQL官方的 Yum Repository

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm</pre>

    2, 使用上面的命令就直接下载了安装用的Yum Repository,大概25KB的样子,然后就可以直接yum安装了。

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> yum -y install mysql57-community-release-el7-10.noarch.rpm</pre>

    3,安装MySQL服务器

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">yum -y install mysql-community-server</pre>

    4, MySQL数据库设置

    首先启动MySQL

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">systemctl start mysqld.service</pre>

    查看MySQL运行状态,运行状态如图:

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">systemctl status mysqld.service</pre>

    image

    此时MySQL已经开始正常运行,不过要想进入MySQL还得先找出此时root用户的密码,通过如下命令可以在日志文件中找出密码:

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">grep "password" /var/log/mysqld.log</pre>

    image

    如下命令进入数据库:

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> mysql -uroot -p</pre>

    输入初始密码(是上面图片最后面的 no;e!5>>alfg),此时不能做任何事情,因为MySQL默认必须修改密码之后才能操作数据库:

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password';</pre>

    其中‘new password’替换成你要设置的密码,注意:密码设置必须要大小写字母数字和特殊符号(,/';:等),不然不能配置成功

    开启mysql的远程访问

    执行以下命令开启远程访问限制(注意:下面命令开启的IP是 192.168.0.1,如要开启所有的,用%代替IP):

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">grant all privileges on . to 'root'@'192.168.0.1' identified by 'password' with grant option;</pre>

    然后再输入下面两行命令

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">mysql> flush privileges;
    mysql> exit</pre>

    然后在windows远程测试(也可以用可视化第三方navcat等)

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">mysql -h 148.70.75.219 -P 3306 -uroot -p</pre>

    redis安装

    下载安装包

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">wget http://download.redis.io/releases/redis-4.0.2.tar.gz</pre>

    解压安装包并安装

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">tar xzf redis-4.0.2.tar.gz
    cd redis-4.0.2 make
    make install</pre>

    redis 的很多设置例如:设置密码,远程访问,持久化等,请移驾 点击 跳转。

    最后开始进行项目配置

    1,安装uwsgi

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">pip install uwsgi</pre>

    2,配置uwsgi,在项目根目录下(manage.py同层目录),新建uwsgi.ini

    [ 复制代码

    ](javascript:void(0); "复制代码")

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"># mysite_uwsgi.ini file
    [uwsgi] # Django-related settings # 项目根目录的路径
    chdir = /data/wwwroot/wtp_02 # Django's wsgi file #wsgi-file = demo01/wsgi.py
    module = wtp_02.wsgi:application # uwsgi服务器的角色
    master = true # 进程7数
    processes=4

    线程数

    threads=2

    存放进程编号的文件

    pidfile= uwsgi.pid # the socket (use the full path to be safe
    socket=:8082

    http = 127.0.0.1:8002

    stats = 127.0.0.1:9191 # ... with appropriate permissions - may be needed #chmod-socket = 664 # clear environment on exit

    vacuum = true # 日志文件,因为uwsgi可以脱离终端在后台运行,日志看不见。我们以前的runserver是依赖终端的
    daemonize=uwsgi.log # 虚拟环境的目录
    pythonpath = /data/env/pyweb</pre>

    [ 复制代码

    ](javascript:void(0); "复制代码")

    然后启动uwsgi

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">uwsgi -i uwsgi.ini # 启动
    uwsgi --stop uwsgi.pid # 停止</pre>

    我们查下是否启动成功

    image

    通过查询日志,成功运行,证明配置没有问题

    配置项目静态文件

    修改你的dango setting.py

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">DEBUG = False

    ALLOWED_HOSTS = ['*']

    STATIC_ROOT = os.path.join(BASE_DIR,'static') #静态文件收集 配置路径且将 STATICFILES_DIRS 注释</pre>

    然后收集你项目里的所有静态文件,

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">python manage.py collectstatic</pre>

    3,安装nginx

    切进 该 /usr/local/ 目录下,执行下面命令

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">wget http://nginx.org/download/nginx-1.13.7.tar.gz</pre>

    下载完成后,执行解压命令:

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">tar -zxvf nginx-1.13.7.tar.gz</pre>

    进入解压后的nginx-1.13.7文件夹,依次执行以下命令:

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">./configure
    make
    make install</pre>

    nginx一般默认安装好的路径为/usr/local/nginx
    在/usr/local/nginx/conf/中先备份一下nginx.conf文件,以防意外。

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">cp nginx.conf nginx.conf.bak</pre>

    然后打开nginx.conf,把原来的内容删除,直接加入以下内容:

    [ 复制代码

    ](javascript:void(0); "复制代码")

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">user root;
    worker_processes 1; #

    error_log logs/error.log; ##error_log logs/error.log notice; ##error_log logs/error.log info;

    pid logs/nginx.pid;

    events {

    worker_connections 1024;
    

    } #

    http {
    include mime.types;
    default_type application/octet-stream;
    sendfile on;
    keepalive_timeout 65;
    upstream django { #django项目的端口号 和uwsgi里面的端口号保存一致
    server 127.0.0.1:8082; # for a web port socket (we'll use this first)
    } # configuration of the server
    server { # nginx服务的端口号 不用修改
    listen 8001; # the domain name it will serve for # 这里可以填写你的ip地址或者域名
    server_name 148.70.75.219;
    charset utf-8;
    gzip on;
    gzip_disable "msie6";
    gzip_proxied any;
    gzip_min_length 1k;
    gzip_comp_level 4;
    gzip_types text/plain text/css application/json application/x-javascript text/javascript text/xml image/jpeg image/png image/gif; # max upload size
    client_max_body_size 75M; # adjust to taste

    Django media #location /media { # alias /home/x/work/liebiao/lbpro/media; # 指向django的media目录 #}

    location /static {
    alias /home/x/work/liebiao/lbpro/static; # 指向django的static目录
    } # Finally, send all non-media requests to the Django server.
    location / {
    uwsgi_pass django;
    include uwsgi_params; # the uwsgi_params file you installed
    }
    }
    }</pre>

    [ 复制代码

    ](javascript:void(0); "复制代码")

    配置完成后,启动nginx

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf #启动nginx
    ./nginx -s reload                             #重启nginx/当然stop是停止 (/usr/local/nginx/sbin下)
    ./nginx                                 #启动nginx</pre>

    查询是否启动成功

    image

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">lsof -i:8000 查端口占用
    ps -ef|grep nginx 查端口号
    pkill -9 nginx 强制停止运行</pre>

    现在打开浏览器即可进行访问。

    image

    当然,在实际的开发过程中,经常会碰到服务器需要部署多个项目,我们只需要把 nginx.conf 配置文件稍微修改即可

    [ 复制代码

    ](javascript:void(0); "复制代码")

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">#user root; #worker_processes 1;
    events {
    worker_connections 1024;
    }

    http {
    include mime.types;
    default_type application/octet-stream;

    sendfile        on;
    
    keepalive_timeout 65;
    
    gzip on;
    
    server {
        listen       #监听端口号;
        server_name  #你的服务器ip;
    
     location / { # 将nginx所有请求转到uwsgi
    

    include uwsgi_params; # uwsgi的ip与端口
    uwsgi_pass 127.0.0.1:8082;
    } #location /static {
    # 静态文件目录
    # alias /home/XXXXX(静态文件目录);
    # }
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
    root html;
    }
    }

      server {
        listen       # 监听端口号;
        server_name  # 你的服务器ip; 
      client_max_body_size 75M; # adjust to taste 
    

    location / { # 将nginx所有请求转到uwsgi
    include uwsgi_params; # uwsgi的ip与端口
    uwsgi_pass 127.0.0.1:8100;
    }

      location /static { # 静态文件目录
            alias /data/wwwroot/wtp_upload_video/static;
            }
    
      error_page 500 502 503 504  /50x.html;
      location = /50x.html {
             root   html;
            }
    
            }
    

    } </pre>

    [ 复制代码

    ](javascript:void(0); "复制代码")

    标红的是上面要修改的地方,

    相关文章

      网友评论

          本文标题:centos7+nginx+uwsgi+python3+djan

          本文链接:https://www.haomeiwen.com/subject/bsmfbctx.html