CentOS7配置使用Django(Nginx方式)

本文以gunicorn方式配置,参考文章1文章2

我们Django工程是ourcase,在/var/www/test 下

建立一个gunicorn_start.sh

#!/bin/bash

NAME="ourcase"                              #Name of the application (*)
DJANGODIR=/var/www/test/ourcase             # Django project directory (*)
SOCKFILE=/var/www/test/run/gunicorn.sock        # we will communicate using this unix socket (*)
#USER=nginx                                        # the user to run as (*)
#GROUP=webdata                                     # the group to run as (*)
NUM_WORKERS=1                                     # how many worker processes should Gunicorn spawn (*)
DJANGO_SETTINGS_MODULE=ourcase.settings             # which settings file should Django use (*)
DJANGO_WSGI_MODULE=ourcase.wsgi                     # WSGI module name (*)

echo "Starting $NAME as `whoami`"

# Activate the virtual environment
cd $DJANGODIR
source /var/www/test/venv/bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH

# Create the run directory if it doesn't exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR

# Start your Django Unicorn
# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
exec /var/www/test/venv/bin/gunicorn ${DJANGO_WSGI_MODULE}:application 
  --name $NAME 
  --workers $NUM_WORKERS 
  --user $USER 
  --bind=127.0.0.1:8000

创建/usr/lib/systemd/system/gunicorn_ourcase.service文件

内容为

[Unit]
Description=Ourcase gunicorn daemon

[Service]
Type=simple
User=nginx
ExecStart=/var/www/test/gunicorn_start.sh

[Install]
WantedBy=multi-user.target

 whereis nginx

找到nginx配置的位置/etc/nginx

nano /etc/nginx/conf.d/default.conf

server {
    listen     80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
       proxy_pass http://127.0.0.1:8000;
      proxy_set_header Host $host;
        proxy_set_header X-Real-IP $http_x_real_ip;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   }
}

安装:注意防火墙的设置

CentOS 64
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum install nginx
//启动停止
whereis nginx
找到位置
service nginx start
/usr/sbin/nginx -s stop//reload
/usr/sbin/nginx -t //测试配置文件
service nginx status//当前状态

开机自启动chkconfig nginx on
添加端口号:
永久性添加只需要下面2条命令之一:
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --permanent --add-service=http
重新载入,即可
firewall-cmd --reload

对应的查询和删除命令:
firewall-cmd --query-port=80/tcp
查看哪些端口打开了firewall-cmd --zone=public --list-ports
查看哪些服务打开了:firewall-cmd --list-services ,查看哪些服务可以打开firewall-cmd --get-services

firewall-cmd --zone=public --remove-port=80/tcp --permanent
firewall-cmd --remove-service=http --zone=public --permanent

service firewalld //start stop status enable disable restart


Python3
wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tgz
 ./configure --prefix=/home/python3
make
make install //需要zlib,yum install zlib zlib-devel
ln -s /home/python3/bin/python3 /usr/bin/python3

安装一堆包yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel

wget --no-check-certificate  https://pypi.python.org/packages/source/s/setuptools/setuptools-19.6.tar.gz#md5=c607dd118eae682c44ed146367a17e26

tar -zxvf setuptools-19.6.tar.gz

cd setuptools-19.6.tar.gz

python3 setup.py build

python3 setup.py install

wget --no-check-certificate  https://pypi.python.org/packages/source/p/pip/pip-8.0.2.tar.gz#md5=3a73c4188f8dbad6a1e6f6d44d117eeb

tar -zxvf pip-8.0.2.tar.gz

cd pip-8.0.2

python3 setup.py build

python3 setup.py install

ln -s /home/python3/bin/pip3 /usr/bin/pip3


Mysql
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
chkconfig mysqld on
service mysqld start

设置密码
grep 'temporary password' /var/log/mysqld.log 
找寻密码,如果没有直接mysql -uroot进入控制
set password for root@localhost = password('jsty123456789A#')

create database django DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci

yum install python-devel
 pip3 install uwsgi
测试
/home/python3/bin/uwsgi --http :80 --chdir /home/go/mysite/ -w mysite.wsgi


安装python2下面的supervisor
先位python2安装pip
yum -y install epel-release
yum install python-pip
pip install supervisor


//python3 gunicorn virtualEnv
pip3 install virtualenv
/home/python3/bin/virtualenv /var/www/test
使用/var/www/test/bin/pip install
django,pymysql gunicorn ...

综合参考

http://11hcw.me/setting-up-django-and-cloud-server-with-uwsgi-and-nginx/

https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-uwsgi-and-nginx-on-centos-7

https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html
原文地址:https://www.cnblogs.com/legion/p/7170755.html