centos6.5腾讯云django环境部署---2、Gunicorn+Django+nginx+mysql部署
接上一章:已经按照之前章节安装了Python2.7.10,Django 1.8.14. 以及mysql,并创建了数据库以及数据库帐号密码。
一、安装nginx
yum -y install nginx
#设置nginx开机启动:
chkconfig nginx on
启动nginx:service nginx start
如果安装不了或者找不到nginx的源,则手动添加文件:
/etc/yum.repos.d/nginx.repo
在里面填入:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/6/$basearch/
gpgcheck=0
enabled=1
然后再执行一次yum -y install nginx
启动报错:nginx: [emerg] socket() [::]:80 failed (97: Address family not supported by protocol)
解决办法:vim /etc/nginx/conf.d/default.conf
将: listen 80 default_server;
listen [::]:80 default_server;
改为: listen 80;
#listen [::]:80 default_server;
启动nginx就行了。。
二、配置nginx
vi /etc/nginx/nginx.conf
(修改成自己的用户账号,它默认用的是nginx用户,但我们博客的目录下访问这些是需要用户账号的,而nginx这个用户是没有权限的。所以将user后改成自己的用户账号brad。如果不做这些修改,我们后面访问静态文件会出现问题。因为nginx本来就没有权限去访问这些静态的文件。)
三、安装gunicorn
1、安装
/usr/local/bin/pip install gunicorn
2、 在xxx_project下新建gunicorn的配置文件gunicorn.conf.py
import multiprocessing bind = "127.0.0.1:8080" workers = 2 errorlog = '/home/brad/xxx_project/gunicorn.error.log' #accesslog = './gunicorn.access.log' #loglevel = 'debug' proc_name = 'gunicorn_blog_project'
3、在XXX_project下新建nginx的配置文件nginx.conf
server { listen 80; server_name localhost example.com; access_log /home/brad/xxx_project/nginx.access.log; error_log /home/brad/xxx_project/nginx.error.log; location / { proxy_pass http://127.0.0.1:8080; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /robots.txt { alias /home/brad/xxx_project/static/robots.txt; } location /favicon.ico { alias /home/brad/xxx_project/static/img/favicon.ico; } location ~ ^/(media|static)/ { root /home/brad/xxx_project; expires 30d; } # this prevents hidden files (beginning with a period) from being served location ~ /\. { access_log off; log_not_found off; deny all; } }
4、将其链接到 /etc/nginx/conf.d/blog_project.conf (需要root帐号权限或者sudo)
sudo ln -s /home/brad/xxx_project/nginx.conf /etc/nginx/conf.d/xxx_project.conf
5、同时,必须把nginx.conf里server_name后面的内容(localhost)加入到 settings.py里的ALLOWED_HOSTS
sudo vi settings.py
6、现在我们现在可以运行一下gunicorn:
sudo nohup /usr/local/python2710/bin/gunicorn xz1024_project.wsgi:application -c /home/brad/xz1024_project/gunicorn.conf.py&
注意:需要在/home/brad/xxx_project目录下面执行。
8.如果要外部访问,则打开80端口:
sudo /sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT sudo service iptatbles save