Django+Uwsgi+Nginx 的生产环境部署

1.准备条件

  • 本地正常能使用的django项目
  • linux的python环境和本地一模一样

2.uwsgi安装配置

安装uwsgi

pip install uwsgi

uwsgi测试django启动

  • 进入django项目:例如mysite
  • 命令启动测试:
uwsgi --http 0.0.0.0:8888 --file mysite/wsgi.py --static-map=/static=static

采用uwsgi配置文件启动django项目

  • 在项目同级目录建立script目录:
mkdir script
  • 进入/script目录,创建一个uwsgi.ini文件,添加以下内容
[uwsgi]
# 项目目录
chdir=/opt/mysite/
# 指定项目的application
module=mysite.wsgi:application
# 指定sock的文件路径       
socket=/opt/script/uwsgi.sock
# 进程个数       
workers=5
pidfile=/opt/script/uwsgi.pid
# 指定IP端口       
http=0.0.0.0:8080
# 指定静态文件
static-map=/static=/opt/mysite/static
# 启动uwsgi的用户名和用户组
uid=root
gid=root
# 启用主进程
master=true
# 自动移除unix Socket和pid文件当服务停止的时候
vacuum=true
# 序列化接受的内容,如果可能的话
thunder-lock=true
# 启用线程
enable-threads=true
# 设置自中断时间
harakiri=30
# 设置缓冲
post-buffering=4096
# 设置日志目录
daemonize=/opt/script/uwsgi.log
  • 启动项目:
uwsgi --ini uwsgi.ini

3.Nginx安装配置

  • 安装
yum -y install nginx
  • 找到Nginx的配置文件并打开,修改为以下内容
server {
    listen 80;
    server_name www.test.cn;
    rewrite ^(.*)$ https://${server_name}$1 permanent; 
}

server {
    listen 443;
    server_name www.test.cn;
    ssl on;
    root /opt/mysite;
    index index.html index.htm;
    ssl_certificate   cert/www.test.cn.pem;
    ssl_certificate_key  cert/www.test.cn.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    location / {
        include  uwsgi_params;
        uwsgi_connect_timeout 30;
        uwsgi_pass unix:/opt/script/uwsgi.sock;      
    }
    location /static/ {
    alias /opt/mysite/static/;
    index index.html index.htm;
    }
    location /media/ {
    alias /opt/mysite/media/;
    }
}
  • 最后一步,重启nginx
service nginx restart

Centos7下安裝Dokuwiki、Nginx

安裝 Nginx + php-fpm

rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum install nginx wget

php 要升級成 5.6

yum install epel-release
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
yum remove php-*
  • 启用 [remi-php56]
:
[remi-php56]
:
enabled=1
:
  • 安裝 php 5.6
yum install php php-fpm php-mbstring php-pdo php-gd php-xml php-mcrypt php-mysql

设定nginx

  • 网站名 - wiki.ichiayi.com
  • 网页路径 - /var/www/html
vi /etc/nginx/conf.d/default.conf

server {
  server_name wiki.ichiayi.com;
  listen 80;
  autoindex off;
  client_max_body_size 15M;
  client_body_buffer_size 128k;
  index index.html index.htm index.php doku.php;
  access_log  /var/log/nginx/wiki.ichiayi.com/access.log;
  error_log  /var/log/nginx/wiki.ichiayi.com/error.log;
  root /var/www/html;
 
  location / {
    try_files $uri $uri/ @dokuwiki;
  }
 
  location ~ ^/lib.*\.(gif|png|ico|jpg)$ {
    expires 30d;
  }
 
  location = /robots.txt  { access_log off; log_not_found off; }
  location = /favicon.ico { access_log off; log_not_found off; }
  location ~ /\.          { access_log off; log_not_found off; deny all; }
  location ~ ~$           { access_log off; log_not_found off; deny all; }
 
  location @dokuwiki {
    rewrite ^/_media/(.*) /lib/exe/fetch.php?media=$1 last;
    rewrite ^/_detail/(.*) /lib/exe/detail.php?media=$1 last;
    rewrite ^/_export/([^/]+)/(.*) /doku.php?do=export_$1&id=$2 last;
    rewrite ^/(.*) /doku.php?id=$1 last;
  }
 
  location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include /etc/nginx/fastcgi_params;
    fastcgi_param  QUERY_STRING     $query_string;
    fastcgi_param  REQUEST_METHOD   $request_method;
    fastcgi_param  CONTENT_TYPE     $content_type;
    fastcgi_param  CONTENT_LENGTH   $content_length;
    fastcgi_intercept_errors        on;
    fastcgi_ignore_client_abort     off;
    fastcgi_connect_timeout 60;
    fastcgi_send_timeout 180;
    fastcgi_read_timeout 180;
    fastcgi_buffer_size 128k;
    fastcgi_buffers 4 256k;
    fastcgi_busy_buffers_size 256k;
    fastcgi_temp_file_write_size 256k;
  }
 
  location ~ /(data|conf|bin|inc)/ {
    deny all;
  }
 
  location ~ /\.ht {
    deny  all;
  }
 
}
  • php-fpm 配置 - /etc/php-fpm.d/www.conf

安裝 Dokuwiki

mkdir -p /var/www
cd /var/www
wget https://download.dokuwiki.org/src/dokuwiki/dokuwiki-stable.tgz
tar -zxvf dokuwiki-stable.tgz
mv dokuwiki-stable.tgz wiki-stable.tgz
mv dokuwiki-* html
  • 建立目录和权限设置
mkdir -p /var/log/nginx/wiki.ichiayi.com
chown -R apache:apache /var/www/html/data
chown -R apache:apache /var/www/html/conf
  • 更改 /etc/php.ini 设定
vi /etc/php.ini

:
upload_max_filesize = 10M
:
post_max_size = 10M
:
:
[Date]
date.timezone = Asia/Taipei
:
  • 启动nginx + php-fpm
systemctl enable nginx
systemctl enable php-fpm
systemctl start php-fpm
systemctl start nginx