项目上线之——配置和启动Nginx

it2026-04-17  1

在本地文件中新加了配置

将代码push到远端仓库后,首先将服务器上的uwsgi停掉

(venv) [root teamwork]# pkill -9 uwsgi

进入到项目代码所在的文件夹

(venv) [root code]# cd teamwork/

从远端将代码pull下来

(venv) [root teamwork]# git pull

将静态资源转移到/root/project/static目录下让Nginx来管理

(venv) [root teamwork]# python manage.py collectstatic 155 static files copied to '/root/project/static'. (venv) [root teamwork]# uwsgi --ini conf/uwsgi.ini & [2] 7096

检查是否安装了Nginx

(venv) [root static]# nginx -V

如果没有,就安装Nginx

(venv) [root static]# yum install -y nginx

修改Nginx的配置文件nginx.conf

(venv) [root static]# cd/etc/nginx

修改之前一定要先备份一份

(venv) [root nginx]# cp nginx.conf nginx.conf.bak

为了方便,不看文件中的注释和空行,可以来一个输出重定向

-v 表示反向查找E 表示正则表达式 (venv) [root nginx]# cat nginx.conf.bak | grep -vE "(^$)|(^#)" > nginx.conf

编辑配置文件

(venv) [root nginx]# vim nginx.conf # 用户 1 user root; # 自动识别进程数 2 worker_processes auto; # 错误日志 3 error_log /var/log/nginx/error.log; # 进程文件名字 4 pid /run/nginx.pid; # 其他模块配置 5 include /usr/share/nginx/modules/*.conf; # io工作模式 6 events { # epoll 多路io复用方式(默认也是) 7 use epoll; # 最大连接数 8 worker_connections 1024; 9 } # web服务器相关配置 10 http { 11 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 12 '$status $body_bytes_sent "$http_referer" ' 13 '"$http_user_agent" "$http_x_forwarded_for"'; 14 access_log /var/log/nginx/access.log main; # 以下三行是高效文件传输模式 15 sendfile on; 16 tcp_nopush on; 17 tcp_nodelay on; 18 keepalive_timeout 65; 19 types_hash_max_size 2048; 20 include /etc/nginx/mime.types; # 默认二进制文件传输模式 21 default_type application/octet-stream; 22 # Load modular configuration files from the /etc/nginx/conf.d directory. 23 # See http://nginx.org/en/docs/ngx_core_module.html#include 24 # for more information. 25 include /etc/nginx/conf.d/*.conf; 26 server { # 监听IPv4 27 listen 80 default_server; # 监听IPv6 28 listen [::]:80 default_server; # 配域名(配一个应用可以不给名字) 29 server_name _; # 配静态资源 30 location /static/{ 31 alias /root/project/static/; 32 } 33 } 34 }

启动Nginx

(venv) [root nginx]# systemctl start nginx

如此即可远程访问静态资源了

最新回复(0)