django+nginx+uwsgi_cent0s7.4 部署
几条命令
# 查看是否有 uwsgi 相关的进程 ps -aux|grep "uwsgi" # 杀死有关 uwsgi 相关的进程 pkill -9 uwsgi # 使用yum install nginx 安装nginx的安装目录 /etc/nginx/
安装环境
# 安装 gcc 环境 yum install -y gcc # 为python安装pip -- 这里的路径在执行的时候,估计会有问题,涉及相对路径和绝对路径 wget https://pypi.python.org/packages/11/b6/abcb525026a4be042b486df43905d6893fb04f05aac21c32c638e939e447/pip-9.0.1.tar.gz#md5=35f01da33009719497f01a4ba69d63c9 tar -xf pip-9.0.1.tar.gz cd pip-9.0.1 sudo python setup.py install cd .. rm -rf * # 安装 Django pip install django==1.8 # 解决uwsgi安装报错问题 yum install -y python-devel.x86_64 # 安装uwsgi pip install uwsgi # 安装 nginx yum install -y nginx
修改django配置
# 将debug模式改成False
DEBUG = False
# 允许访问的 host, 可以写成单独的 host, 也可以直接写 "*",代表全部
ALLOWED_HOSTS = ['*', ]
STATIC_URL = '/static/'
# 修改 静态文件的位置
if DEBUG:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
else:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
收集静态文件
python manage.py collectstatic
这条命令会将所有 Django 项目的静态文件搜集到上面配置中的,静态文件的位置
修改uwsgi以及nginx配置
-
uwsgi配置
[uwsgi] # 使用socket协议,直接绑定 127.0.0.1:8888,和下面http任意存在一个就可以了 # 这里的1207.0.01:8888 在nginx中也有用到 socket=127.0.0.1:8888 # 使用http协议,监听端口,可以是 0.0.0.0:8888(比socket多了一层封装) # http = :8888 # 本地项目目录 chdir = /home/dja/cost_total/ # 项目 wsgi.py 文件目录 wsgi-file = /home/dja/cost_total/cost_total/wsgi.py # 该项目使用的进程数,一般使用电脑的 核数 processes = 1 # 开启的线程数 threads = 1 # 指定静态文件路径 static-map=/static=/home/dja/cost_total/static # pidfile = /opt/classiclaw/nginx/logs/uwsgi.pid # 日志文件的位置 daemonize = /var/log/nginx/uwsgi.log
-
nginx配置
# 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 /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;
# 修改的配置内容 -->开始
upstream django { # 设置别名,如将 127.0.0.1:8888 设置一个别名,叫 django,下面可以直接使用这个别名
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8888; # for a web port socket (we'll use this first)
}
server { # 这个server标识我要配置了
listen 80; # 80 是http默认的端口, 443 是https默认的端口(网页一般使用这两个端口)
server_name _; # 你访问的路径前面的url名称,后面可以写一条短横杠(我没写,报错了)
charset utf-8; # Nginx编码
# 指定静态文件路径,含义为,如果访问的是 /static 开头的,走下面的位置获取内容
# 之前我写 /static/ 没有效果,后来修改为 /static
location /static { # 如果是 /static 开头的,那么直接走这里,就是 静态文件存放的位置
alias /home/dja/cost_total/static;
}
# 指定项目路径uwsgi
# 如果是 其它开头的,那么直接走这里,就是 我们django项目中,uwsgi起的服务
location / { # 这个location就和咱们Django的url(r'^admin/', admin.site.urls),
include uwsgi_params; # 导入一个Nginx模块他是用来和uWSGI进行通讯的
uwsgi_connect_timeout 30; # 设置连接uWSGI超时时间
# 指定uwsgi的sock文件所有动态请求就会直接丢给它
uwsgi_pass django; # 如果上面写别名了,那么,这里还可以直接使用别名
# uwsgi_pass 127.0.0.1:8888; # 如果上面没有写别名,那么这里可以直接写 url
# 这个配置和uwsgi中绑定的一致,这里直接写127,这样可以之通过127,也就是这个代理进行访问
}
}
# 修改的配置内容 -->结束
启动服务
-
启动 uwsgi
# 直接uwsgi 后面跟 uwsgi 文件的路径 uwsgi uwsgi.ini
-
启动 nginx
# 我使用的系统是 CentOs7.4,所以使用了 systemctl 命令启动 systemctl start nginx