centos6 python3 django-uwsgi-nginx 环境搭建03-之nginx

前端之家收集整理的这篇文章主要介绍了centos6 python3 django-uwsgi-nginx 环境搭建03-之nginx前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1.安装ngnix

yum install Nginx

2.启动,停止,重启命令

sudo /etc/init.d/Nginx start
sudo /etc/init.d/Nginx stop
sudo /etc/init.d/Nginx restart

3.配置Nginx

3.1创建配置文件

uwsgi_params文件拷贝到项目文件夹(和manage.py同一级目录)下。uwsgi_params文件/etc/Nginx/目录下

在当前目录下,创建myblog_ngnix.conf。内容如下:

#myblog_Nginx.conf

# the upstream component Nginx needs to connect tou
upstream django {
   # server unix:///path/to/your/mysite/mysite.sock; # for a file socket
   server 127.0.0.1:8080; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
    # the port your site will be served on
    listen      8000;
    # the domain name it will serve for
   
    server_name www.***.com  ***.***.***.***; # substitute your machine's IP address or FQDN
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    location /media  {
        alias /home/aslan/myblog/media;  # your Django project's media files - amend as required       
    }

    location /static {
        alias /home/aslan/myblog/static; # your Django project's static files - amend as required
    }

    # Finally,send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /home/aslan/myblog/myblog/uwsgi_params; # the uwsgi_params file you installed
    }
}

如果此时目录感觉有些不清楚的话,看下我当前的目录结构:

myblog
|-- db.sqlite3
|-- foobar.py
|-- manage.py
|-- media
|   `-- a.png
|-- myblog
|   |-- __init__.py
|   |-- __pycache__
|   |   |-- __init__.cpython-35.pyc
|   |   |-- settings.cpython-35.pyc
|   |   |-- urls.cpython-35.pyc
|   |   `-- wsgi.cpython-35.pyc
|   |-- logs
|   |-- settings.py
|   |-- urls.py
|   `-- wsgi.py
|-- myblog_Nginx.conf
|-- mysite.sock
|-- static
|-- use.ini
`-- uwsgi_params

创建配置文件的软链接,使ngnix能够加载这个配置文件

sudo ln -s /home/aslan/myblog/myblog_Nginx.conf /etc/Nginx/conf.d/

3.2 部署static文件

在django的setting文件中,添加下面一行内容

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

然后运行:

python manage.py collectstatic

3.3 重启Nginx

[aslan@VM_11_215_centos myblog]$ sudo /etc/init.d/Nginx restart
Stopping Nginx:                                            [  OK  ]
Starting Nginx:                                            [  OK  ]
[aslan@VM_11_215_centos myblog]$

3.4 测试ngnix的配置是否成功

在media路径下创建1.html 内容为hellworld

在浏览器中访问该文件:

说明该配置文件生效了

原文链接:https://www.f2er.com/centos/378829.html

猜你在找的CentOS相关文章