linux下搭建nginx+uwsgi+django环境(二)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lianshaohua/article/details/82744458

第四步 安装nginx及mariadb

       4.1、安装nginx

               yum install nginx

              或从源码安装:

                   下载源码,wget http://nginx.org/download/nginx-1.14.0.tar.gz

                    执行下面的指令:

                             tar -xzvf nginx-1.14.0.tar.gz

                             cd nginx-14.0
                              ./configure --prefix=/usr           
                          

checking for struct dirent.d_type ... found
checking for sysconf(_SC_NPROCESSORS_ONLN) ... found
checking for sysconf(_SC_LEVEL1_DCACHE_LINESIZE) ... found
checking for openat(), fstatat() ... found
checking for getaddrinfo() ... found
checking for PCRE library ... found
checking for PCRE JIT support ... found
checking for zlib library ... found
creating objs/Makefile

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr"
  nginx binary file: "/usr/sbin/nginx"
  nginx modules path: "/usr/modules"
  nginx configuration prefix: "/usr/conf"
  nginx configuration file: "/usr/conf/nginx.conf"
  nginx pid file: "/usr/logs/nginx.pid"
  nginx error log file: "/usr/logs/error.log"
  nginx http access log file: "/usr/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

                              make -j4 && make install
                             验证是否安装成功:
                              nginx -v      #显示如下:

nginx version: nginx/1.14.0

       4.2、安装mariadb

                      yum install mariadb mariadb-server

第五步    配置ngnix+uwsgi+django

         假设我有一个基于django的应用,工程名叫Resource_Management_System,简称rms

         rms的部署文件放在/rms/src/Resource_Management_System目录

        1、配置rms工程的uwsgi文件

             在Resource_Management_System目录(与manage.py文件同级目录)新建一个uwsgi.ini文件,内容如下:          

[uwsgi]
# 对外提供 http 服务的端口
http = :8000

#the local unix socket file than commnuincate to Nginx   用于和 nginx 进行数据交互的端口
socket = 127.0.0.1:8001

# the base directory (full path)  django 程序的主目录(包含manage.py的目录)
chdir = /rms/src/Resource_Management_System

# Django's wsgi file(相对于chdir的目录)
wsgi-file = Resource_Management_System/wsgi.py

# maximum number of worker processes
processes = 10

#thread numbers startched in each worker process
threads = 4

#monitor uwsgi status  通过该端口可以监控 uwsgi 的负载情况
stats = 127.0.0.1:9000


# clear environment on exit
vacuum          = true

# 后台运行,并输出日志
daemonize = /var/log/uwsgi.log

       2、配置nginx

# 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;
    client_body_max_size 1024m;

    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  47.106.198.xxx;                     #你的域名,或主机的外网地址,如果是本机访问,则可使用localhost
        root         /usr/share/nginx/html;              #存放html静态文件的目录。

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

	location /media  {
		root /rms/src/Resource_Management_System/media;  #存放rms静态资源的目录,如:上传的pdf文件、上传的图片、上传的视频等等(当然还要在rms的settings.py文件进行配置,详见我的博客:https://blog.csdn.net/lianshaohua/article/details/81781582
	}

	location /static {
		root /path/to/project/static;
	}

	location /ajax/ {#通过ajax访问
    		proxy_set_header Host $host;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_pass http://127.0.0.1:8000/;
	}

	location / {
    		include uwsgi_params;
            uwsgi_pass 127.0.0.1:8001;     #对应于uwsgi.ini与nginx通讯的端口
    		uwsgi_read_timeout 30;
	}

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

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

      完成nginx的配置以后,把html静态文件放到/usr/share/nginx/html目录;如果没有这个目录,则可以创建。

  3、启动uwsgi和nginx

        3.1、启动uwsgi

                进入/rms/src/Resource_Management_System目录(uwsgi所在目录),执行:

                 uwsgi  uwsgi.ini

                 使用ps -aux|grep uwsgi命令查看运行情况:

