Centos搭建django+nginx+uwsgi+Mysql

2018-01-09   Programming, Linux,

<!--markdown-->首先安装 PIP

cd /data;
mkdir tmp;
cd tmp;
wget https://bootstrap.pypa.io/get-pip.py;
python ./get-pip.py;

通过pip下载django

pip install Django==1.11.7

安装并启动 mariadb
由于Centos7之后的版本不在提供安装源,所以用Mariadb代替

yum install mariadb mariadb-server -y
yum install MySQL-python -y
systemctl start mariadb

进行Mariadb初始化

mysql_secure_installation

设置密码为test

mysql -uroot -ptest

创建数据库testa

create database testa;

退出

exit

创建testa

cd /data/
django-admin startproject testa

创建django数据库

cd /data/test
python manage.py migrate

启动django

python manage.py runserver

下一步:通过安装nginx

yum install nginx -y

安装uwsgi

yum install uwsgi uwsgi-plugin-python -y

修改 Nginx 配置文件
你可以在/etc/nginx/nginx.conf中找到
For example

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
            include uwsgi_params;
            uwsgi_pass 127.0.0.1:8000;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

下面重启nginx

/usr/sbin/nginx -s reload

接下来在/data/testa/目录下创建uwsgi.ini文件
加入下面代码

[uwsgi]
socket = 127.0.0.1:8000
chdir = /data/testa
wsgi-file = testa/wsgi.py
processes = 4
threads = 2
stats = 127.0.0.1:9191
uid = nobody
gid = nobody
master = true
harakiri = 30
daemonize = /data/testa/uwsgi.log
plugins = python

启动

uwsgi uwsgi.ini

大功告成,访问页面你将能看到django的欢迎

提交