一文搞定CentOS下python/django+env+mysql+uwsgi+ngnix的安装配置

  • 测试环境
  • 需要使用的SSH工具
  • python3.7.1安装
  • 安装python虚拟环境
  • 配置MariaDB
  • 安装配置uwsgi
  • 安装配置nginx


测试环境

  • VMWare Workstation 15 Pro (虚拟机)
  • CentOS-7-x86_64-DVD-1804.iso

需要使用的SSH工具

  • 个人建议使用 MobaXterm ,此工具功能非常强大,支持SSH同步SFTP,这个对远程操作linux上传文件等太方便了.这个就请自行下载了.

python3.7.1安装

  • 下载python源码包 Python-3.7.1.tar.xz
  • 通过SSH工具上传python源代码包到CentOS服务器上
  • 以下是安装脚本
su #登录超级用户
mkdir /usr/local/python3
mv /home/kt/Python-3.7.1.tar.xz /usr/local/python3/
yum -y install gcc-c++
yum -y install zlib zlib-devel openssl openssl-devel libffi-devel

tar xvf Python-3.7.1.tar.xz
cd Python-3.7.1/
./configure --prefix=/usr/local/python3
make && make install
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
PATH=$PATH:$HOME/bin:
PATH=$PATH:$HOME/bin:/usr/local/python3/bin
pip3 install --upgrade pip

以上安装需要联网进行安装,如果有CentOS的安装盘就可参考我之前的文章,直接使用安装盘进行模块的安装

如何在群晖中直接执行python脚本 群晖安装python3.7_nginx


如何在群晖中直接执行python脚本 群晖安装python3.7_nginx_02

安装python虚拟环境

su
pip3 install virtualenv
exit
/usr/local/python3/bin/virtualenv -p /usr/bin/python3 myenv
source ./myenv/bin/activate
python3 -V
pip3 -V

pip3 install -r require.pip -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

如何在群晖中直接执行python脚本 群晖安装python3.7_uwsgi_03

require.pip文件

django
virtualenv
django-extensions
django-werkzeug-debugger-runserver
pyOpenSSL
pymysql
requests
pdbpp
pillow
qrcode
uwsgi
pyecharts
concurrent-log-handler

opencv-python
matplotlib
numpy

配置MariaDB

MariaDB等同于MySQL
  • 之前安装CentOS已经选择了MariaDB模块,因此MariaDB已经安装好了.只需要配置即可
su
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload
systemctl enable mariadb
systemctl start mariadb
mysqladmin -u root password '1234'
mysql -u root -p
		grant all privileges on *.* to 'root'@'%' identified by '1234' with grant option;
		flush privileges;
		CREATE DATABASE IF NOT EXISTS myTestDB DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_general_ci;
		exit
		
systemctl restart mariadb

脚本说明:

使用超级用户
加入端口3306到防火墙
更新防火墙
启用mariadb数据库服务
启动mariadb数据库服务
更新mariadb的root用户密码为'1234'
登录到mysql的shell
	允许root远程登录
	更新
	创建一个数据库,支持超宽字符集
	退出mysql的shell
重新mariadb服务

安装配置uwsgi

pip3 install uwsgi
uwsgi --ini myproject_uwsgi.ini   ####通过配置文件启动
uwsgi --http 0.0.0.0:6080 --file myproject/wsgi.py --static-map=/static=static    ####通过python直接启动

uwsgi --ini uwsgi.ini				#### 启动
uwsgi --stop uwsgi.pid			#### 停止
uwsgi --reload uwsgi.pid		#### 重启

配置文件示例

# uwsig使用配置文件启动,myproject为项目名称
[uwsgi]
# 项目目录
chdir=/home/kt/02.dev/project/myproject/
# 指定项目的application
module=myproject.wsgi:application
# 进程个数       
workers=4
pidfile=/home/kt/02.dev/project/myproject/uwsgi.pid
# 指定IP端口       
#http=192.168.66.141:8000
socket = 127.0.0.1:8000
# 指定静态文件
static-map=/static=static
# 启动uwsgi的用户名和用户组
uid=root
gid=root
# 启用主进程
master=true
# 自动移除unix Socket和pid文件当服务停止的时候
vacuum=true
# 序列化接受的内容,如果可能的话
thunder-lock=true
# 启用线程
enable-threads=true
# 设置自中断时间
harakiri=30
# 设置缓冲
post-buffering=4096
# 设置日志目录
daemonize=/home/kt/02.dev/project/myproject/log/uwsgi.log

安装配置nginx

- 下载nginx-1.14.2.tar.gz
http://nginx.org/en/download.html- 上传到CentOS或直接在CentOS中下载

su
cd /usr/local/src
 wget http://120.52.51.15/nginx.org/download/nginx-1.14.2.tar.gz
 tar zxvf nginx-1.14.2.tar.gz
 cd nginx-1.14.2/
 ./configure --with-http_ssl_module
 make && make install
/usr/local/nginx/sbin/nginx -c /home/kt/02.dev/project/kttest_nginx.conf

- nginx配置文件示例

  • kttest_nginx.conf文件
user  kt kt;
worker_processes  1;



