django+nginx+uwsgi部署
2018-03-14 本文已影响74人
你常不走的路
安装nginx
sudo apt install nginx
访问127.0.0.1:80 端口 看是否启动成功
nignx命令
/etc/init.d/nginx restart/stop/start/status
安装虚拟环境
pip install virtualenv
cd /var/www/
virtualenv env27
source env27/bin/activate
#激活虚拟环境
pip install -i https:pypi.douban.com/simple Mezzanine #他会自动安装合适的django版本 使用豆瓣源快一点
mezzanine-project django-project #创建项目
cd django-project
python manage.py createdb #创建数据库
python manage.py collectstatic #收集静态资源
vim django-project/settings.py
#修改settings.py 中的ALLOWED_HOSTS = ['*']
python manage.py runserver 0.0.0.0:8000
#访问 看看是否能访问
安装uwsgi
#依然是在虚拟环境中
pip install uwsgi
#然后连接uwsgi 和 django-project
uwsgi --http-socket :8000 --chdir /var/www/django-project/ --wsgi-file django-project/wsgi.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191
#参数的意思是
chdir 目录路径
wsgi-file django本身的wsgi.py 路径
master 应该是作为主机
processes 多少个进程
threads 多少个线程
stats 在什么端口查看 状态
#后面 做成 uwsgi.ini
测试访问8000端口是否可以 加载成功
如果能够加载 但是没有静态文件 那就是对的
这里b站up主录制的视频的
![](https://img.haomeiwen.com/i1724459/73a944b3a20752c7.png)
配置Nginx.conf
cd /var/www/
vim Nginx.conf
Nginx.conf内容
# configuration fo the server
server{
# the port your site will be served on
listen 80; #监听的端口
# the domain name it will serve for
server_name 192.168.2.23; # substitute your machine's IP address or FQDN #可以访问的ip
charset utf-8; #编码
access_log /var/www/librepath/logs/access.log; #访问日志,行为日志
error_log /var/www/librepath/logs/error.log; #错误日志
# max upload size
client_max_body_size 75M; #adjust to taste #最大负载
# Django media #图片资源等文件 路径
location /media {
alias /var/www/librepath/media; #your Djagno project's media files -amed as required
}
# Django static #静态文件路径
location /static {
alias /var/www/librepath/static; #your Djagno project's media files -mmed as required
}
#最后是 连接 uwsgi
# Finally,send all non-media requests to the Django server.
location / {
uwsgi_pass 127.0.0.1:8001; #套接字
include /etc/nginx/uwsgi_params; # the uwsgi_params file you installed nginx目录下 跟 uwsgi对接的 一个文件
}
}
然后是配置 nginx.ini
[uwsgi]
chdir =/var/www/librepath
wsgi-file =/var/www/librepath/librepath/wsgi.py
home =/var/www/env27
master =true
processes =4
threads =2
status =127.0.0.1:9191
#user full path to be safe
socket = :8001 #这里的套接端口 就是 上面写的 8001
chmod-socket = 666
#clear environment on exit
vacuum =true
把nginx.conf 配置到 nginx目录下
cd /etc/nginx/sites-enabled
ls
里面有一个 default文件
由于它监听了 80 端口 所以我们不能监听了
ll -s default #发现他是一个 软连接 所以删除也没有什么
然后创建一个软连接
ln -s /var/www/nginx.conf django-project.conf
启动测试一下
cd /var/www/
#只有虚拟环境下 才有uwsgi
uwsgi --ini uwsgi.ini
#然后测试一下 看看 是否成功
重启
/etc/init.d/nginx restart 如果失败
systemctl restart nginx.service
nginx -t 查看错误在哪
然后修改
最后来一张部署成功的图
![](https://img.haomeiwen.com/i1724459/43855ac4c780a47e.png)