[root@localhost Resource_Management_System]# ps -aux|grep uwsgi
root      5740  0.0  0.0 112660   972 pts/4    S+   17:04   0:00 grep --color=auto uwsgi
root      6625  0.0  2.6 260844 26684 ?        S    Sep14   0:16 uwsgi uwsgi.ini
root      6628  0.0  2.5 556820 26180 ?        Sl   Sep14   0:00 uwsgi uwsgi.ini
root      6629  0.0  2.5 556820 26100 ?        Sl   Sep14   0:00 uwsgi uwsgi.ini
root      6630  0.0  2.6 556692 26484 ?        Sl   Sep14   0:00 uwsgi uwsgi.ini
root      6631  0.0  2.6 556684 26576 ?        Sl   Sep14   0:00 uwsgi uwsgi.ini
root      6632  0.0  2.5 556564 25960 ?        Sl   Sep14   0:00 uwsgi uwsgi.ini
root      6633  0.0  2.5 556564 26040 ?        Sl   Sep14   0:00 uwsgi uwsgi.ini
root      6634  0.0  2.5 556820 26136 ?        Sl   Sep14   0:00 uwsgi uwsgi.ini
root      6635  0.0  2.5 556820 26284 ?        Sl   Sep14   0:00 uwsgi uwsgi.ini
root      6636  0.0  2.5 556820 25960 ?        Sl   Sep14   0:00 uwsgi uwsgi.ini
root      6664  0.0  2.7 557524 27660 ?        Sl   Sep14   0:02 uwsgi uwsgi.ini
root      6666  0.0  2.2 261360 23180 ?        S    Sep14   0:05 uwsgi uwsgi.ini

       3.2、启动nginx

               server nginx start

第六步  测试nginx、uwsgi是否运行成功

              借助于强大的curl命令(yum install curl)来测试

             1、测试uwsgi是否能正常工作

                  在命令行执行:curl http://127.0.0.1:8000

                  如果能正常工作,则有回应信息,我的rms回应如下:

          <li>

                courseware/update/
                [name='courseware_update']

          </li>

          <li>

                trainingproject/
                [name='trainingproject_operater']

          </li>

          <li>

                trainingresource/
                [name='trainingproject_resource_operater']

          </li>

          <li>

                trainingresource/update/
                [name='trainingproject_resource_update']

          </li>

      </ol>
      <p>

        The empty path didn't match any of these.
      </p>

  </div>

  <div id="explanation">
    <p>
      You're seeing this error because you have <code>DEBUG = True</code> in
      your Django settings file. Change that to <code>False</code>, and Django
      will display a standard 404 page.
    </p>
  </div>
</body>
</html>

           如果不能正常工作,则没有回应或显示超时,不能工作的主要原因有二:

                    I、配置错误导致nginx或uwsgi不能正常启动

                    2、没有配置合理的防火墙规则,导致nginx与外网不通

            2、测试nginx是否能正常工作,

                 在命令行执行:curl http://127.0.0.1:80

                 如果能正常工作,则有回应信息,我的rms回应如下:

                trainingresource/
                [name='trainingproject_resource_operater']

          </li>

          <li>

                trainingresource/update/
                [name='trainingproject_resource_update']

          </li>

      </ol>
      <p>

        The empty path didn't match any of these.
      </p>

  </div>

  <div id="explanation">
    <p>
      You're seeing this error because you have <code>DEBUG = True</code> in
      your Django settings file. Change that to <code>False</code>, and Django
      will display a standard 404 page.
    </p>
  </div>
</body>
</html>
[root@izwz9fb67b4fasdme28f7tz nginx]# curl http://127.0.0.1:80
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>xxxxxxxx职业技术学院</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
<link rel="stylesheet" href="styles.a7c61d8e7ade073feafa.css"></head>
<body>
  <app-root></app-root>
<script type="text/javascript" src="runtime.a66f828dca56eeb90e02.js"></script><script type="text/javascript" src="polyfills.662173b507c7bb60d452.js"></script><script type="text/javascript" src="main.1e23f000d7c4dcd90a7e.js"></script></body>
</html>

至此,已经完成了nginx+uwsgi+django的配置,如果对并发性没有要求,在命令行直接运行:python manage.py runserver 127.0.0.1:8000 也是可以的(这种方法我只在调测阶段用过,部署的时候还是要考虑高并发的情况)

猜你喜欢

转载自blog.csdn.net/lianshaohua/article/details/82744458