在centos7下从零搭建Nginx+uWSGI+Django
Posted shouwangrenjian
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在centos7下从零搭建Nginx+uWSGI+Django相关的知识,希望对你有一定的参考价值。
一、安装Python3
在centos7中系统自带Python2.7,需要自己安装Python3
1、安装依赖
sudo yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel libffi-devel gcc make
2、安装wget
sudo yum install wget
3、获取python
wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tar.xz
4、解压、编译
tar -xf Python-3.7.3.tar.xz # 解压 ./configure prefix=/usr/local/python3 # 配置安装目录 make # 编译 make install # 安装
5、添加软连接
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
二、安装Django
sudo pip3 install django sudo ln -s /usr/local/python3/bin/django-admin /usr/bin/django-admin # 建立软连接
由于pymysql无法在django2.2上使用,也不是完全没办法,但django推荐使用mysqlclient,参考https://docs.djangoproject.com/zh-hans/2.2/ref/databases/#mysql-notes
安装mysqlclient
sudo yum install mysql-devel
sudo pip3 install mysqlclient
三、安装MySQL
1、确认系统是否装有mysql
rpm -qa | grep mysql
如果装有:
rpm -e mysql // 普通删除模式
rpm -e --nodeps mysql // 强力删除模式,如果使用上面命令删除时,提示有依赖的其它文件,则用该命令可以对其进行强力删除
2、下载yum资源包
wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
3、使用yum安装mysql
sudo rpm -ivh mysql80-community-release-el7-3.noarch.rpm sudo yum install mysql-community-server chown mysql:mysql -R /var/lib/mysql # 修改mysql权限 sudo mysqld --initialize # 初始化 systemctl start mysqld # 启动mysql服务 grep ‘temporary password‘ /var/log/mysqld.log # 查看临时密码 mysql -uroot -p # 使用临时密码登录
4、修改密码
由于默认安装有validate_password,因此密码要求:至少包含1个数字字符,1个小写字符,1个大写字符和1个特殊(非字母数字)字符,长度不小于8位;
ALTER USER ‘root’@‘localhost‘ IDENTIFIED BY ‘new_password‘;
如果希望使用简单的密码,可以修改validate_password的策略,validate_password.policy,有LOW,MEDIUM,STRONG三种等级,默认为MEDIUM;
参考:https://dev.mysql.com/doc/refman/8.0/en/validate-password.html
SET GLOBAL validate_password.policy=0; # 修改为LOW,但是要求长度不小于8位 SET GLOBAL validate_password.length=4; # 修改长度
四、安装uWSGI
1、安装uWSGI
pip3 install uwsgi ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi # 建立软连接
2、启动
a、使用命令行启动
1.进入项目目录
2.
uwsgi --http 192.168.57.129:8000 --file mysite/wsgi.py --static-map=/static=static
--http
--file
--processes 选项添加更多的进程
--threads 选项添加更多的线程
--master 一个master进程 (在Inc死掉的时候会生成它们),后面没有参数
--stats 127.0.0.1:9191 允许将uWSGI的内部统计数据作为JSON导出
b、使用配置文件启动
1.建立yourfile.ini文件
[uwsgi] #http = 0.0.0.0:3031 # 当只使用uWSGI时,使用http,配合nginx,使用socket与nginx建立连接 chdir = /root/mysite/ module = mysite.wsgi:application wsgi-file = myproject/wsgi.py socket = /root/mysite/script/uwsgi.sock pidfile = /root/mysite/script/uwsgi.pid uid = root gid = root master = true processes = 4 threads = 2 stats = 127.0.0.1:9191 static-map=/static=static
2.启动
uwsgi --ini youfile.ini
五、安装nginx
参考官方文档:https://nginx.org/en/linux_packages.html#RHEL-CentOS
1、Install the prerequisites:
sudo yum install yum-utils
2、To set up the yum repository, create the file named /etc/yum.repos.d/nginx.repo with the following contents:
[nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key [nginx-mainline] name=nginx mainline repo baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/ gpgcheck=1 enabled=0 gpgkey=https://nginx.org/keys/nginx_signing.key
-3、不是必须,如果不装标准版的nginx才需要注意。By default, the repository for stable nginx packages is used. If you would like to use mainline nginx packages, run the following command:
sudo yum-config-manager --enable nginx-mainline
4、To install nginx, run the following command:
sudo yum install nginx # When prompted to accept the GPG key, verify that the fingerprint matches 573B FD6B 3D8F BC64 1079 A6AB ABF5 BD82 7BD9 BF62, and if so, accept it.
5、配置,在/etc/nginx/conf.d/建立配置文件my.conf,这只是一个简单配置
server { listen 80; server_name 192.168.57.129; charset utf-8; #access_log logs/host.access.log main; location / { include uwsgi_params; uwsgi_pass unix:/root/mysite/script/uwsgi.sock; uwsgi_connect_timeout 30; } #静态文件,nginx自己处理,不去uWSGI请求 location /static/ { alias /root/mysite/static/; index index.html index.htm; } error_page 404 /404.html; error_page 500 502 503 504 /50x.html; }
6、注意/etc/nginx/nginx.conf中的user,默认为nginx,需要改为主机用户
7、启动nginx
sudo nginx -c /etc/nginx/nginx.conf # 启动 sudo nginx -s quit 或者 sudo nginx -s stop # 停止
如果uwsgi_params文件缺失,自己添加,注意路径要与配置文件中对应,默认应该在nginx目录下有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;
以上是关于在centos7下从零搭建Nginx+uWSGI+Django的主要内容,如果未能解决你的问题,请参考以下文章
Centos7下搭建Django+uWSGI+nginx基于python3
CentOS7 + Python3 + Django(rest_framework) + MySQL + nginx + uwsgi 部署 API 开发环境, 记坑篇
centos+python2+flask+nginx+uwsgi环境搭建