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
sudo ln -s /home/aslan/myblog/myblog_Nginx.conf /etc/Nginx/conf.d/
3.2 部署static文件
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
在浏览器中访问该文件:
说明该配置文件生效了