实验:部署2048

上传一个2048文件夹到指定目录/home/2048
重新写一个nginx配置文件:

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


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

    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;

    keepalive_timeout  65;

    #gzip  on;

    server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /home/2048;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

}

}

接着测试配置文件是否正确:(切记一定要使用绝对路径)

[root@VM-244-7-centos ~]# nginx -t -c /home/config2048.conf
nginx: the configuration file /home/config2048.conf syntax is ok
nginx: configuration file /home/config2048.conf test is successful

测试:


可以成功访问。

为Django项目配置nginx文件

静态资源部署

刚刚了解了Nginx如何处理静态文件资源,现在配置Django项目

root需要填写项目所在路径,并且拿到外层,这样可以通用这一个目录
location匹配static静态资源文件,内部需要匹配各种静态资源,这里就可以使用nginx中的alias别名

测试:


可以访问对应的静态文件了。

动态资源部署

动态资源部署需要使用nginx的反向解析到uwsgi服务器上。

我们首先要将settings.py中的DEBUG关闭:

DEBUG = False

安装uwsgi服务器:

pip install uwsgi

在linux上安装报错:

    In file included from plugins/python/python_plugin.c:1:
    plugins/python/uwsgi_python.h:2:10: fatal error: Python.h: No such file or directory
     #include <Python.h>
              ^~~~~~~~~~
    compilation terminated.

原因是缺乏依赖环境,运行:

yum install gcc
yum install python36-devel

然后安装成功。

下面我们配置uwsgi,因为uwsgi默认是没有配置文件,所以需要手动写配置文件:

[uwsgi]
# 使用nginx连接时 使用
# socket=0.0.0.0:8888

# 直接作为web服务器使用
http=0.0.0.0:8888
# 配置工程目录
chdir=/home/PeiQi1

# 配置项目的wsgi目录。相对于工程目录
wsgi-file=PeiQi1/wsgi.py

#配置进程,线程信息
processes=4

threads=10

enable-threads=True
# 主从结构
master=True
# 进程id存储文件
pidfile=uwsgi.pid
# 日志文件
daemonize=uwsgi.log

然后启动uwsgi:

[root@VM-244-7-centos PeiQi1]# uwsgi --ini /var/www/PeiQi1/uwsgi.ini 
[uWSGI] getting INI configuration from /var/www/PeiQi1/uwsgi.ini

可以看到,生成了uwsgi的pid和log文件:

[root@VM-244-7-centos PeiQi1]# ls
alipay_config  App  config.conf  manage.py  middleware  PeiQi1  static  templates  uwsgi.ini  uwsgi.log  uwsgi.pid

查看log文件:

*** Starting uWSGI 2.0.19.1 (64bit) on [Tue Aug 18 09:23:48 2020] ***
compiled with version: 8.3.1 20191121 (Red Hat 8.3.1-5) on 18 August 2020 01:02:23
os: Linux-4.18.0-80.el8.x86_64 #1 SMP Tue Jun 4 09:19:46 UTC 2019
nodename: VM-244-7-centos
machine: x86_64
clock source: unix
detected number of CPU cores: 1
current working directory: /home/PeiQi1
writing pidfile to uwsgi.pid
detected binary path: /usr/local/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
chdir() to /home/PeiQi1
your processes number limit is 3181
your memory page size is 4096 bytes
detected max file descriptor number: 100001
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uWSGI http bound on 0.0.0.0:8888 fd 7
uwsgi socket 0 bound to TCP address 127.0.0.1:44309 (port auto-assigned) fd 6
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
Python version: 3.6.8 (default, Apr 16 2020, 01:36:27)  [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]
Python main interpreter initialized at 0x185f420
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
python threads support enabled
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 834320 bytes (814 KB) for 40 cores
*** Operational MODE: preforking+threaded ***
Traceback (most recent call last):
  File "PeiQi1/wsgi.py", line 12, in <module>
    from django.core.wsgi import get_wsgi_application
ModuleNotFoundError: No module named 'django'
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 8848)
spawned uWSGI worker 1 (pid: 8849, cores: 10)
spawned uWSGI worker 2 (pid: 8850, cores: 10)
spawned uWSGI worker 3 (pid: 8851, cores: 10)
spawned uWSGI worker 4 (pid: 8852, cores: 10)
spawned uWSGI http 1 (pid: 8853)
最后修改:2024 年 03 月 13 日
如果觉得我的文章对你有用,请随意赞赏