Centos:7.8(cat /etc/redhat-release查看版本号)
Python:3.9.5(python -V查看版本号)
Django:2.2.6(django-admin --version查看版本号)
Uwsgi:2.0.20(uwsgi --version查看版本号)
Nginx:1.20.1 (nginx -v查看版本号)
yum -y groupinstall "Development tools"
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel
yum -y install wget vim unzip
## 1.下载自己需要版本的python版本包
wget https://www.python.org/ftp/python/3.9.5/Python-3.9.5.tar.xz
## 2.新建python3目录
mkdir /usr/local/python3
## 3.安装Python3压缩包,进入解压目录,指定安装目录,安装Python3
tar -xvf Python-3.9.5.tar.xz
cd Python-3.9.5
./configure --prefix=/usr/local/python3
make && make install
## 4.安装Python3时,会自动安装pip,如果没有就手动安装
yum -y install python-pip
## 5.pip配置(更换pip的源)
(1)找到pip.conf并编辑
[root@devops ~]# cd ~
[root@devops ~]# mkdir .pip
[root@devops ~]# vim pip.conf
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
:wq!保存退出即可
(2) 安装依赖安装好pip
[root@devops ~]# yum -y install epel-release
[root@devops ~]# yum install python-pip
[root@devops ~]# pip install --upgrade pip
## 6.升级下pip
pip install --upgrade pip
## 7.创建软链接
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
## 输入python3测试是否安装成功
python -V
## 1.yum方式安装nginx
yum -y install nginx
## 2.查看nginx版本
nginx -v
## 3.启动nginx
systemctl status nginx ## 查看nginx状态
systemctl start nginx ## 开启nginx服务
systemctl stp nginx ## 关闭nginx服务
## 1.安装uwsgi
pip3 install uwsgi
## 2.建立软链接
ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi
## 3.查看uwsgi版本
uwsgi --version
## 1.上传文件到服务器
## 2.解压项目
unzaip devops.zip
## 3.pip安装项目依赖模块
pip3 install -r requirements.txt
## 4.通过python3 manage.py runserver运行一下项目,如果能正常启动则进行下一步,不能正常运行往上检查。
这里需要注意使用python3来执行manage.py,否则会出现报错情况!
(1)在settings.py所在目录vim新文件 项目名称.ini
(2)配置 项目名称.ini文件
[uwsgi]
master=true
chdir=/data(服务器实际目录)/项目名称
module=项目名称.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
(3)检查settings.py配置并收集项目静态文件
一般都是这个配置:
settings.py配置:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, '/static/')
STATICFILES_DIRS = [
os.path.join(BASE_DIR, '../bitmain_cmdb/static'), ## 实际静态文件所在项目路径,亲测好使
]
url.py配置:
from django.conf.urls import *
from django.contrib import admin
from django.conf.urls import url,include
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
url(r'^cmdb_admin/', admin.site.urls),
] + static(settings.STATIC_URL,document_root = settings.STATIC_ROOT)
执行收集静态文件命令:
python manage.py collectstatic
(4)启动测试,并查看日志
## 在当前目录启动
uwsgi -i 项目名称.ini
## 启动之后查看进程
ps -ef | grep uwsgi
启动:uwsgi -i 项目名称.ini
查看进程
vim /etc/nginx/nginx.conf
nginx.conf配置
user nginx;
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 {
include mime.types;
default_type application/octet-stream;
sendfile on;
server {
listen 80;
charset utf-8;
location /static {
alias /项目所在目录/static/;
}
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8000;
}
}
}
(4)启动nginx
## 1.配置完nginx.conf,执行 nginx -t /etc/nginx.conf 启用nginx配置文件
## 2.软加载nginx nginx -s reload
(5)访问django项目http://服务器ip/login/
之后项目重启只需要重启uwsgi之后再重启nginx即可,切记正式环境要关掉django的debug模式,防止报错源码被看到。
## 项目重启
killall -9 uwsgi
uwsgi -i 项目名称.ini
nginx -s reload
## 关闭项目debug模式
vim settings.py
debug = Fasle