[Website building] Use Nginx and flask framework to build a website on the cloud server

1. Cloud server purchase

For the server I purchased on Tencent Cloud, you can register an account on Tencent Cloud and purchase a cloud server according to [Console]-[Cloud Server]-[New]. As shown in the figure below, you
Insert picture description here
can select the memory size and CPU core according to your personal needs Number and other parameters
Insert picture description here
I chose CentOs7.7, 64-bit, 2 cores, 4G memory, 2Mbps public network bandwidth cloud server
Insert picture description here

2. Xshell download and connect to the server

(1) Xshell6.0 download link

(Link: https://pan.baidu.com/s/1gUpALbp797Vl5RnfSJGI1g
extraction code: vuxu)

(2) Connection

Insert picture description here
After purchasing the cloud server, you will obtain the public network and internal network IP addresses of the cloud server, enter the public network IP address and password to link, and you can operate the Linux operating system through Xshell

3. Domain name purchase and resolution

After you purchase the cloud server, you can access the cloud server through the public IP address. At the same time, you can also purchase a dedicated domain name to access the cloud server. The domain name purchase link (https://cloud.tencent.com/document/product /242/8584). After purchasing the domain name, you need to resolve the domain name and resolve the link (https://cloud.tencent.com/document/product/242/8584)

4. Publish html files directly

(1) Install nginx

With command

yum install -y nginx

Install nginx, enter nginx -v after installation, and if the corresponding # version number appears, the installation is successful

(2) Document preparation

mkdir -p /data/www/hexo
chmod -R 777 /data/www/hexo
vim /data/www/hexo/index.html

Add the code as follows

<!DOCTYPE html><html>
  <head>
    <title></title>
    <meta charset="UTF-8">
  </head>
  <body>
    <p>Hello world!</p>
  </body></html>

(3) Modify the nginx.conf configuration file

vim /etc/nginx/nginx.conf
server {
    
    
        listen       80; //nginx 默认80端口
        server_name  www.xxx;//域名
        root         /data/www/hexo;//文件位置/usr/share/nginx/html
    }

(4) Open Nginx service

systemctl  start nginx

The browser accesses the domain name (public IP or domain name) to check whether the content of index.html can be accessed normally. If you change the nginx configuration every time, you can enter the command nginx -s reload to make the configuration effective. You must add it to the server’s security group rules 80 port, otherwise there will be no output
Insert picture description here

5. Flask framework to build a website

(1) Install pip

Detailed tutorials such as:

  • First check whether the python-pip package is installed in linux, directly execute yum install python-pip, and it will display No package python-pip available. If not, continue as follows
  • Execute the command yum -y install epel-release without the python-pip package
  • After successful execution, execute yum install python-pip again
  • Upgrade the installed pip pip install --upgrade pip

At this point, the pip tool is installed.

(2) Install virtualenv and virtualenvwrapper

pip install virtualenv
pip install virtualenvwrapper

(3) Create a project and python virtual environment

Use python's virtualenv to create a virtual environment. Used to create different python isolation environments in a system. It will not affect each other, and it is quite simple to use.

mkdir myflask
cd myflask
virtualenv venv

(4) Activate the virtual environment

source venv/bin/activate

(5) Install python web framework — flask

Flask is a lightweight python web framework, simple and efficient. Flask depends on two libraries werkzeug and jinjia2. Can be installed by pip

pip install flask

(6) Code file preparation

vim run.py
from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
  return 'hello world!'

if __name__ == '__main__':
  app.run()

(7) Run the code

Start flask

python run.py

At this point, use the browser to visit the domain name or public IP and you can see the web page displaying hello world!
Insert picture description here

(8) Use gunicorn to deploy python web

In the running result of the above code, you can find the following two lines of results. Now we use the server that comes with flask to complete the startup of the web service. In a production environment, Flask's own server cannot meet the performance requirements. Here we use gunicorn as the wsgi container to deploy python and install it directly with pip.

   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
pip install gunicorn

After we install gunicorn, we need to start flask with gunicorn. Note that the code in the name in flask starts app.run(), which means to start the app with the server that comes with flask. Here we use gunicorn, run.py is equivalent to a library file, called by gunicorn.

gunicorn -w4 -b0.0.0.0:5000 run:app

At this point, we can use port 5000 for access.
To end gunicorn, you only need to execute pkill gunicorn, and sometimes you need to use ps to find the pid process number to kill.

(9) Nginx configuration

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

}

The server_name is your domain name. Here, localhost is used to represent access via ip. After configuring default.conf, try to start Nginx!

(10) Open Nginx service

systemctl  start nginx

The browser accesses the domain name (public IP or domain name) to check whether the content of index.html can be accessed normally. If you change the nginx configuration every time, you can enter the command nginx -s reload to make the configuration effective. You must add it to the server’s security group rules 80 port, otherwise there will be no output

(11) The use of nohup

When you access the domain name with xshell, you will find the following error.
Insert picture description here
If you want to make your program run in the linux background, that is, close the xshell program and still be able to run. At this time, you need to use the nuhup command. For details, please refer to the link (https ://www.jb51.net/article/169783.htm)
Edit the sh file and name it test.sh

vim test.sh
cd myflask
source venv/bin/activate
gunicorn -w4 -b0.0.0.0:5000 run:app

After editing the shell file, run the following code to make the program run in the background all the time.

nohup bash test.sh &

(12) Warm reminder

When jumping html in the cloud server, there is no need to add a port. In a normal server or windows environment, actionUrl = "http:/%s/%sresult"%(ip,port), the code is as follows

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()

Guess you like

Origin blog.csdn.net/weixin_44704985/article/details/108563741