Centos 7 下部署Django + uWSGI + Nginx
环境:
Python: 3.6
Django: 2.1
OS: CentOS 7 x86_64
uwsgi: 2.0.17
安装Python3.6
不要删除自带的python2.7,否则会出问题,因为centos许多软件需要依赖系统自带python
安装依赖工具 yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel
下载 wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz
解压 tar -zxvf Python-3.6.5.tgz
移动至规范的放软件的目录下 mv Python-3.6.5 /usr/local
安装:
* cd /usr/local/Python-3.6.5/
* ./configure
* make
* make install
验证
python -V
source /home/cosmic/py3.6env/bin/activate 进入虚拟环境
安装uWSGI
def application(env, start_response):start_response('200 OK', [('Content-Type','text/html')])return [b"Hello Django"]
uwsgi --http :8001 --wsgi-file test.py
浏览器访问,网页能显示 Hello Django 那么就没问题
- 如果安装失败
- deactivate 退出虚拟环境
- yum install -y python-devel
- easy_install uwsgi
安装Nginx
配置源
vi /etc/yum.repos.d/nginx.repo 添加下面内容
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/7/x86_64/
gpgcheck=0
enabled=1
gpkcheck=0 表示对从这个源下载的rpm包不进行校验;
enable=1 表示启用这个源。
yum install nginx
启动nginx:
systemctl start nginx
修改默认端口号(默认为80)
vim /etc/nginx/conf.d/default.conf
systemctl restart nginx 重启nginx,直接访问http://ip:8089 能看到nginx的欢迎界面即可。
-
配置
配置uwsgi启动django的参数
vim django_uwsgi.ini
[uwsgi]
通过uwsgi访问django需要配置成http
通过nginx请求uwsgi来访问django 需要配置成socket
9000 是django的端口号
socket = :9000
web项目根目录
chdir = /home/root/pydj/django_one
module指定项目自带的的wsgi配置文件位置
module = django_one.wsgi
允许存在主进程
master = true
开启进程数量
processes = 3
服务器退出时自动清理环境
vacuum = true
配置nginx
vi /etc/nginx/conf.d/default.conf
在文件最后,新加一个server
server {
listen 8089;
listen [::]:8089;
server_name 127.0.0.1 192.168.10.114;
location / {
include /etc/nginx/uwsgi_params;
uwsgi_pass 127.0.0.1:9000;
}
location /index/ {
root /index/;
}
location /static{
alias /home/root/pydj/django_one/sign/static;
}
}
- 8089 是对外的端口号
- server_name nginx代理uwsgi对外的ip
- 127.0.0.1:9000 即当nginx收到8089端口的请求时,直接将请求转发给 127.0.0.1:9000
uwsgi启动django
# 进入项目根目录
/home/root/pydj/django_one
启动
uwsgi –ini django_uwsgi.ini
重启Nginx
systemctl restart nginx
secrtcrt
filiza
xshell
安装
yum install mysql-devel gcc gcc-devel python-devel
Django==2.1.2
django-haystack==2.8.1
mysqlclient==1.3.13
pytz==2018.5
uWSGI==2.0.17.1
Whoosh==2.7.4
semanage port -l | grep http_port_t
mysql可以被局域网任意主机访问
mysql> use mysql;
mysql> update user set host = '%' where user = 'root';
mysql> select host, user from user;
mysql> flush privileges;