好了,我们现在有了一个完整的网站,在自己电脑上跑起来没问题了,但是我们做网站肯定不只是为了在本机上自己欣赏,总要放到网上去让别人来浏览。这一章我们就完整跑一遍Django项目的生产环境部署。
想让你的网站在公网上让别人访问,最基本的需要有以下条件:
无论是物理意义上的还是虚拟的都可以,所有程序和数据都要放在这上面
就是服务器得能上网,一般购买云服务器的话云供应商会提供,如果自建服务器的话,看网络提供商的配置
让别人可以通过网址来访问,而不是网络IP,这个要去申请、备案
网络访问的过程就是别人通过域名经过解析为IP地址来访问你的服务器,实现浏览等功能。
对个人网站来说,自己搭建物理服务器费钱费力,现在一般都是选择云服务器,我这里选择的是阿里云ECS实例,为什么选阿里云?因为我抢到了9.9三个月的优惠活动。其它什么华为云、百度云等等都可以,它们不时都会推出一些优惠活动,羊毛不薅白不薅。域名我选择的是百度域名,也是因为便宜,9.9抢了个一年域名
因为用了云服务器,就要涉及到远程控制和数据上传的问题,这里就要用到两个工具,xshell和xftp,这两个工具可以去官网申请个人免费版本,非常好用,对新手友好,强力推荐!
Django自带的服务器仅限于开发调试用,它的并发能力很弱,不能用于生产环境,这里就要用一个专门的服务器工具Nginx。Nginx("engine x")是一款是由俄罗斯的程序设计师Igor Sysoev所开发高性能的 Web和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。
Nginx 处理静态网页很优秀,但是处理动态网页就需要另一个工具uwsgi了。Uwsgi本身也是个服务器,它处理动态网页比较高效。
就选熟悉的MySQL5.7,只要把本地的数据库迁移到网上就行,可以实现线下开发调试,线上发布运行 同步。
所以整个网络部署的实际流程就是:
阿里云 云服务器ECS实例 1核(vCPU) 2 GiB
申请好以后会给一个公网IP,这个地址保存好,后面要用到
端口配置:
点击 配置安全组规则
点进去,是一个列表,在这里配置端口
一些默认的端口不要去动它,它们都是有常规功能的
比如22是给ssh的,80是HTTP即网页访问的,443是给HTTPS的,3306是给MySQL的等等
快速添加就是这些默认的端口,手动添加可以自定义任意端口
我们可以自定义几个端口,后面会用到,比如我们设一个8005端口,用于给uwsgi和Nginx的内部通信。再自定义一个1905端口,用来做测试用。注意这里端口范围一定要写成8005/8005 格式,授权对象写 0.0.0.0/0,表示对所有地址开放。
这两个是同一家公司的产品,官网https://www.xshell.com
申请一个个人免费版本,下载安装都没啥说的
xshell 用于远程连接服务器,实现在自己的电脑上操作服务器的功能。当然服务器一般都是Linux系统,需要熟悉Linux命令。
xftp 用于上传下载资源,包括上传项目文件、数据库、静态资源等等,是自己本地电脑和服务器实现数据传输的工具。
启动软件,新建一个会话
在主机这里填入从阿里云那里获得的公网IP
点击左边的用户身份验证
填入阿里云实例的用户名和密码
Tips:这里有个小坑,这里默认的用户名是root,我迷惑了很久,以为是实例名
点击连接,看到的界面:
现在就可以操作服务器了。当然,它用的是Linux系统,所以还要熟悉一些基本的Linux操作。
详细的Linux命令可以参见这篇文章
Linux命令详细总结(万字总结值得一看)_linux命令大全详解_程序员小王java的博客-CSDN博客
这里记录几个最常用的:
- ls
- 查询
-
- netstat -ntlp
- 查询网络状况
-
- ps aux | grep uwsgi
- 查询所有uwsgi进程
-
-
- nginx -s reload
- 重启nginx服务
-
- uwsgi --ini uwsgi.ini
- 以uwsgi.ini配置文件启动uwsgi
-
- pkill -f uwsgi -9
- 杀死所有uwsgi进程,这个和上面的命令结合就是重启uwsgi
-
4.2 xftp的使用
一样启动软件后,新建连接
这里端口号填22,一般是给ssh用的,主机填公网IP地址,用户名和密码跟上面xshell一样的
连接上以后看到下面的界面
这里左边是自己电脑上的文件夹,右边是服务器的文件目录,这里的上传操作就很简单了,把左边相应文件夹的文件直接拖到右边相应位置就行。(这个操作比Git之类的要简单多了,适合新手)
tips:这个东西还有个妙用,就是可以直接对右边的文件进行操作,和打开编辑,基本跟Windows下一样,这大大方便了维护,比如右边某个py文件需要修改,我直接在右边这个框里找到该文件,打开修改,然后保存关闭即可。当然还要用xshell 把 nginx 和uwsgi 服务重启一下。
这里是比较困难的部分,各个系统情况不一样,各个Python和Django的版本不一样,装起来确实挺费劲的,我这里记录一次我成功安装的过程
强调一下Linux下不要用anaconda来安装Python,之前习惯了用anaconda来做Python的底座,但是Linux系统下anaconda有很多版本的坑,我装到最后都崩溃了,所以就老老实实手动安装Python吧
参考文章:linux安装Python3 https://blog.csdn.net/SiShen654/article/details/109077772
首先找官网也好找镜像也好,先下载一个安装包,我这里用xshell远程连接服务器,直接在服务器下用Linux命令下载:
wget https://npm.taobao.org/mirrors/python/3.8.6/Python-3.8.6.tar.xz
然后用tar命令进行解压
- mkdir -p /mine/python3
- -- 在根目录的创建mine文件夹,再到下面创建python3文件夹
- -- 你也可以安装到自己想要安装的包下面,但要记得后面的命令有些要改地址
- tar xvf Python-3.8.6.tar.xz -C /mine/python3
- -- 将文件解压到-C后面的地址(你可以自定义)
然后进入刚刚解压后的目录执行编译
- cd Python-3.8.6
- ./configure --prefix=/mine/python3 // 执行配置文件
- make && make install // 进行编译安装
安装完成后可以查看一下
- cd /mine/python3
- ls
- -- 安装成功的话可以看到bin文件夹
- cd bin
- ls
- -- 可以看到运行文件都在这里,接下来创建软连接
创建软连接,类似与windows创建快捷方式
- -s后面接上刚刚查询的地址
- ===========创建===========
- ln -s /mine/python3/bin/python3.8 /usr/bin/python3
- ln -s /mine/python3/bin/pip3.8 /usr/bin/pip
- ===========查询===========
- ls -l /usr/bin/python*
- ls -l /usr/bin/pip*
- =====================================================
- 如果报错ln: failed to create symbolic link ‘/usr/bin/pip’: File exists
- 这里的python3的软连接如果被占用也一样删掉 rm -rf ./python3
- 使用下面的命令将python2的pip软连接删除:
- cd /usr/bin/
- rm -rf ./pip
tips:在Linux下运行程序和Windows下的一个 不同,你要时刻关注用的是哪个路径下的程序,否则会有很多的坑。后面说uwsgi的时候会说到,不指定路径很容易出各种错
有了Python,再安装Django就好办了,可以直接用pip 安装
pip3 install Django
当然这个过程也没那么顺利,它可能还要求安装各种环境等等,但是因为有了pip我们只要耐心一点,基本上都能装好,实在pip装不了的,可以yum install安装
如果你的项目用到了bootstap4的,还要再安装一下
pip3 install django-bootstrap4
参考文章 nginx学习,看这一篇就够了:下载、安装。使用:正向代理、反向代理、负载均衡。常用命令和配置文件,很全_java冯坚持的博客-CSDN博客
Nginx 是高性能的 HTTP 和反向代理的web服务器,处理高并发能力是十分强大的,能经受高负 载的考验,有报告表明能支持高达 50,000 个并发连接数。
其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。
Nginx 可以作为静态页面的 web 服务器,同时还支持 CGI 协议的动态语言,比如 perl、php 等。但是不支持 java。Java 程序只能通过与 tomcat 配合完成。Nginx 专为性能优化而开发, 性能是其最重要的考量,实现上非常注重效率 ,能经受高负载的考验,有报告表明能支持高 达 50,000 个并发连接数。
先安装其依赖软件,最后安装nginx。
依赖工具:pcre-8.3.7.tar.gz, openssl-1.0.1t.tar.gz, zlib-1.2.8.tar.gz, nginx-1.11.1.tar.gz。
nginx的http模块使用pcre来解析正则表达式,需要在linux上安装pcre库
nginx使用zlib对http包的内容进行gzip,需要在linux上安装安装zlib库
安装openssl库,让 nginx 支持 https(即在ssl协议上传输http)
安装都有两种方式,一种直接下载,第二种使用解压包方式。这里大多使用解压包方式。
我的安装路径:/usr/feng/
Mac系统安装请移步到这儿(和Linux安装并无多大区别):Mac os 安装 nginx 教程(success)
安装pcre
方式一、wget http://downloads.sourceforge.net/project/pcre/pcre/8.37/pcre-8.37.tar.gz 。
方拾二、上传源码压缩包,解压、编译、安装 三部曲。
1)、解压文件, 进入pcre目录,
2)、./configure 完成后,
3)、执行命令: make && make install
安装 openssl
下载OpenSSL的地址:
http://distfiles.macports.org/openssl/
1)、解压文件, 回到 openssl目录下,
2)、./configure 完成后,
3)、执行命令: make && make install
安装 zlib
1)、解压文件, 回到 zlib 目录下,
2)、./configure 完成后,
3)、执行命令: make && make install
**安装 nginx **
1)、解压文件, 回到 nginx 目录下,
2)、./configure 完成后,
3)、执行命令: make && make install
安装完nginx后,会在 路径 /usr/local 下自动生成 nginx 文件夹。这是自动生成的。
进入这个目录:
cd /usr/local/nginx
目录内容如下:
进入sbin文件夹,里面有两个文件:nginx 和 nginx.old。
执行命令:./nginx 即可执行
测试启动: ps -ef | grep nginx
已经启动。
查看nginx默认端口(默认为80),使用网页的形式测试
进入目录查看端口:cd /usr/local/nginx/conf 下的 nginx.conf文件。这个文件也是nginx的配置文件。vim 下:
输入IP:80,则显示:
/usr/local/nginx/conf/nginx.conf
配置文件中有很多#, 开头的表示注释内容,我们去掉所有以 # 开头的段落,精简之后的 内容如下:
- worker_processes 1;
-
- events {
- worker_connections 1024;
- }
-
- http {
- include mime.types;
- default_type application/octet-stream;
- sendfile on;
- keepalive_timeout 65;
-
- server {
- listen 80;
- server_name localhost;
-
- location / {
- root html;
- index index.html index.htm;
- }
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
- }
- }
总结一下,nginx 配置文件有三部分组成
第一部分:全局块
从配置文件开始到 events 块之间的内容,主要会设置一些影响nginx 服务器整体运行的配置指令,主要包括配 置运行 Nginx 服务器的用户(组)、允许生成的 worker process 数,进程 PID 存放路径、日志存放路径和类型以 及配置文件的引入等。
比如上面第一行配置的:
worker_processes 1;
这是 Nginx 服务器并发处理服务的关键配置,worker_processes 值越大,可以支持的并发处理量也越多,但是 会受到硬件、软件等设备的制约。
比如上面的配置:
- events {
- worker_connections 1024;
- }
events 块涉及的指令**主要影响 Nginx 服务器与用户的网络连接,常用的设置包括是否开启对多 work process 下的网络连接进行序列化,是否 允许同时接收多个网络连接,选取哪种事件驱动模型来处理连接请求,每个 word process 可以同时支持的最大连接数等。**
上述例子就表示每个 work process 支持的最大连接数为 1024.
这部分的配置对 Nginx 的性能影响较大,在实际中应该灵活配置。
第三部分 http块:
- http {
- include mime.types;
- default_type application/octet-stream;
- sendfile on;
- keepalive_timeout 65;
-
- server {
- listen 80;
- server_name localhost;
-
- location / {
- root html;
- index index.html index.htm;
- }
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
- }
- }
这算是 Nginx 服务器配置中最频繁的部分,代理、缓存和日志定义等绝大多数功能和第三方模块的配置都在这里。
需要注意的是:http 块也可以包括 http全局块、server 块。
http全局块
http全局块配置的指令包括文件引入、MIME-TYPE 定义、日志自定义、连接超时时间、单链接请求数上限等。
server 块
这块和虚拟主机有密切关系,虚拟主机从用户角度看,和一台独立的硬件主机是完全一样的,该技术的产生是为了 节省互联网服务器硬件成本。
每个 http 块可以包括多个 server 块,而每个 server 块就相当于一个虚拟主机。
而每个 server 块也分为全局 server 块,以及可以同时包含多个 locaton 块。
全局 server 块
最常见的配置是本虚拟机主机的监听配置和本虚拟主机的名称或IP配置。
location 块
一个 server 块可以配置多个 location 块。
这块的主要作用是基于 Nginx 服务器接收到的请求字符串(例如 server_name/uri-string),对虚拟主机名称 (也可以是IP 别名)之外的字符串(例如 前面的 /uri-string)进行匹配,对特定的请求进行处理。 地址定向、数据缓 存和应答控制等功能,还有许多第三方模块的配置也在这里进行。
如果不记得自己的Nginx装在哪里了,可以用 find -name nginx 命令找一下
- [root@iZuf6ctterqg8fllx4kb9zZ local]# find / -name nginx
- /usr/share/doc/nginx
- /usr/share/nginx
- /usr/share/licenses/nginx
- /usr/lib64/nginx
- /usr/sbin/nginx #运行
- /var/log/nginx
- /var/lib/nginx
- /etc/nginx #配置
- /etc/logrotate.d/nginx
查看网络服务命令
- [root@iZuf6ctterqg8fllx4kb9zZ ~]# netstat -ntlp
- Active Internet connections (only servers)
- Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
- tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1028/sshd
- tcp 0 0 0.0.0.0:56185 0.0.0.0:* LISTEN 1037/rpc.statd
- tcp 0 0 0.0.0.0:2049 0.0.0.0:* LISTEN -
- tcp 0 0 127.0.0.1:8005 0.0.0.0:* LISTEN 1854/uwsgi
- tcp 0 0 0.0.0.0:43367 0.0.0.0:* LISTEN -
- tcp 0 0 0.0.0.0:5355 0.0.0.0:* LISTEN 483/systemd-resolve
- tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
- tcp 0 0 0.0.0.0:20048 0.0.0.0:* LISTEN 1036/rpc.mountd
- tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 736/nginx: master p
- tcp6 0 0 :::46743 :::* LISTEN 1037/rpc.statd
- tcp6 0 0 :::46111 :::* LISTEN -
- tcp6 0 0 :::2049 :::* LISTEN -
- tcp6 0 0 :::3306 :::* LISTEN 1219/mysqld
- tcp6 0 0 :::5355 :::* LISTEN 483/systemd-resolve
- tcp6 0 0 :::111 :::* LISTEN 1/systemd
- tcp6 0 0 :::20048 :::* LISTEN 1036/rpc.mountd
- tcp6 0 0 :::80 :::* LISTEN 736/nginx: master p
nginx 常用操作命令,找到所在文件夹以后使用
- ./nginx #启动
- ./nginx -s start #启动
- ./nginx -s stop #停止
- ./nginx -s reload #重启
WSGI,全称 Web Server Gateway Interface,或者 Python Web Server Gateway Interface ,是为 Python 语言定义的 Web 服务器和 Web 应用程序或框架之间的一种简单而通用的接口。这东西是一个Gateway,也就是网关。网关的作用就是在协议之间进行转换。
WSGI 是作为 Web 服务器与 Web 应用程序或应用框架之间的一种低级别的接口,以提升可移植 Web 应用开发的共同点。WSGI 是基于现存的 CGI 标准而设计的。
uWSGI是一个Web服务器,它实现了WSGI协议、uwsgi、http等协议。Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换。
uwsgi协议是一个uWSGI服务器自有的协议,它用于定义传输信息的类型(type of information),每一个uwsgi packet前4byte为传输信息类型描述,它与WSGI相比是两样东西。
注意WSGI / uwsgi / uWSGI 这三个概念的区分。
有了pip,可以直接用pip命令安装:
pip install uwsgi
也可以下载源代码安装
- wget http://projects.unbit.it/downloads/uwsgi-latest.tar.gz
- tar zxvf uwsgi-latest.tar.gz
- cd uwsgi-latest
- make
让我们新建一个test.py文件,内容如下:
- def application(env, start_response):
- start_response('200 OK', [('Content-Type','text/html')])
- return [b"Hello World"]
uWSGI Python 加载器将会搜索的默认函数 application 。
接下来我们启动 uWSGI 来运行一个 HTTP 服务器,将程序部署在HTTP端口 8005 上:
uwsgi --http :8005 --wsgi-file test.py
默认情况下,uWSGI 启动一个单一的进程和一个单一的线程。
你可以用 --processes 选项添加更多的进程,或者使用 --threads 选项添加更多的线程 ,也可以两者同时使用。
uwsgi --http :8005 --wsgi-file test.py --master --processes 4 --threads 2
以上命令将会生成 4 个进程, 每个进程有 2 个线程。
一个uwsgi的基本配置项目如下:
- [uwsgi]
- uid=www-data # Ubuntu系统下默认用户名
- gid=www-data # Ubuntu系统下默认用户组
- project=mysite1 # 项目名
- base = /home/user1 # 项目根目录
-
-
- home = %(base)/Env/%(project) # 设置项目虚拟环境,Docker部署时不需要
- chdir=%(base)/%(project) # 设置工作目录
- module=%(project).wsgi:application # wsgi文件位置
-
-
- master=True # 主进程
- processes=2 # 同时进行的进程数,一般
-
-
-
-
- # 以下uwsgi与nginx通信手段3选一即可
-
-
-
-
- # 选项1, 使用unix socket与nginx通信,仅限于uwsgi和nginx在同一主机上情形
- # Nginx配置中uwsgi_pass应指向同一socket文件
- socket=/run/uwsgi/%(project).sock
-
-
- # 选项2,使用TCP socket与nginx通信
- # Nginx配置中uwsgi_pass应指向uWSGI服务器IP和端口
- # socket=0.0.0.0:8000 或则 socket=:8000
-
-
- # 选项3,使用http协议与nginx通信
- # Nginx配置中proxy_pass应指向uWSGI服务器一IP和端口
- # http=0.0.0.0:8000
-
-
- # socket权限设置
- chown-socket=%(uid):www-data
- chmod-socket=664
-
-
- # 进程文件
- pidfile=/tmp/%(project)-master.pid
-
-
- # 以后台守护进程运行,并将log日志存于temp文件夹。
- daemonize=/var/log/uwsgi/%(project).log
-
-
- # 服务停止时,自动移除unix socket和pid文件
- vacuum=True
-
-
- # 为每个工作进程设置请求数的上限。当处理的请求总数超过这个量,进程回收重启。
- max-requests=5000
-
-
- # 当一个请求花费的时间超过这个时间,那么这个请求都会被丢弃。
- harakiri=60
-
-
- #当一个请求被harakiri杀掉会输出一条日志
- harakiri-verbose=true
-
-
- # uWsgi默认的buffersize为4096,如果请求数据超过这个量会报错。这里设置为64k
- buffer-size=65536
-
-
- # 如果http请求体的大小超过指定的限制,打开http body缓冲,这里为64k
- post-buffering=65536
-
-
- #开启内存使用情况报告
- memory-report=true
-
-
- #设置平滑的重启(直到处理完接收到的请求)的长等待时间(秒)
- reload-mercy=10
-
-
- #设置工作进程使用虚拟内存超过多少MB就回收重启
- reload-on-as=1024
注意:uWSGI和Nginx之间有3种通信方式, unix socket,TCP socket和http。Nginx的配置必须与uwsgi配置保持一致。
配置uwsgi
touch uwsgi.ini
uwsgi守护运行
uwsgi -d --ini uwsgi.ini
杀死后台uwsgi进程
pkill -f uwsgi -9
查看后台uwsgi进程
ps aux | grep uwsgi
用xshell远程登录,在Linux下直接去官网下载源程序安装
wget http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
中间有些小困难克服一下,安装完毕
- [root@iZuf6ctterqg8fllx4kb9zZ ~]# rpm -qa | grep mysql
- [root@iZuf6ctterqg8fllx4kb9zZ ~]# wget http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
- --2023-03-21 14:23:08-- http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
- Resolving dev.mysql.com (dev.mysql.com)... 23.76.83.234, 2600:1417:e800:185::2e31, 2600:1417:e800:189::2e31
- Connecting to dev.mysql.com (dev.mysql.com)|23.76.83.234|:80... connected.
- HTTP request sent, awaiting response... 301 Moved Permanently
- Location: https://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm [following]
- --2023-03-21 14:23:08-- https://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
- Connecting to dev.mysql.com (dev.mysql.com)|23.76.83.234|:443... connected.
- HTTP request sent, awaiting response... 302 Moved Temporarily
- Location: https://repo.mysql.com//mysql57-community-release-el7-10.noarch.rpm [following]
- --2023-03-21 14:23:09-- https://repo.mysql.com//mysql57-community-release-el7-10.noarch.rpm
- Resolving repo.mysql.com (repo.mysql.com)... 23.209.108.229
- Connecting to repo.mysql.com (repo.mysql.com)|23.209.108.229|:443... connected.
- HTTP request sent, awaiting response... 200 OK
- Length: 25548 (25K) [application/x-redhat-package-manager]
- Saving to: ‘mysql57-community-release-el7-10.noarch.rpm’
-
- mysql57-community-release-el7-10.noarch.rpm 100%[=========================================================================================>] 24.95K --.-KB/s in 0s
-
- 2023-03-21 14:23:09 (364 MB/s) - ‘mysql57-community-release-el7-10.noarch.rpm’ saved [25548/25548]
-
- [root@iZuf6ctterqg8fllx4kb9zZ ~]# yum -y install mysql57-community-release-el7-10.noarch.rpm
- Last metadata expiration check: 3:04:46 ago on Tue 21 Mar 2023 11:20:02 AM CST.
- Dependencies resolved.
- ==============================================================================================================================================================================
- Package Architecture Version Repository Size
- ==============================================================================================================================================================================
- Installing:
- mysql57-community-release noarch el7-10 @commandline 25 k
-
- Transaction Summary
- ==============================================================================================================================================================================
- Install 1 Package
-
- Total size: 25 k
- Installed size: 30 k
- Downloading Packages:
- Running transaction check
- Transaction check succeeded.
- Running transaction test
- Transaction test succeeded.
- Running transaction
- Preparing : 1/1
- Installing : mysql57-community-release-el7-10.noarch 1/1
- Verifying : mysql57-community-release-el7-10.noarch 1/1
-
- Installed:
- mysql57-community-release-el7-10.noarch
-
- Complete!
- [root@iZuf6ctterqg8fllx4kb9zZ ~]# yum -y install mysql-community-server
- Last metadata expiration check: 0:00:15 ago on Tue 21 Mar 2023 02:24:59 PM CST.
- Dependencies resolved.
- ==============================================================================================================================================================================
- Package Architecture Version Repository Size
- ==============================================================================================================================================================================
- Installing:
- mysql-community-server x86_64 5.7.41-1.el7 mysql57-community 178 M
- Installing dependencies:
- libaio x86_64 0.3.112-1.2.al8 alinux3-os 33 k
- mysql-community-client x86_64 5.7.41-1.el7 mysql57-community 28 M
- mysql-community-common x86_64 5.7.41-1.el7 mysql57-community 311 k
- mysql-community-libs x86_64 5.7.41-1.el7 mysql57-community 2.6 M
-
- Transaction Summary
- ==============================================================================================================================================================================
- Install 5 Packages
-
- Total download size: 209 M
- Installed size: 895 M
- Downloading Packages:
- (1/5): libaio-0.3.112-1.2.al8.x86_64.rpm 241 kB/s | 33 kB 00:00
- (2/5): mysql-community-common-5.7.41-1.el7.x86_64.rpm 182 kB/s | 311 kB 00:01
- (3/5): mysql-community-libs-5.7.41-1.el7.x86_64.rpm 239 kB/s | 2.6 MB 00:11
- (4/5): mysql-community-client-5.7.41-1.el7.x86_64.rpm 816 kB/s | 28 MB 00:35
- (5/5): mysql-community-server-5.7.41-1.el7.x86_64.rpm 87 kB/s | 178 MB 35:01
- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- Total 102 kB/s | 209 MB 35:02
- MySQL 5.7 Community Server 27 MB/s | 27 kB 00:00
- Importing GPG key 0x5072E1F5:
- Userid : "MySQL Release Engineering
" - Fingerprint: A4A9 4068 76FC BD3C 4567 70C8 8C71 8D3B 5072 E1F5
- From : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
- Key imported successfully
- Import of key(s) didn't help, wrong key(s)?
- Public key for mysql-community-client-5.7.41-1.el7.x86_64.rpm is not installed. Failing package is: mysql-community-client-5.7.41-1.el7.x86_64
- GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
- Public key for mysql-community-common-5.7.41-1.el7.x86_64.rpm is not installed. Failing package is: mysql-community-common-5.7.41-1.el7.x86_64
- GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
- Public key for mysql-community-libs-5.7.41-1.el7.x86_64.rpm is not installed. Failing package is: mysql-community-libs-5.7.41-1.el7.x86_64
- GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
- Public key for mysql-community-server-5.7.41-1.el7.x86_64.rpm is not installed. Failing package is: mysql-community-server-5.7.41-1.el7.x86_64
- GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
- The downloaded packages were saved in cache until the next successful transaction.
- You can remove cached packages by executing 'yum clean packages'.
- Error: GPG check FAILED
- [root@iZuf6ctterqg8fllx4kb9zZ ~]# yum -y install mysql-community-server
- Last metadata expiration check: 0:40:23 ago on Tue 21 Mar 2023 02:24:59 PM CST.
- Dependencies resolved.
- ==============================================================================================================================================================================
- Package Architecture Version Repository Size
- ==============================================================================================================================================================================
- Installing:
- mysql-community-server x86_64 5.7.41-1.el7 mysql57-community 178 M
- Installing dependencies:
- libaio x86_64 0.3.112-1.2.al8 alinux3-os 33 k
- mysql-community-client x86_64 5.7.41-1.el7 mysql57-community 28 M
- mysql-community-common x86_64 5.7.41-1.el7 mysql57-community 311 k
- mysql-community-libs x86_64 5.7.41-1.el7 mysql57-community 2.6 M
- Transaction Summary
- ==============================================================================================================================================================================
- Install 5 Packages
- Total size: 209 M
- Installed size: 895 M
- Downloading Packages:
- [SKIPPED] libaio-0.3.112-1.2.al8.x86_64.rpm: Already downloaded
- [SKIPPED] mysql-community-client-5.7.41-1.el7.x86_64.rpm: Already downloaded
- [SKIPPED] mysql-community-common-5.7.41-1.el7.x86_64.rpm: Already downloaded
- [SKIPPED] mysql-community-libs-5.7.41-1.el7.x86_64.rpm: Already downloaded
- [SKIPPED] mysql-community-server-5.7.41-1.el7.x86_64.rpm: Already downloaded
- MySQL 5.7 Community Server 27 MB/s | 27 kB 00:00
- GPG key at file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql (0x5072E1F5) is already installed
- The GPG keys listed for the "MySQL 5.7 Community Server" repository are already installed but they are not correct for this package.
- Check that the correct key URLs are configured for this repository.. Failing package is: mysql-community-client-5.7.41-1.el7.x86_64
- GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
- Public key for mysql-community-common-5.7.41-1.el7.x86_64.rpm is not installed. Failing package is: mysql-community-common-5.7.41-1.el7.x86_64
- GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
- Public key for mysql-community-libs-5.7.41-1.el7.x86_64.rpm is not installed. Failing package is: mysql-community-libs-5.7.41-1.el7.x86_64
- GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
- Public key for mysql-community-server-5.7.41-1.el7.x86_64.rpm is not installed. Failing package is: mysql-community-server-5.7.41-1.el7.x86_64
- GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
- The downloaded packages were saved in cache until the next successful transaction.
- You can remove cached packages by executing 'yum clean packages'.
- Error: GPG check FAILED
- [root@iZuf6ctterqg8fllx4kb9zZ ~]# rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
- [root@iZuf6ctterqg8fllx4kb9zZ ~]# yum -y install mysql-community-server
- Last metadata expiration check: 0:41:35 ago on Tue 21 Mar 2023 02:24:59 PM CST.
- Dependencies resolved.
- ==============================================================================================================================================================================
- Package Architecture Version Repository Size
- ==============================================================================================================================================================================
- Installing:
- mysql-community-server x86_64 5.7.41-1.el7 mysql57-community 178 M
- Installing dependencies:
- libaio x86_64 0.3.112-1.2.al8 alinux3-os 33 k
- mysql-community-client x86_64 5.7.41-1.el7 mysql57-community 28 M
- mysql-community-common x86_64 5.7.41-1.el7 mysql57-community 311 k
- mysql-community-libs x86_64 5.7.41-1.el7 mysql57-community 2.6 M
- Transaction Summary
- ==============================================================================================================================================================================
- Install 5 Packages
- Total size: 209 M
- Installed size: 895 M
- Downloading Packages:
- [SKIPPED] libaio-0.3.112-1.2.al8.x86_64.rpm: Already downloaded
- [SKIPPED] mysql-community-client-5.7.41-1.el7.x86_64.rpm: Already downloaded
- [SKIPPED] mysql-community-common-5.7.41-1.el7.x86_64.rpm: Already downloaded
- [SKIPPED] mysql-community-libs-5.7.41-1.el7.x86_64.rpm: Already downloaded
- [SKIPPED] mysql-community-server-5.7.41-1.el7.x86_64.rpm: Already downloaded
- Running transaction check
- Transaction check succeeded.
- Running transaction test
- Transaction test succeeded.
- Running transaction
- Preparing : 1/1
- Installing : mysql-community-common-5.7.41-1.el7.x86_64 1/5
- Installing : mysql-community-libs-5.7.41-1.el7.x86_64 2/5
- Running scriptlet: mysql-community-libs-5.7.41-1.el7.x86_64 2/5
- Installing : mysql-community-client-5.7.41-1.el7.x86_64 3/5
- Installing : libaio-0.3.112-1.2.al8.x86_64 4/5
- Running scriptlet: mysql-community-server-5.7.41-1.el7.x86_64 5/5
- Installing : mysql-community-server-5.7.41-1.el7.x86_64 5/5
- Running scriptlet: mysql-community-server-5.7.41-1.el7.x86_64 5/5
- [/usr/lib/tmpfiles.d/mysql.conf:23] Line references path below legacy directory /var/run/, updating /var/run/mysqld → /run/mysqld; please update the tmpfiles.d/ drop-in file accordingly.
- Verifying : libaio-0.3.112-1.2.al8.x86_64 1/5
- Verifying : mysql-community-client-5.7.41-1.el7.x86_64 2/5
- Verifying : mysql-community-common-5.7.41-1.el7.x86_64 3/5
- Verifying : mysql-community-libs-5.7.41-1.el7.x86_64 4/5
- Verifying : mysql-community-server-5.7.41-1.el7.x86_64 5/5
- Installed:
- libaio-0.3.112-1.2.al8.x86_64 mysql-community-client-5.7.41-1.el7.x86_64 mysql-community-common-5.7.41-1.el7.x86_64 mysql-community-libs-5.7.41-1.el7.x86_64
- mysql-community-server-5.7.41-1.el7.x86_64
- Complete!
然后启动MySQL服务器并查看服务器状态
- [root@iZuf6ctterqg8fllx4kb9zZ ~]# systemctl status mysqld.service
- ● mysqld.service - MySQL Server
- Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: enabled)
- Active: inactive (dead)
- Docs: man:mysqld(8)
- http://dev.mysql.com/doc/refman/en/using-systemd.html
- [root@iZuf6ctterqg8fllx4kb9zZ ~]# systemctl start mysqld.service
- [root@iZuf6ctterqg8fllx4kb9zZ ~]# netstat -ntlp | grep mysql
- tcp6 0 0 :::3306 :::* LISTEN 23116/mysqld
从mysqld.log文件查看随机生成的初始密码,用root账户登录并修改密码:
- [root@iZuf6ctterqg8fllx4kb9zZ ~]# grep "password" /var/log/mysqld.log
- 2023-03-21T07:08:02.255989Z 1 [Note] A temporary password is generated for root@localhost: .TyFH/#8aot0
- [root@iZuf6ctterqg8fllx4kb9zZ ~]# mysqladmin -u root -h 127.0.0.1 -p password
- Enter password:
- New password:
- Confirm new password:
- Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
- mysqladmin: unable to change password; error: 'Your password does not satisfy the current policy requirements'
发现输入密码太简单,系统不接受,查了一下,要求8位以上字母有大小写,于是重新输入:
- [root@iZuf6ctterqg8fllx4kb9zZ ~]# mysqladmin -u root -h 127.0.0.1 -p password
- Enter password:
- New password:
- Confirm new password:
- Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
- mysqladmin: [Warning] Using a password on the command line interface can be insecure.
- Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
-
- [root@iZuf6ctterqg8fllx4kb9zZ ~]# mysql -u root -p
- Enter password:
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 8
- Server version: 5.7.41 MySQL Community Server (GPL)
-
- Copyright (c) 2000, 2023, Oracle and/or its affiliates.
-
- 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> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | mysql |
- | performance_schema |
- | sys |
- +--------------------+
- 4 rows in set (0.00 sec)
-
- mysql>
如果我们想把本地已经跑的数据放到服务器上,或者本地版本做了修改以后让服务器上数据同步,那就要用到数据迁移,步骤如下:
A)使用mysqldump指令将数据库表/数据保存成xx.sql文件存到本地,
- #导出数据到/root/目录下
- # 导出所有数据库的表结构和数据
- mysqldump -uroot -p123456 --all-databases > /root/alldata.sql
- # 导出指定数据库(user)的所有表结构和数据
- mysqldump -uroot -p123456 user > /root/user.sql
- # 导出指定数据库(user)的所有表结构(不包含数据)
- mysqldump -uroot -p123456 -d user > /root/user.sql
- # 导出指定数据库(user)某一张表(socre)的结构
- mysqldump -uroot -p123456 -d user score > /root/userscore.sql
- # 导出指定数据库(user)某一张表(score)的结构和数据
- mysqldump -uroot -p123456 user score > /root/userscoredata.sql
B)上传导出的.sql文件到服务器
这里,我们可以通xftp直接拉过去,比那些上传的方法方便多了
C) 在服务器执行.sql文件完成数据迁移
- # 登录mysql
- mysql -uroot -p
- 输入密码
- # 新建迁移保存的数据库,要和准备迁移的数据库同名
- mysql > create database user;
- mysql > use user;
- # 执行.sql文件
- mysql > source /root/user.sql;
好了,到现在为止,我们才算是做好了所有准备工作,开始真正地来部署上线了。
修改setting.py
打开你的项目文件夹中项目同名文件夹中的setting.py文件,修改其中设置:
- DEBUG = False #防止攻击者根据DEBUG信息攻击你的项目
-
- ALLOWED_HOSTS = ['*'] #允许所有端口访问项目
在文件末尾添加
STATIC_ROOT= '/home/mylog/static/' #你的静态文件存放的文件夹
线上程序最好是跑在一个虚拟环境中,这样可以隔离不同的项目
用xshell远程登录,在Linux下用以下命令安装虚拟环境
- #如果以root用户登录的话可以不用加 sudo
- sudo pip install virtualenv
- sudo pip install virtualenvwrapper
如果提示找不到mkvirtualenv命令,则还要配置环境变量
- #1.创建目录用来存放虚拟环境
- mkdir $HOME/.virtualenvs
-
- #2.打开~/.bashrc文件,添加两行代码
- export WORKON_HOME=$HOME/.virtualenvs
- source /usr/local/bin/virtualenvwrapper.sh
-
- #如果还是提示找不到mkvirtualenv命令,可能是virtualenvwrapper.sh文件路径不对,用which找出路径
- #which virtualenvwrapper.sh
- #用找出的路径替换source /usr/local/bin/virtualenvwrapper.sh中的路径
-
- #3.运行
- source ~/.bashrc
创建虚拟环境
- mkvirtualenv 虚拟环境名称
- #例:mkvirtualenv myweb
-
-
查看虚拟环境
workon
使用虚拟环境
- workon 虚拟环境名称
- #例:workon myweb
退出虚拟环境
deactivate
删除虚拟环境
- rmvirtualenv 虚拟环境名称
- #例:rmvirtualenv myweb
-
- #删除正在运行的虚拟环境,需要先退出,再删除
- #即,先deactivate,再rmvirtualenv myweb
用conda方法的操作
- #创建一个python版本是3.6的虚拟环境
- conda create -n 虚拟环境名称 python=3.6
-
- #激活虚拟环境
- conda activate 虚拟环境名称
-
- #查看已安装的虚拟环境
- conda info --envs
-
- #退出虚拟环境
- deactivate 虚拟环境名称
a) 用xshell连接到服务器,在/home/virtualenv 文件夹下创建一个虚拟环境,比如我的项目名称叫basketball,
mkvirtualenv basketball
然后切换到虚拟环境的目录
workon basketball
b) 用xftp把项目文件上传到 /home/virtualenv/basketball 目录下
c) 修改Nginx配置
Nginx的配置文件在 /etc/nginx 文件夹下,这里主要修改server这里涉及uwsgi的一些配置
-
- server {
- listen 80; #这里默认是80,最好别改,80端口是给http用的,不用这个端口网址解析不了,只能公网访问
- listen [::]:80;
- server_name 47.100.184.223; #这里是从阿里云申请的公网IP
-
-
- error_log /home/virtualenvs/basketball/logs/nginx_error.log; #报错日志,可以随便改
-
- # Load configuration files for the default server block.
- include /etc/nginx/default.d/*.conf;
-
- location / {
-
- include /etc/nginx/uwsgi_params;
- uwsgi_pass 127.0.0.1:8005; #这里是和uwsgi通信的内部地址,要和uwsgi.ini的配置里面一致,否则无法实现通信
- uwsgi_param UWSGI_SCRIPT BasketballManager.wsgi;
- uwsgi_param UWSGI_CHDIR /home/virtualenvs/basketball/BasketballManager;
-
- index index.html index.htm;
-
-
- client_max_body_size 4096m;
- }
- location /static {
- alias /home/virtualenvs/basketball/BasketballManager/static;
- }
-
- error_page 404 /404.html;
- location = /40x.html {
- }
-
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- }
- }
d) 修改uwsgi配置
uwsgi.ini文件一般放在虚拟目录下,你也可以指定位置和文件名,但是启动调用的时候也要指定路径,我这里是在指定目录下新建了一个uwsgi_basket.ini配置文件
/home/virtualenvs/basketball/BasketballManager/uwsgi_basket.ini
这里最主要修改的就是网络配置,上面说过,uwsgi 和 nginx 有三种方式可以通信,因为我的uwsgi和nginx都装在同一个服务器上,所以用socket比较快,所以这里选择用socket配置
- [uwsgi]
-
- socket = 127.0.0.1:8005 //这里的端口号要跟nginx配置文件里面的一致
- master = true //主进程
- vhost = true //多站模式
- no-site = true //多站模式时不设置入口模块和文件
- workers = 4 //子进程数
- reload-mercy = 10
- vacuum = true //退出、重启时清理文件
- max-requests = 1000 //最大连接数
- limit-as = 1024
- http-timeout=600
- buffer-size = 10240000 //这里根据电脑配置可以调大一点
-
-
- pidfile = /home/virtualenvs/basketball/BasketballManager/uwsgi6666.pid
- daemonize = /home/virtualenvs/basketball/BasketballManager/uwsgi6666.log //指定log文件位置
-
- pythonpath=/usr/local/lib/python3.6/site-packages
- pythonpath=/home/virtualenvs/basketball/lib/python3.6/site-packages
tips:这里有个坑,可以看到我这里配置文件里面的注释都用//来分割,而不是Python常用的#,有些网上的配置文件用#注释,最后就会连接不上,我郁闷了好久,后来发现它读取这个配置文件的时候可能不是当作Python文件来读的,// 这个是C语言或者Java之类的注释,这样就能理解了。
e) 在阿里云开放刚刚设定的端口
f) 启动
这里要注意的是,如果你的机器(服务器)上有多个版本的Python,或者说虚拟环境重装了Python的话,一定要搞清楚启动的是哪个Python,还有uwsgi也是一样,所以我用的笨办法就是指定路径,我的启动命令如下:
- #启动nginx
- /usr/sbin/nginx start
-
- #启动uwsgi 这里指定启动哪个uwsgi,调用哪个uwsgi的ini文件
- /home/virtualenvs/basketball/bin/uwsgi --ini /home/virtualenvs/basketball/BasketballManager/uwsgi_basket.ini
-
如果一切顺利,这时候打开浏览器输入公网地址,就可以看到网站的页面了
g) 网址解析
找到申请域名的服务商,我的是百度,找到百度的域名管理
点击解析
把自己的公网地址填进去,在服务器重启服务,这样就可以直接通过网址访问你的网站了。
总结:
整个上线部署是一个复杂的过程,这里只是介绍了最基本的流程,其他还要结合自己的业务考虑很多,比如访问量、安全漏洞、数据安全、网络配置、支付系统、管理权限等等很多问题,这里不展开叙述了,真实上线部署的时候要慎之又慎。
到这里,我们就基本掌握了用Django开发一个网站的技术,结合前端技术,就可以开发出一个完整的网站了,动手尝试一下吧!