1. 程式人生 > >linux環境,從零開始搭建nginx+uwsgi+django環境(二)

linux環境,從零開始搭建nginx+uwsgi+django環境(二)

第四步 安裝nginx及mariadb

       4.1、安裝nginx

               yum install nginx

              或從原始碼安裝:

                    執行下面的指令:

                             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 程式的主目錄
chdir = /rms/src/Resource_Management_System

# Django's wsgi file
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;

    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/ {
    		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 / {
    		root html;
    		index index.html index.html;
		try_files $uri $uri/ /index.html;
	}

        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.ini

                 使用ps -aux|grep uwsgi命令檢視執行情況:

[[email protected] 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>
[[email protected] 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 也是可以的(這種方法我只在調測階段用過,部署的時候還是要考慮高併發的情況)