1.新建三台centos虚拟机
(虚拟机名称和对应IP地址请各位靓仔按照自己实际建立的虚拟机信息去修改,后面不多复述)
hdp01 | 192.168.33.128 |
hdp02 | 192.168.33.132 |
hdp03 | 192.168.33.131 |
2.安装Python3.8.2
bash
#Python-3.8.2.tgz项目提供,也可以自己下载,存放到/opt/tools
#1.解压
tar -zxvf Python-3.8.2.tgz
#安装依赖包
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel
#指定安装目录
yum install gcc
./configure --prefix=/usr/local/python3.8
#编译和安装
make && make install
#建立软连接
ln -s /usr/local/python3.8/bin/python3.8 /usr/bin/python3
ln -s /usr/local/python3.8/bin/pip3.8 /usr/bin/pip3
#测试
python3
python
3.安装虚拟环境和django
perl
#下载虚拟包
pip3 install virtualenv -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
#创建软连接
ln -s /usr/local/python3.8/bin/virtualenv /usr/bin/virtualenv
#在home目录下创建virtualenv目录,执行如下命令接环到virtualenv目录下
virtualenv -p /usr/bin/python3 env-py3.8.3
#命令执行后会创建相应目录
[root@hdp03 market]# cd /home/virtualenv/
[root@hdp03 virtualenv]# cd env-py3.8.3/
[root@hdp03 env-py3.8.3]# ll
总用量 8
drwxr-xr-x. 3 root root 4096 1月 3 12:56 bin
drwxr-xr-x. 3 root root 23 1月 2 10:54 lib
-rw-r--r--. 1 root root 230 1月 2 10:54 pyvenv.cfg
drwxr-xr-x. 3 root root 17 1月 3 12:56 share
#激活和退出虚拟环境
[root@hdp03 virtualenv]#source env-py3.8.3/bin/activate
(env-py3.8.3) [root@hdp03 env-py3.8.3]#
(env-py3.8.3) [root@hdp03 env-py3.8.3]#deactibate
[root@hdp03 virtualenv]#
#安装Django
(env-py3.8.3) [root@hdp03 env-py3.8.3]#pip3 install Django -i https://pypi.tuna.tsinghua.edu.cn/simple
4. 使用mysql数据库
python
#下载mysql5.7.30RPM包去官网
mysql-community-common-5.7.30-1.el7.x86_64.rpm
mysql-community-libs-5.7.30-1.el7.x86_64.rpm
mysql-community-client-5.7.30-1.el7.x86_64.rpm
mysql-community-server-5.7.30-1.el7.x86_64.rpm
#查找MariaDB的包文件
rpm -qa |grep mariadb
#卸载MariaDB,注意卸载顺序
rpm -ev mariadb-5.5.68-1.el7.x86_64
rpm -ev mariadb-libs-5.5.68-1.el7.x86_64
tar -xf mysql-5.7.30-1.el7.x86_64.rpm-bundle.tar -C mysql
mysql-community-common-5.7.30-1.el7.x86_64.rpm
#安装mysql相关组件
rpm -ivh mysql-community-common-5.7.30-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.30-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-5.7.30-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-5.7.30-1.el7.x86_64.rpm
#启动并查看状态
[root@hdp03 /]# cd opt
[root@hdp03 opt]# cd tools
[root@hdp03 tools]# systemctl start mysqld.service
[root@hdp03 tools]# systemctl status mysqld.service
#配置mysql
[root@hdp03 tools]# cat /var/log/mysqld.log |grep password
2024-01-02T04:48:21.387281Z 1 [Note] A temporary password is generated for root@localhost: o:YXL?J9,sLt
[root@hdp03 tools]# o:YXL?J9,sLt^C
#使用临时密码登录mysql
[root@hdp03 tools]# mysql -uroot -po:YXL?J9,sLt
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.30
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
#修改密码
mysql> alter user'root'@'localhost' IDENTIFIED BY '123456Test*'
-> ;
Query OK, 0 rows affected (0.00 sec)
#测试重新弄登录
5.开启防火墙
yaml
[root@hdp03 tools]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since 五 2023-12-29 15:46:21 CST; 3 days ago
Docs: man:firewalld(1)
Main PID: 804 (firewalld)
Tasks: 2
Memory: 1.3M
CGroup: /system.slice/firewalld.service
└─804 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid
[root@hdp03 tools]# systemctl stop firewalld
[root@hdp03 tools]# systemctl start firewalld
#开发3306端口
[root@hdp03 tools]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
success
#重启防火墙
[root@hdp03 tools]# firewall-cmd --reload
success
#使用navicate测试连接
连接名与ip地址为主机ip,用户名与密码为自己设置的mysql用户密码
#创建数据库
mysql>create database shop;
#数据库迁移
python managae.py makemigrations
python manage.py migrate
6.django项目如果用sqlite3需要数据库升级
参考这篇文章
Centos7 上的sqlite3安装及升级 - 知乎 (zhihu.com)
7 uwsgi部署
将准备好的django项目(market)
放入虚拟机/home/bu/market/下
ini
#下载项目运行配置组件
(env-py3.8.3) [root@hdp03 market]# pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple/
....
Successfully installed Flask-2.0.3 Jinja2-3.0.3 MarkupSafe-2.0.1 Pillow-8.4.0 Werkzeug-2.0.3 click-8.0.4 colorama-0.4.5 cycler-0.11.0 dataclasses-0.8 django-simpleui-2023.12.12 django-tinymce-3.6.1 django-utils-six-2.0 et-xmlfile-1.1.0 onttools-4.27.1 itsdangerous-2.0.1 kiwisolver-1.3.1 matplotlib-3.3.4 numpy-1.19.5 openpyxl-3.1.2 pandas-1.1.5 python-dateutil-2.8.2 pytz-2022.1 six-1.16.0 sql parse-0.4.3 terminaltables-3.1.10 tzdata-2022.7
#测试使用python manage.py命令运行能否成功
(env-py3.8.3) [root@hdp03 market]# python3 manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
January 03, 2024 - 12:57:08
Django version 3.2.23, using settings 'market.settings'
Starting development server at http://127.0.0.1:8000/
#在market项目下新建已改uwsgi.ini文件,文件内输入
[uwsgi]
http=0.0.0.0:8081 #端口号
chdir=/home/bu/market/ #uwsgi.ini所在位置
wsgi-file=/home/bu/market/market/wsgi.py #uwsgi.py文件所在位置
master=true
processes=1
static-map=/static=/home/bu/market/static #项目静态文件所在位置
vacuum=true
virtualenv= /home/virtualenv/env-py3.8.3/ #虚拟环境所在位置
#启动服务并测试
(env-py3.8.3) [root@hdp03 market]# uwsgi --ini /home/bu/market/uwsgi.ini
#在火狐浏览器输入127.0.0.1:8081
测试结果:
编辑
8.nginx部署
-
处理静态文件性能更好
-
更安全
-
可以进行多台机器的负载均衡
perl
#上传并解压nginx安装包
(env-py3.8.3) [root@hdp03 ~]# cd /opt/tools/
(env-py3.8.3) [root@hdp03 tools]# wget http://nginx.org/download/nginx-1.18.0.tar.gz -i https://pypi.tuna.tsinghua.edu.cn/simple/
#解压
(env-py3.8.3) [root@hdp03 tools]# tar -zxvf nginx-1.18.0.tar.gz
#安装依赖
yum -y install gcc pcre pcre-devel zlib zlib-devel
#指定nginx安装目录
(env-py3.8.3) [root@hdp03 tools]# cd nginx-1.18.0/
(env-py3.8.3) [root@hdp03 nginx-1.18.0]# ./configure --prefix=/usr/local/nginx
(env-py3.8.3) [root@hdp03 nginx-1.18.0]# make && make install
#创建软链接
(env-py3.8.3) [root@hdp03 nginx-1.18.0]# ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
#启动并测试
(env-py3.8.3) [root@hdp03 nginx-1.18.0]# nginx
#查看nginx的进程信息
(env-py3.8.3) [root@hdp03 nginx-1.18.0]# ps -ef |grep nginx
root 122269 1 0 14:46 ? 00:00:00 nginx: master process nginx
nobody 122270 122269 0 14:46 ? 00:00:00 nginx: worker process
root 122356 52859 0 14:46 pts/1 00:00:00 grep --color=auto nginx
#查看80端口是否被占用
(env-py3.8.3) [root@hdp03 nginx-1.18.0]# curl http://localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
...
#一切正常
/usr/local/nginx/conf/
填写nginx.conf上传到虚拟机/usr/local/nginx/conf/目录下。
打开/usr/local/nginx/conf/nginx.conf文件,内容如下:
ini
worker_processes 1; #开启进程数,小于或者等海域cpu数
events { #每个worker允许同时产生1024个链接
worker_connections 1024;
}
http{
include mime.types; #文件扩展名与文件类型的映射
default_type application/octet-stream; #默认文件类型
sendfile on;
keepalive_timeout 65; #链接超时时间
#配置负载均衡的服务器列表
upstream django {
server 127.0.0.1:8001;
}
#监控端口、访问域名等
server {
listen 8082; #配置监听端口
server_name localhost; #配置访问域名
charset UTF-8;
client_max_body_size 75M;
location / {
proxy_pass http://127.0.0.1:8081; #负载均衡反向代理
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_connect_timeout 600; #指定与后端应用服务器建立连接的超时时间。
proxy_read_timeout 600; #指定从后端应用服务器读取响应的超时时间。
proxy_send_timeout 600; #指定向后端应用服务器发送请求的超时时间。
proxy_buffering off; #指定是否开启代理服务器缓冲响应的功能。
}
}
}
#检测nginx.conf文件是否出错
(env-py3.8.3) [root@hdp03 market]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
#重启nginx
(env-py3.8.3) [root@hdp03 market]# nginx -s reload
#启动uwsgi
(env-py3.8.3) [root@hdp03 market]# uwsgi --ini /home/bu/market/uwsgi.ini
#在浏览器输入(uwsgi定义的端口号是8081)url:http://127.0.0.1:8081/
结果:编辑
-
#在浏览器输入(nginx代理服务器定义的端口号是8082)url:http://127.0.0.1:8082/
-
编辑
-
注意:出现504、502错误可以参考:Django 504 网关超时 uwsgi + nginx Django 应用|极客教程 (geek-docs.com)
总结
-
部署过程都是我这两天实际操作的按照步骤来绝对没问题,文章中的django项目是我学生的一个毕设项目用来做部署的,需要的可以私信我,作为新手你也可以直接自己虚拟机新建django项目去部署,通过传统部署的学习,你会发现传统的uwsgi+nginx方式非常繁琐麻烦,很多人望而却步
-
那么下一节我会分享docker一键部署,简单,高效,docker的理论知识属于云计算范围,后期我也会分享相关内容,欢迎各位靓仔,大佬关注加点赞,感谢支持!