【Ubuntu18.04】部署Python+Django+uwsgi+Nginx

部署Python+Django+uwsgi+Nginx

关于uwsgi

uWSGI实现了WSGI的所有接口,是一个快速、自我修复、开发人员和系统管理员友好的服务器。uWSGI代码完全用C编写,效率高、性能稳定。

uwsgi是一种线路协议而不是通信协议,在此常用于在uWSGI服务器与其他网络服务器的数据通信。uwsgi协议是一个uWSGI服务器自有的协议,它用于定义传输信息的类型。
 

安装Python

https://blog.csdn.net/lengyer/article/details/117279877?spm=1001.2014.3001.5501

安装Nginx

apt-get update
apt-get install nginx

Nginx配置文件

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

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

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/test_access.log;
        error_log /var/log/nginx/test_error.log;

        ##
        # Gzip Settings
        ##

        gzip on;

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Virtual Host Configs
        ##
        server {
                  listen 8088;
                  server_name 127.0.0.1
                 #listen 80 default_server;
                 #listen [::]:80 default_server;

                 # SSL configuration
                 #
                 # listen 443 ssl default_server;
                 # listen [::]:443 ssl default_server;
                 #
                 # Note: You should disable gzip for SSL traffic.
                 # See: https://bugs.debian.org/773332
                 #
                 # Read up on ssl_ciphers to ensure a secure configuration.
                 # See: https://bugs.debian.org/765782
                 #
                 # Self signed certs generated by the ssl-cert package
                 # Don't use them in a production server!
                 #
                 # include snippets/snakeoil.conf;

                 ##root /var/www/html;

                 # Add index.php to the list if you are using PHP
                 index index.html index.htm index.nginx-debian.html;


                 location / {
                         include uwsgi_params;
                         uwsgi_pass 127.0.0.1:8000;
                         uwsgi_read_timeout 2;
                          }
                 location /static {
                         alias /static;
                                }
                }

验证和重启nginx的命令

nginx -t
nginx -s reload

安装uwsgi

python3.9 -m pip install uwsgi

创建一个test.py文件用uwsgi运行该文件测试

mkdir /var/test
vim /var/test/test.py

def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"]

通过uwsgi运行该文件

uwsgi --http :8001 --wsgi-file test.py

查看是否能够正常运行

http://127.0.0.1:8001

安装Django

python3.9 -m pip install django

mkdir /var/tests

django-admin startproject tests   #创建项目

写配置文件

vim /var/tests/tests_uwsgi.ini

# myweb_uwsgi.ini file
[uwsgi]

# Django-related settings

socket = :8000

# the base directory (full path)
chdir           = /var/tests

# Django s wsgi file
module          = tests.wsgi

# process-related settings
# master
master          = true

# maximum number of worker processes
processes       = 4

# ... with appropriate permissions - may be needed
# chmod-socket    = 664
# clear environment on exit
vacuum          = true
vim /var/tests/tests/settings.py

添加:
ALLOWED_HOSTS = ["192.168.1.231", "192.168.1.112"]  #不加,会报没有访问的权限,加你的服务器ip

STATIC_ROOT = '/static'    #静态文件

修改:

DATABASES = {
            'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'test',
            'USER': 'root',
            'PASSWORD': '123456',
            'HOST': 'localhost',    ##或者数据库服务器的ip地址
            'PORT': '3306',
                                                                    }
            }

本地的mysql,记得要先在mysql上面创建数据建库,名字:test
vim /var/tests/tests/models.py

from django.db import models
  
class Students(models.Model):
        sname    = models.CharField(max_length=20)
        sgender  = models.BooleanField(default=True)
        sage     = models.IntegerField()
        scontend = models.CharField(max_length=20)
        isDelete = models.BooleanField(default=False)

连接Django和uwsgi

uwsgi --http :8001 --chdir /var/tests/ --wsgi-file tests/wsgi.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191

常用选项:

http : 协议类型和端口号

processes : 开启的进程数量

workers : 开启的进程数量,等同于processes(官网的说法是spawn the specified number ofworkers / processes)

chdir : 指定运行目录(chdir to specified directory before apps loading)

wsgi-file : 载入wsgi-file(load .wsgi file)

stats : 在指定的地址上,开启状态服务(enable the stats server on the specified address)

threads : 运行线程。由于GIL的存在,我觉得这个真心没啥用。(run each worker in prethreaded mode with the specified number of threads)

master : 允许主进程存在(enable master process)

daemonize : 使进程在后台运行,并将日志打到指定的日志文件或者udp服务器(daemonize uWSGI)。实际上最常用的,还是把运行记录输出到一个本地文件上。

pidfile : 指定pid文件的位置,记录主进程的pid号。

vacuum : 当服务器退出的时候自动清理环境,删除unix socket文件和pid文件(try to remove all of the generated file/sockets)

uwsgi --ini tests_uwsgi.ini  #启动项目

没有报错就是正常

最后重启nginx

http://127.0.0.1:8099/

如果http://127.0.0.1:8099/admin没有css样式

python3.9 manage.py collectstatic

重启 nginx

参考:

https://www.cnblogs.com/fnng/p/5268633.html

猜你喜欢

转载自blog.csdn.net/lengyer/article/details/117294113