AlmaLinux

20230224-Alma Linux 8环境部署Django项

2023-02-23  本文已影响0人  負笈在线

1.基础环境

OS:AlmaLinux release 8.6 (Sky Tiger)
python:Python 3.9.13
nginx:nginx/1.14.1
uwsgi:uWSGI 2.0.21 (64bit)
Django:Django==3.2.16

2.python环境准备

python安装

# yum install python39 -y
# whereis python
python: /usr/bin/python3.9 /usr/lib/python3.6 /usr/lib/python3.9 /usr/lib64/python3.6 /usr/lib64/python3.9 /usr/include/python3.6m /usr/include/python3.9 /usr/share/man/man1/python.1.gz
# ln -s /usr/bin/python3.9 /usr/bin/python
# whereis pip
pip: /usr/bin/pip3.9
# ln -s /usr/bin/pip3.9 /usr/bin/pip
# python -V
Python 3.9.13

3.nginx部署

nginx安装

# yum install nginx -y
# nginx -v
nginx version: nginx/1.14.1

nginx启动及自启动

  # systemctl start nginx
  # systemctl status nginx
  # systemctl enable nginx

4.uwsgi部署

uwsgi安装

# yum install python39-devel
# pip install uwsgi
# whereis uwsgi
uwsgi: /usr/local/bin/uwsgi
# uwsgi -version
*** Starting uWSGI 2.0.21 (64bit) on [Thu Feb 23 17:28:15 2023] ***

5.上传源码并安装项目模块

上传源码

# ll local_cmdb.zip 
-rw-r--r-- 1 root root 3442542 Feb 23 18:22 local_cmdb.zip
# unzip local_cmdb.zip
# cd local_cmdb

pip安装项目依赖模块

# rpm -ivh mysql-community-devel-8.0.31-1.el8.x86_64.rpm
### 安装mysqlclient之前必须的组件
# cat requirements.txt 
asgiref==3.4.1
beautifulsoup4==4.11.1
bs4==0.0.1
certifi==2022.12.7
charset-normalizer==2.0.12
commonmark==0.9.1
dataclasses==0.8
Django==3.2.16
idna==3.4
mysqlclient==2.0.0
pip-search==0.0.12
Pygments==2.14.0
pytz==2022.7
requests==2.27.1
rich==12.6.0
simpleui==4.0.2
soupsieve==2.3.2.post1
sqlparse==0.4.3
typing_extensions==4.1.1
urllib3==1.26.13

# pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
# pip install mysqlclient -i https://pypi.tuna.tsinghua.edu.cn/simple
### 可以选择指定源-i https://pypi.tuna.tsinghua.edu.cn/simple

6.创建数据库并导入django项目数据

创建数据库

# mysql -u root -p
>create database local_cmdb;

导入数据库:

# mysql -uroot -p123456 local_cmdb <local_cmdb.sql

7.尝试启动django项目

启动项目

# python manage.py runserver
# netstat -ant |grep 8000
tcp        0      0 127.0.0.1:8000          0.0.0.0:*               LISTEN

8.uwsgi配置及启动

在settings.py所在目录新建django项目配置文件,名称为项目名称.ini

# cd /root/local_cmdb/local_cmdb
# vi local_cmdb.ini
[uwsgi]
master=true
chdir=/root/local_cmdb      ##项目路径
module=local_cmdb.wsgi      ##项目名称.wsgi
py-autoreload=1
lazy-apps=true  
socket=127.0.0.1:8000
processes=4
buffer-size=32768
daemonize=uwsgi.log
log-maxsize = 5000000
vacuum = true
disable-logging = true

检查settings.py配置并收集项目静态文件

# cat settings.py
...
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATICFILES_DIRS = [
  os.path.join(BASE_DIR, '../static'),
]
...

收集静态文件

# cd /root/local_cmdb
# python manage.py collectstatic
...
0 static files copied to '/root/local_cmdb/static', 567 unmodified.

启动uwsgi

# cd /root/local_cmdb/local_cmdb
启动uwsgi
# uwsgi -i local_cmdb.ini
查看uwsgi进程
# ps -ef|grep uwsgi
确认uwsgi端口
# netstat -ant |grep 8000
查看uwsgi日志
# tail -f  uwsgi.log

9.配置nginx

配置nginx

# cp -p /etc/nginx/nginx.conf /etc/nginx/nginx.conf.org
# cat /dev/null /etc/nginx/nginx.conf
# vi /etc/nginx/nginx.conf
user root;           ##建议root启动,否则可能部分web页面权限不足
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/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;
    
    include /etc/nginx/conf.d/*.conf;
    
    server {
        listen       81;
        charset utf-8;
        location /static {
               alias /root/local_cmdb/static/;
        }

        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 {
        }
    }
}

重启nginx

# systemctl restart nginx.service
# systemctl status nginx.service

访问页面
http://172.26.37.127:81/
http://172.26.37.127:81/admin

10.配置uwsgi开启自启动

创建配置

# vi /etc/systemd/system/server_uwsgi.service
[Unit]
Description=HTTP Interface Server
After=syslog.target
 
[Service]
KillSignal=SIGQUIT
ExecStart=/usr/local/bin/uwsgi --ini /root/local_cmdb/local_cmdb/local_cmdb.ini  
Restart=always
Type=notify
NotifyAccess=all
StandardError=syslog
 
[Install]
WantedBy=multi-user.target

修改/root/local_cmdb/local_cmdb/local_cmdb.ini 文件,删除daemonize=uwsgi.log行配置

[uwsgi]
master=true
chdir=/root/local_cmdb
module=local_cmdb.wsgi
py-autoreload=1
lazy-apps=true  
socket=127.0.0.1:8000
processes=4
buffer-size=32768
#daemonize=uwsgi.log
log-maxsize = 5000000
vacuum = true
disable-logging = true

将该服务加入到systemd中

# systemctl enable /etc/systemd/system/server_uwsgi.service

关闭uwsgi服务

# systemctl stop server_uwsgi.service

开启uwsgi服务

# systemctl start server_uwsgi.service 

重启uwsgi服务

# systemctl restart server_uwsgi.service 

参考URL

https://blog.csdn.net/qq_41905051/article/details/125181364
https://blog.csdn.net/sinat_41292836/article/details/103764842

上一篇下一篇

猜你喜欢

热点阅读