Centos django+uwsgi+nginx部署
安装Python
CentOS 7 Linux发行版默认包含Python 2。但是,Python 2将于2020年1月1日停产。尽管一些遗留应用程序可能由于各种原因需要访问Python 2,但是启动Python 3中的新项目至关重要。
通过软件管理包Yum
-
更新环境
$ yum update -y
- -y:安装包的时候会询问y/n,这个参数是所有询问默认y,下边不再提醒,在终端输入以上命令时,直接下载安装,不再要求确认。
在7.7之前的CentOS 7版本中,CentOS基本存储库没有提供Python 3软件包。从CentOS 7.7开始,Python 3可在基本软件包存储库中使用!
-
安装Python3
$ yum install -y python3
源码安装
环境依赖
$ yum install gcc openssl-devel bzip2-devel libffi-devel -y
下载Python
# 下载
$ wget https://www.python.org/ftp/python/3.8.3/Python-3.8.3.tgz
# 解压
$ tar -xzf Python-3.8.3.tgz
安装Python3
进入目录
$ cd Python-3.8.3/
编译
$ ./configure --enable-optimizations
安装
$ make && make install
uwsgi
安装uwsgi
由于uWSGI由EPEL提供,因此无需使用进行安装pip
。
$ yum -y install uwsgi uwsgi-plugin-python3
配置uwsgi
在django项目manage.py
同级目录下,创建uwsgi.ini
文件。
[uwsgi]
chdir=/home/wj/Project/data_analysis
module=data_analysis.wsgi:application
socket=:8000
processes=5
threads=2
master=True
pidfile=uwsgi.pid
vacuum=True
max-requests=5000
daemonize=uwsgi.log
env DJANGO_SETTINGS_MODULE=data_analysis.settings
home=/home/wj/Envs/django
注意:如果后期运行时出现以下这种错误,只需在uwsgi.ini
文件里添加plugins = python36
(python36是Python版本)
-- unavailable modifier requested: 0 --
运行uwsgi
$ uwsgi --ini uwsgi.ini
会生成指定自己指定名称的.log
文件和.pid
文件
停止uwsgi
$ uwsgi --stop uwsgi.pid
nginx
安装
$ yum install -y nginx
配置nginx
查看nginx配置文件位置
$ nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
查看配置文件/etc/nginx/nginx.conf
$ cat /etc/nginx/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 /var/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;
}
我们只需关注include /etc/nginx/conf.d/*.conf;
进入/etc/nginx/conf.d/
,在此目录下创建以.conf
结尾的文件。
创建配置文件
$ vim django.conf
# mysite_nginx.conf
upstream django {
server 127.0.0.1:8001; # 服务器的套接字
}
server {
# 监听的端口号
listen 8000;
# 编码
charset utf-8;
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include uwsgi_params; # 可下载,也可自己创建文件。
}
}
uwsgi_params:https://github.com/nginx/nginx/blob/master/conf/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;
检查nginx配置
$ nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
启动nginx
$ service nginx restart