一. 系统环境配置
1.关闭iptables和selinux
su - root
service iptables stop
setenforce 0
vi /etc/sysconfig/selinux
修改
SELINUX=disabled
2.添加本地host DNS
# vi /etc/hosts
127.0.0.1 django.example.com
二. Python配置
1.安装python3.6.5源及依赖包
# yum install epel-release -y
# yum groupinstall "Development tools" -y
# yum install zlib-devel bzip2-devel openssl-devel ncurses-devel zx-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel -y
2.编译安装python3.6.5以及pip package manager
# wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tar.xz --no-check-certificate
# tar xf Python-3.6.5.tar.xz
# cd Python-3.6.5
# ./configure --prefix=/usr/local --with-ensurepip=install --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
# make && make altinstall
3.安装virtualenv
# pip3.6 install --upgrade pip
# pip3.6 install virtualenv
三. Nginx配置
1. 安装nginx package
# yum install nginx -y
2.配置nginx with nWSGI
# vi /etc/nginx/conf.d/django.conf
server {
listen 80;
server_name django.example.com;
charset utf-8;
access_log /var/log/nginx/django_access.log;
error_log /var/log/nginx/django_error.log;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /usr/share/nginx/html/django.example.com;
}
client_max_body_size 20M;
location / {
include uwsgi_params;
uwsgi_pass unix:/etc/uwsgi/uwsgi-django.sock;
uwsgi_read_timeout 30s;
uwsgi_send_timeout 30s;
}
}
四. Django+uWSGI配置
1. uWSGI配置
# mkdir -p /etc/uwsgi
# vi /etc/uwsgi/uwsgi-django.ini
[uwsgi]
project = django.example.com
base = /data/www
chdir = %(base)/%(project)
home = %(base)/%(project)/.py3env
module = myproject.wsgi:application
pidfile = /tmp/uwsgi-master-django.pid
master = true
processes = 2
enable-threads = true
# use unix socket because it is more secure and faster than TCP socket
socket = /etc/uwsgi/uwsgi-django.sock
chmod-socket = 660
uid = nginx
gid = nginx
vacuum = true
die-on-term = true
logto = /var/log/nginx/uwsgi-django.log
2. 配置Django base folder
# cd /usr/share/nginx/html
# mkdir django.example.com
# cd django.example.com
# virtualenv -p /usr/local/bin/python3 .py3env
3. 开启virtualenv python3环境
# source .py3env/bin/activate
4. 在此环境安装Django相关模块
# pip install django uwsgi PyMySQL
5. 创建uWSGI启动脚本
# mkdir -p /etc/uwsgi/bin
# vi /etc/systemd/system/uwsgi-django.service
[Unit]
Description=uWSGI instance to serve myproject
[Service]
BASE=/data/www/django.example.com
ENV=$BASE/.py3env
ExecStartPre=-/usr/bin/bash -c 'chown -R nginx:nginx /etc/uwsgi'
ExecStart=/usr/bin/bash -c 'source /usr/share/nginx/html/django.example.com/.py3env/bin/activate; uwsgi --ini /etc/uwsgi/uwsgi-django.ini'
[Install]
WantedBy=multi-user.target
五. Django项目配置
1. 保证virtualenv python3环境开启
# cd /usr/share/nginx/html/django.example.com/
# source .py3env/bin/activate
2.创建一个Django项目
# django-admin startproject myproject .
3.添加static目录
# vi myproject/settings.py
末行添加:
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
4.创建本地SQLlite文件
Tip:这里使用SQLlite代替其他数据库作为我们项目的DB
# ./manage.py makemigrations
# ./manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying sessions.0001_initial... OK
5.创建项目管理员账户
# ./manage.py createsuperuser
Username (leave blank to use 'root'): root
Email address: admin@admin.com
Password:
Password (again):
Superuser created successfully.
6.生成项目静态文件目录
# ./manage.py collectstatic
7.修改wsgi入口文件
# vi myproject/wsgi.py
import os
import sys
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
sys.path.append('/usr/share/nginx/html/django.example.com')
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
8.添加ALLOWED_HOSTS
# vi myproject/settings.py
Update:
ALLOWED_HOSTS = ['django.example.com']
9. 修改权限(可执行并保持与nginx启动user一致)
# chmod -R 755 /etc/uwsgi
# chown -R nginx:nginx /etc/uwsgi
# chmod -R 755 /usr/share/nginx/html/django.example.com
# chown -R nginx:nginx /usr/share/nginx/html/django.example.com
10.启动nginx+uwsgi
# systemctl restart nginx
# systemctl restart uwsgi-django
python manage.py runserver 来运行服务器,但这只适用测试环境中使用。
正式发布的服务,我们需要一个可以稳定而持续的服务器,比如apache, Nginx, lighttpd等,本文将以 Nginx 为例。
安装基础开发包
Centos 下安装步骤如下:
yum groupinstall "Development tools"
yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel
"Development tools"
yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel
CentOS 自带 Python 2.4.3,但我们可以再安装Python2.7.5:
cd ~
wget http://python.org/ftp/python/2.7.5/Python-2.7.5.tar.bz2
tar xvf Python-2.7.5.tar.bz2
cd Python-2.7.5
./configure --prefix=/usr/local
make && make altinstall
~
wget http://python.org/ftp/python/2.7.5/Python-2.7.5.tar.bz2
tar xvf Python-2.7.5.tar.bz2
cd Python-2.7.5
./configure --prefix=/usr/local
make && make altinstall
安装Python包管理
easy_install 包 https://pypi.python.org/pypi/distribute
安装步骤:
cd ~
wget https://pypi.python.org/packages/source/d/distribute/distribute-0.6.49.tar.gz
tar xf distribute-0.6.49.tar.gz
cd distribute-0.6.49
python2.7 setup.py install
easy_install --version
~
wget https://pypi.python.org/packages/source/d/distribute/distribute-0.6.49.tar.gz
tar xf distribute-0.6.49.tar.gz
cd distribute-0.6.49
python2.7 setup.py install
easy_install --version
pip 包: https://pypi.python.org/pypi/pip
安装 pip 的好处是可以用 pip list、pip uninstall 管理 Python 包, easy_install 没有这个功能,只有 uninstall。
安装 uwsgi
uwsgi:https://pypi.python.org/pypi/uWSGI
uwsgi 参数详解:http://uwsgi-docs.readthedocs.org/en/latest/Options.html
pip install uwsgi
uwsgi --version # 查看 uwsgi 版本
--version # 查看 uwsgi 版本
测试 uwsgi 是否正常:
新建 test.py 文件,内容如下:
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return "Hello World"
然后在终端运行:
uwsgi --http :8001 --wsgi-file test.py
--http :8001 --wsgi-file test.py
在浏览器内输入:http://127.0.0.1:8001,查看是否有"Hello World"输出,若没有输出,请检查你的安装过程。
安装 Django
pip install django
测试 django 是否正常,运行:
django-admin.py startproject demosite
cd demosite
python2.7 manage.py runserver 0.0.0.0:8002
-admin.py startproject demosite
cd demosite
python2.7 manage.py runserver 0.0.0.0:8002
在浏览器内输入:http://127.0.0.1:8002,检查django是否运行正常。
安装 Nginx
安装命令如下:
cd ~
wget http://nginx.org/download/nginx-1.5.6.tar.gz
tar xf nginx-1.5.6.tar.gz
cd nginx-1.5.6
./configure --prefix=/usr/local/nginx-1.5.6 \
--with-http_stub_status_module \
--with-http_gzip_static_module
make && make install
~
wget http://nginx.org/download/nginx-1.5.6.tar.gz
tar xf nginx-1.5.6.tar.gz
cd nginx-1.5.6
./configure --prefix=/usr/local/nginx-1.5.6 \
--with-http_stub_status_module \
--with-http_gzip_static_module
make && make install
uwsgi 配置
uwsgi支持ini、xml等多种配置方式,本文以 ini 为例,在/ect/目录下新建uwsgi9090.ini,添加如下配置:
[uwsgi]
socket = 127.0.0.1:9090
master = true //主进程
vhost = true //多站模式
no-site = true //多站模式时不设置入口模块和文件
workers = 2 //子进程数
reload-mercy = 10
vacuum = true //退出、重启时清理文件
max-requests = 1000
limit-as = 512
buffer-size = 30000
pidfile = /var/run/uwsgi9090.pid //pid文件,用于下面的脚本启动、停止该进程
daemonize = /website/uwsgi9090.log
uwsgi]
socket = 127.0.0.1:9090
master = true //主进程
vhost = true //多站模式
no-site = true //多站模式时不设置入口模块和文件
workers = 2 //子进程数
reload-mercy = 10
vacuum = true //退出、重启时清理文件
max-requests = 1000
limit-as = 512
buffer-size = 30000
pidfile = /var/run/uwsgi9090.pid //pid文件,用于下面的脚本启动、停止该进程
daemonize = /website/uwsgi9090.log
Nginx 配置
找到nginx的安装目录(如:/usr/local/nginx/),打开conf/nginx.conf文件,修改server配置:
server {
listen 80;
server_name localhost;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:9090; //必须和uwsgi中的设置一致
uwsgi_param UWSGI_SCRIPT demosite.wsgi; //入口文件,即wsgi.py相对于项目根目录的位置,“.”相当于一层目录
uwsgi_param UWSGI_CHDIR /demosite; //项目根目录
index index.html index.htm;
client_max_body_size 35m;
}
}
{
listen 80;
server_name localhost;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:9090; //必须和uwsgi中的设置一致
uwsgi_param UWSGI_SCRIPT demosite.wsgi; //入口文件,即wsgi.py相对于项目根目录的位置,“.”相当于一层目录
uwsgi_param UWSGI_CHDIR /demosite; //项目根目录
index index.html index.htm;
client_max_body_size 35m;
}
}
设置完成后,在终端运行:
uwsgi --ini /etc/uwsgi9090.ini &
/usr/local/nginx/sbin/nginx
--ini /etc/uwsgi9090.ini &
/usr/local/nginx/sbin/nginx
在浏览器输入:http://127.0.0.1,你就可以看到 django 的 "It work" 了。