events {
    worker_connections  1024;
}


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

    sendfile        on;

    keepalive_timeout  65;


    server {
        listen       6080;
        server_name  10.0.0.118 www.test.com.cn;
		ssl                  on;  
		ssl_certificate      /home/kt/02.dev/project/www.test.com.cn.crt;
		ssl_certificate_key  /home/kt/02.dev/project/www.test.com.cn.key;

		ssl_session_cache    shared:SSL:1m;
		ssl_session_timeout  5m;

		ssl_ciphers  HIGH:!aNULL:!MD5;
		ssl_prefer_server_ciphers  on;
		
		charset utf-8;

        location / {
			client_max_body_size 100m;
			include /home/kt/02.dev/project/uwsgi_params; # 导入一个Nginx模块他是用来和uWSGI进行通讯的
			uwsgi_connect_timeout 30; # 设置连接uWSGI超时时间
			uwsgi_pass 127.0.0.1:8000; # 指定uwsgi的sock文件所有动态请求就会直接丢给他
        }

		location /static/ {
			alias /home/kt/02.dev/project/kttest/static/;
        }
		
        error_page  404              /main/404.html;

        error_page   500 502 503 504  /main/50x.html;
        location = /main/50x.html {
            root   html;
        }
    }
	
	# 以下实现HTTPS代理
	server {
        listen       6090 ssl;
        server_name  10.0.0.118 www.test.com.cn;

		ssl_certificate      /home/kt/02.dev/project/www.test.com.cn.crt;
		ssl_certificate_key  /home/kt/02.dev/project/www.test.com.cn.key;

        location / {
            proxy_redirect off;
    #        #proxy_set_header Host $host;
    #        #proxy_set_header X-Real-IP $remote_addr;
    #        #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass https://192.168.34.136:6080;
        }
    #    access_log logs/a.log;
    }
    
}

- mime.types文件

types {
    text/html                                        html htm shtml;
    text/css                                         css;
    text/xml                                         xml;
    image/gif                                        gif;
    image/jpeg                                       jpeg jpg;
    application/javascript                           js;
    application/atom+xml                             atom;
    application/rss+xml                              rss;

    text/mathml                                      mml;
    text/plain                                       txt;
    text/vnd.sun.j2me.app-descriptor                 jad;
    text/vnd.wap.wml                                 wml;
    text/x-component                                 htc;

    image/png                                        png;
    image/svg+xml                                    svg svgz;
    image/tiff                                       tif tiff;
    image/vnd.wap.wbmp                               wbmp;
    image/webp                                       webp;
    image/x-icon                                     ico;
    image/x-jng                                      jng;
    image/x-ms-bmp                                   bmp;

    application/font-woff                            woff;
    application/java-archive                         jar war ear;
    application/json                                 json;
    application/mac-binhex40                         hqx;
    application/msword                               doc;
    application/pdf                                  pdf;
    application/postscript                           ps eps ai;
    application/rtf                                  rtf;
    application/vnd.apple.mpegurl                    m3u8;
    application/vnd.google-earth.kml+xml             kml;
    application/vnd.google-earth.kmz                 kmz;
    application/vnd.ms-excel                         xls;
    application/vnd.ms-fontobject                    eot;
    application/vnd.ms-powerpoint                    ppt;
    application/vnd.oasis.opendocument.graphics      odg;
    application/vnd.oasis.opendocument.presentation  odp;
    application/vnd.oasis.opendocument.spreadsheet   ods;
    application/vnd.oasis.opendocument.text          odt;
    application/vnd.openxmlformats-officedocument.presentationml.presentation
                                                     pptx;
    application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
                                                     xlsx;
    application/vnd.openxmlformats-officedocument.wordprocessingml.document
                                                     docx;
    application/vnd.wap.wmlc                         wmlc;
    application/x-7z-compressed                      7z;
    application/x-cocoa                              cco;
    application/x-java-archive-diff                  jardiff;
    application/x-java-jnlp-file                     jnlp;
    application/x-makeself                           run;
    application/x-perl                               pl pm;
    application/x-pilot                              prc pdb;
    application/x-rar-compressed                     rar;
    application/x-redhat-package-manager             rpm;
    application/x-sea                                sea;
    application/x-shockwave-flash                    swf;
    application/x-stuffit                            sit;
    application/x-tcl                                tcl tk;
    application/x-x509-ca-cert                       der pem crt;
    application/x-xpinstall                          xpi;
    application/xhtml+xml                            xhtml;
    application/xspf+xml                             xspf;
    application/zip                                  zip;

    application/octet-stream                         bin exe dll;
    application/octet-stream                         deb;
    application/octet-stream                         dmg;
    application/octet-stream                         iso img;
    application/octet-stream                         msi msp msm;

    audio/midi                                       mid midi kar;
    audio/mpeg                                       mp3;
    audio/ogg                                        ogg;
    audio/x-m4a                                      m4a;
    audio/x-realaudio                                ra;

    video/3gpp                                       3gpp 3gp;
    video/mp2t                                       ts;
    video/mp4                                        mp4;
    video/mpeg                                       mpeg mpg;
    video/quicktime                                  mov;
    video/webm                                       webm;
    video/x-flv                                      flv;
    video/x-m4v                                      m4v;
    video/x-mng                                      mng;
    video/x-ms-asf                                   asx asf;
    video/x-ms-wmv                                   wmv;
    video/x-msvideo                                  avi;
}

- uwsgi_params文件

uwsgi_param  QUERY_STRING       $query_string;
uwsgi_param  REQUEST_METHOD     $request_method;
uwsgi_param  CONTENT_TYPE       $content_type;
uwsgi_param  CONTENT_LENGTH     $content_length;

uwsgi_param  REQUEST_URI        $request_uri;
uwsgi_param  PATH_INFO          $document_uri;
uwsgi_param  DOCUMENT_ROOT      $document_root;
uwsgi_param  SERVER_PROTOCOL    $server_protocol;
uwsgi_param  REQUEST_SCHEME     $scheme;
uwsgi_param  HTTPS              $https if_not_empty;

uwsgi_param  REMOTE_ADDR        $remote_addr;
uwsgi_param  REMOTE_PORT        $remote_port;
uwsgi_param  SERVER_PORT        $server_port;
uwsgi_param  SERVER_NAME        $server_name;