1、云服务器购买
本人在腾讯云上购买的服务器,可以在腾讯云上注册账号,根据【控制台】-【云服务器】-【新建】购买云服务器,如下图所示
可根据个人需求,选择内存大小、CPU核数等参数
我选定的是CentOs7.7,64位,2核,4G内存,2Mbps的公网带宽的云服务器
2、Xshell下载及连接服务器
(1)Xshell6.0下载链接
(链接:https://pan.baidu.com/s/1gUpALbp797Vl5RnfSJGI1g
提取码:vuxu)
(2)连接
在购买完云服务器过后,将会获取云服务器的公网和内网IP地址,输入公网IP地址和密码进行链接,即可通过Xshell来操作Linux操作系统
3、域名购买与解析
当你购买完云服务器后,可以通过公网IP地址对云服务器进行访问,同时你也可以购买一个专属域名来访问云服务器,域名购买链接(https://cloud.tencent.com/document/product/242/8584),购买完域名之后,需要对域名进行解析,解析链接(https://cloud.tencent.com/document/product/242/8584)
4、直接发布html文件
(1)安装nginx
用命令
yum install -y nginx
安装nginx,安装完后分别输入nginx -v,出现相应#版本号即为安装成功
(2)文件准备
mkdir -p /data/www/hexo
chmod -R 777 /data/www/hexo
vim /data/www/hexo/index.html
添加代码如下
<!DOCTYPE html><html>
<head>
<title></title>
<meta charset="UTF-8">
</head>
<body>
<p>Hello world!</p>
</body></html>
(3)修改nginx.conf配置文件
vim /etc/nginx/nginx.conf
server {
listen 80; //nginx 默认80端口
server_name www.xxx;//域名
root /data/www/hexo;//文件位置/usr/share/nginx/html
}
(4)开启Nginx服务
systemctl start nginx
浏览器访问域名(公网IP或者是域名),查看能否正常访问index.html内容 若每次更改nginx配置,可输入命令nginx -s reload,让配置生效 一定要在服务器的安全组规则中添加80端口,否则不会有任何输出
5、flask框架搭建网站
(1)安装pip
详细教程如:
- 首先检查linux有没有安装python-pip包,直接执行 yum install python-pip,显示No package python-pip available. 则没有,继续如下操作
- 没有python-pip包就执行命令 yum -y install epel-release
- 执行成功之后,再次执行yum install python-pip
- 对安装好的pip进行升级 pip install --upgrade pip
至此,pip工具就安装好了。
(2)安装virtualenv和virtualenvwrapper
pip install virtualenv
pip install virtualenvwrapper
(3)创建一个项目和 python 虚拟环境
使用python的virtualenv创建虚拟环境 。用来在一个系统中创建不同的 python 隔离环境。相互之间还不会影响,而且使用相当简单。
mkdir myflask
cd myflask
virtualenv venv
(4)激活虚拟环境
source venv/bin/activate
(5)安装 python web 框架 — flask
flask 是一个 python web 轻型框架,简洁高效。flask 依赖两个库 werkzeug 和 jinjia2。可采用 pip 方式安装
pip install flask
(6)代码文件准备
vim run.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return 'hello world!'
if __name__ == '__main__':
app.run()
(7)运行代码
启动 flask
python run.py
此时,用浏览器访问域名或者公网IP就能看到网页显示 hello world!
(8)使用 gunicorn 部署 python web
在上述代码的运行结果中,可以发现以下两行结果,现在我们使用 flask 自带的服务器,完成了 web 服务的启动。生产环境下,flask 自带的 服务器,无法满足性能要求。我们这里采用 gunicorn 做 wsgi容器,用来部署 python,用pip直接安装。
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
pip install gunicorn
当我们安装好 gunicorn 之后,需要用 gunicorn 启动 flask,注意 flask 里面的name里面的代码启动了 app.run(),这个含义是用 flask 自带的服务器启动 app。这里我们使用了 gunicorn,run.py 就等同于一个库文件,被 gunicorn 调用。
gunicorn -w4 -b0.0.0.0:5000 run:app
此时,我们可以用 5000 的端口进行访问。
想要结束 gunicorn 只需执行 pkill gunicorn,有时候还需要用 ps 找到 pid 进程号才能 kill。
(9)Nginx配置
vim /etc/nginx/nginx.conf
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://127.0.0.1:5000;
proxy_redirect off;
proxy_set_header Host $host:80;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
其中server_name就是你的域名,这里用localhost代表通过ip访问,配置好default.conf之后试着启动Nginx!
(10)开启Nginx服务
systemctl start nginx
浏览器访问域名(公网IP或者是域名),查看能否正常访问index.html内容 若每次更改nginx配置,可输入命令nginx -s reload,让配置生效 一定要在服务器的安全组规则中添加80端口,否则不会有任何输出
(11)nohup的使用
当你关系xshell再访问域名时,你会发现以下错误
如果你想使你的程序在linux后台运行,即关闭xshell程序仍然能够运行,此时,你需要使用nuhup命令,详细介绍可参考链接(https://www.jb51.net/article/169783.htm)
编辑sh文件,命名为test.sh
vim test.sh
cd myflask
source venv/bin/activate
gunicorn -w4 -b0.0.0.0:5000 run:app
编辑完shell文件之后,运行以下代码,即可使得程序一直在后台运行。
nohup bash test.sh &
(12)温馨提示
在云服务器中跳转html时,不需要添加port,在普通的服务器或者windows环境下,actionUrl = “http:/%s/%sresult”%(ip,port),代码如下
from flask import Flask, render_template
app = Flask(__name__)
ip = "0.0.0.0" #IP地址或者域名
port = 5000
@app.route("/")
def search():
actionUrl = "http:result"#云服务器
#actionUrl = "http://%s:%s/result"%(ip,port)#普通服务器或者windows系统
return render_template("login.html",actionUrl=actionUrl)
@app.route("/result",methods = ["POST", "GET"])
def result():
return render_template("login1.html")
if __name__ == "__main__":
app.run()