当前位置: > CentOS > CentOS服务器 > 环境配置 >

在CentOS7上用MySQL+Nginx+Gunicorn部署Django

时间:2016-12-22 15:51来源:linux.it.net.cn 作者:IT

更新记录

2016-12-08 初稿

MySQL

安装mysql和mysql-devel

yum install mysql
yum install mysql-devel

安装mysql-server

wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum install mysql-community-server

重启mysql服务

service mysqld restart

设置root密码

初次安装mysql需要设置root密码

mysql -uroot
set password for 'root'@'localhost' =password('password');

配置mysql

/etc/my.cnf 文件结尾添加以下内容

[mysql]
default-character-set = utf8

字符编码保持和 /usr/share/mysql/charsets/Index.xml 中的一致。

远程连接设置

把在所有数据库的所有表的所有权限赋值给位于所有IP地址的root用户。

mysql> grant all privileges on *.* to root@'%'identified by 'password';

如果是新用户而不是root,则要先新建用户

mysql>create user 'username'@'%' identified by 'password';

此时就可以进行远程连接了。

Virtualenv

安装epel扩展源

yum install epel-release

安装pip

yum install python-pip

安装virtualenv和virtualenvwrapper

pip install virtualenv virtualenvwrapper

编辑 ~/.bashrc 文件,结尾添加以下内容

export WORKON_HOME=~/.virtualenvs
source /usr/bin/virtualenvwrapper.sh

然后执行以下命令使配置生效

source ~/.bashrc

创建env

mkvirtualenv explame

使用pip安装项目需要的包

WSGI

在项目目录下新建 nginx_wsgi.py 文件

touch nginx_wsgi.py

添加如下内容

import sys
import site
import os
 
# site-packages
site.addsitedir('/home/nginxuser/.virtualenvs/example/lib/python2.7/site-packages')
# Add the  project  directory
# sys.path.append('/home/nginxuser/nginxuser')
PROJECT_DIR = '/home/nginxuser/projects/example'
sys.path.insert(0, PROJECT_DIR)
os.environ['DJANGO_SETTINGS_MODULE'] = 'example.settings.prod'
# Activate your virtual env
activate_env = os.path.expanduser("/home/nginxuser/.virtualenvs/example/bin/activate_this.py")
execfile(activate_env, dict(__file__=activate_env))
 
# after activite env
from django.core.wsgi import get_wsgi_application
 
application = get_wsgi_application()

Nginx

安装

yum install nginx

检查配置是否有错

nginx -t -c /etc/nginx/nginx.conf

启动nginx

service nginx start

设置开机自启

systemctl enable nginx

创建用户

useradd nginxuser
passwd nginxuser

修改nginx主配置

vim /etc/nginx/nginx.conf

非注释首行

user nginx

改为

user nginxuser

不然可能会出现网站静态文件访问报403问题。

新建网站运行配置

mkdir /etc/nginx/site-available/example.conf
server {                                                               
    listen      80;                                                    
    server_name example.com;                            
    charset     utf-8;                                                 
    client_max_body_size 75M;                                          
    access_log /home/nginxuser/projects/example/nginxlogs/access.log;
    error_log /home/nginxuser/projects/example/nginxlogs/error.log;          
 
    location /static {                                                 
        alias /home/nginxuser/projects/explame/static;                
    }                                                                  
 
    location / {                                                       
        proxy_pass http://127.0.0.1:8000;                              
        proxy_set_header Host $host;                                   
        proxy_set_header X-Real-IP $remote_addr;                       
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;   
    }                                                                  
}                                                                       jk

建立软链接

ln -s /etc/nginx/sites-available/explame.conf /etc/nginx/sites-enabled/explame.conf

Gunicorn

安装

pip install gunicorn

项目根目录下添加gunicorn运行配置文件gunicorn.conf.py

import multiprocessing
bind = "127.0.0.1:8000"
workers = 2
errorlog = "/home/nginxuser/example/gunicorn.error.log"
#loglevel = "debug"
proc_name = "gunicorn_example"

启动

sudo gunicorn example.nginx_wsgi:application -c /home/nginxuser/projects/example/gunicorn.conf.py

后台运行

sudo nohup gunicorn example.nginx_wsgi:application -c /home/nginxuser/projects/example/gunicorn.conf.py&

如果运行报错先使用以下命令检查下nginx配置是否有错

nginx -t -c /etc/nginx/nginx.conf

(责任编辑:IT)
------分隔线----------------------------