Centos7部署Django Web 全流程之终章

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012605477/article/details/84344885

前面的章节阐述了SVN、django、python、gunicorn、nginx

其中简单的将django和gunicorn串了起来。

但是还没有完全串成一条线,下面将串起来整个项目:

1. 打开django项目的setting文件:

将Debug=True改为Debug=False

将ALLOWED_HOSTS = ["127.0.0.1"]

2.确认静态资源路径配置

# 所有静态文件的最终放置目录,

#即使用python manage.py collectstatic 命令后所有的app下面的静态资源和下图中的commonstatic文件夹中的静态资源都会存放到static目录中,这里很重要!!!!!!!!

STATIC_ROOT = os.path.join(BASE_DIR, 'static/')

# 别名,即在html中{%static  /bootstarp%}里的static经过解析后的名称。

扫描二维码关注公众号,回复: 4258428 查看本文章

#如果这里STATIC_URL =‘static’,则浏览器引用静态文件时,其路径为127.0.0.1/static/......

#如果这里为STATIC_URL = 'staticAAAAA',则浏览引用时,则为127.0.0.1/staticAAAAA/......

STATIC_URL = '/static/'

# 添加其它路径下的静态资源文件,这里的路径指的就是下面的commonstatic文件夹
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'commonstatic/').replace('\\', '/'),
)

为了让各位更清晰的了解项目的静态资源路径,下图为web工程:

注意!

注意!

注意!

如果,没有特殊情况的话,请按照上方的配置,以免出现其他问题。

3. 修改nginx的conf配置,如果可以的话,请各位直接替换conf文件中的server部分。

    server {
        listen       8000;
        server_name  localhost;
        location / {
            proxy_pass http://127.0.0.1:8080;#动态请求交给gunicorn,8080端口就是gunicorn用的端口
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            #root   html;
            #index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    location /static {
               # alias /home/app/generalWeb/commonstatic;
            alias /svn/generalWeb/static;
            }
        
    }

上面的配置中,着重强调两点:

1.nginx监听的端口改为8000,启动浏览器时,请访问8000端口,然后又让gunicorn监听的端口改为8080

2.location /static { alias /svn/generalWeb/static; } 指的是nginx启动后项目所需要的静态文件去哪里获取。

4.cd到django项目目录,执行资源归并命令:

#python manage.py collectstatic

执行完命令后,项目根目录会生产static文件夹,里面是整个工程的静态资源(包含各个app的)。

查看static里面的文件:

#cd static/

#ls

5.启动gunicorn,这里要和nginx里配置的8080端口对应:

# gunicorn -w4 -b0.0.0.0:8080 generalWeb.wsgi   项目根目录运行

6. 启动nginx (用另外一个终端运行)

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

7. 浏览器访问127.0.0.1:8000

成功!!!!!!!!

撒花!!!!!!!!

鼓掌!!!!!!!!

猜你喜欢

转载自blog.csdn.net/u012605477/article/details/84344885
今日推荐