将Django项目发布到网上
1.测试外网是否可以访问静态页面
# 1. 查看ngixn路径 whereis nginx # 2. 检查nginx端口是否启动 netstat -atunp #80有表示ngxin启动了! #3. 启动 cd /usr/local/nginx/sbin ./nginx #启动 ./nginx -s reload/stop #重启/停止 #4. 本地测试nginx是成功 curl 127.0.0.1:80 #5. 阿里云服务器后台打开80端口 入口/出口方向 #6. 浏览器通过IP访问 http://阿里云服务器域名:80 http://39.98.39.17:80 #成功
**2.项目根目录添加uwsgi.ini配置文件日志 **
nginx.ini文件
#添加配置选择 [uwsgi] #配置和nginx连接的socket连接 socket=127.0.0.1:8000 #配置项目路径,项目的所在目录 chdir=/data/wwwroot/web01/ #配置wsgi接口模块文件路径,也就是wsgi.py这个文件所在的目录名 wsgi-file=web01/wsgi.py #配置启动的进程数 processes=4 #配置每个进程的线程数 threads=2 #配置启动管理主进程 master=True #配置存放主进程的进程号文件 pidfile=uwsgi.pid #配置dump日志记录 daemonize=uwsgi.log
**3. 项目上传到/data/wwwroot/下 **
cd/data/wwwroot #有专门的的上传按钮,好啦之后点击上传
4. 配置nginx.conf
cd/data/local/nginx/conf #conf为配置文件,sbin是启动指令
备份:cp nginx.conf nginx.conf.bak
删除nginx默认的内容(注释删除):
events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; server { listen 80; server_name www.django.cn; #改为自己的域名,没域名修改为127.0.0.1:80 charset utf-8; location / { include uwsgi_params; uwsgi_pass 127.0.0.1:8000; #端口要和uwsgi里配置的一样 uwsgi_param UWSGI_SCRIPT web01.wsgi; #wsgi.py所在的目录名+.wsgi uwsgi_param UWSGI_CHDIR /data/wwwroot/web01/; #项目路径 }location /static/ { alias /data/wwwroot/web01/static/; #静态资源路径 } } }
4. 测试django项目是否正常启动
cd /data/env/my_evn01/bin source activate python manage.py runserver 0.0.0.0:8000 # 测试 1. 打开浏览访问 http://IP地址:8000 #--->成功 # 测试 2: 打开新的SSH命令界面 curl 127.0.0.1:8000 # --->成功显示
5. 启动uwsgi
# 激活虚拟环境,进入项目 cd /data/env/my_env01/bin source activate cd /data/wwwroot/web01 # 启动 uwsgi --ini uwsgi.ini #启动 #显示 [uWSGI] getting INI configuration from uwsgi.ini 表明uwsgi运行成功 # uwsgi --stop uwsgi.pid 停止 # uwsgi --reload uwsgi.pid 重启
6.改nginx.conf配置文件删除原内容加入以下内容
events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; server { listen 80; server_name www.django.cn; #改为自己的域名,没域名修改为127.0.0.1:80 charset utf-8; location / { include uwsgi_params; uwsgi_pass 127.0.0.1:8000; #端口要和uwsgi里配置的一样 uwsgi_param UWSGI_SCRIPT web01.wsgi; #wsgi.py所在的目录名+.wsgi uwsgi_param UWSGI_CHDIR /data/wwwroot/web01/; #项目路径 } location /static/ { alias /data/wwwroot/web01/static/; #静态资源路径 } } }
7. 启动nginx
# 1. nginx配置文件语法正确,启动nginx cd /usr/local/nginx/sbin/ ./nginx -t # 检测配置文件语法 #nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok #nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful # 2. 启动 ./nginx #启动nginx ./nginx -s stop # 停止 ./nginx -s reload # 重启 netstat -atunp #查看80 端口是否启动了! curl 127.0.0.1:80 #本机测试访问 # 外网浏览器访问 http://172.16.44.40

浙公网安备 33010602011771号