Alibaba Cloud ECS deploys python and flask (1)

Because the server was originally owned by a unit, it was not easy to put personal things due to security issues, so I had to find a virtual host to put it. But I don't know what cloud or virtual host, just because when I registered the domain name, I saw that Alibaba Cloud has a free tier . Anyway, I don't need money, so I want to try it as a hands-on hand, so I got a cloud server ECS basic version. The following is a record of my personal deployment process:

  1. After the order is placed, the ECS instance of the cloud server will be successfully created soon. Log in to the Alibaba Cloud management console , and you can see the created instance in the instance, as well as the public and private network addresses of the instance. In the management->basic information->more->reset password at the back, you can modify the root password. For the remote connection behind the instance, you can log in to this instance on Alibaba Cloud. Going up and looking at it, it is actually a centOS server.
  2. The remote connection in the management console is a web version, which is not good-looking and troublesome. You can use a mac terminal to connect: ssh root@ the public network address of the instance. Or use FinalShell to log in remotely more convenient and easy to use.
  3. After entering the system, first upgrade the system:

yum update

  1. Install python3: python2.7 has been installed in the cloud server, but I use python3.以下操作需要登录到云服务器
  • First install the compiled related packages. The command is as follows:
    yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make libffi-devel
  • Find the required version from https://www.python.org/ftp/python/, I download version 3.7.1, command:
    wget https://www.python.org/ftp/python/3.7.1/Python -3.7.1.tar.xz
  • Unzip:
    xz -d Python-3.7.1.tar.xz
    tar -xvf Python-3.7.1.tar
  • After the decompression is complete, enter the corresponding decompressed folder to compile and install:
    ./configure prefix=/usr/local/python3
    make && make install
    After the installation is complete, there will be python3 in the /usr/local/ directory
  • Set soft connection
    ln -s /usr/local/python3/bin/python3 /usr/bin/python3
  1. Create a virtual environment of python:

Because it is for testing purposes, I did not create a new user. Create a project test directly under /root.

  • mkdir /root/test
  • python3 -m venv /root/test/venv
  1. Upload files to cloud server

Upload the locally created test project to the cloud server. This step is to operate on the local machine. On the mac terminal:

  1. Install the required packages in the virtual environment
  • On the cloud server side, enter the python virtual environment just created:
    source /root/test/venv/bin/ activate
  • Install the packages required for the test project (requirements.txt has just been uploaded to the cloud server's /root/test/gh):
    pip install -r /root/test/requirements.txt
  1. After the dependency package is installed, you can 虚拟环境中run the program test.

python test.py

  1. Cloud Management Console -> Security Group -> Configuration Rules -> Add Security Group Rules, add the ports used in the test project. (My test project uses port 4000, see the test.py code below)
  2. Use a browser on the local side to open the public network address: port of the cloud instance, and you can see your test project. test.py is as follows:
from flask import Flask

app=Flask(__name__)

@app.route('/')
def index():
    return '<center><h1>Hello Flask</h1></center>'

if __name__ == '__main__':
    app.run(host='0.0.0.0',port=4000)
  1. Installation and configuration of uwsgi, all operations need to be in the virtual environment of the cloud server.
  • Install uwsgi:

pip install uwsgi

  • Create a new file test_uwsgi.ini in the project and write the following configuration configuration as follows:
[uwsgi]

# uwsgi 启动时所使用的地址与端口
socket = 127.0.0.1:8000

# 外网访问端口,如果直接用uWSGI外网,这里由于使用了Nginx,故注释掉
# http= :80

# 指向网站目录
chdir = /root/test/

# python 启动程序文件
wsgi-file = test.py

# python 程序内用以启动的 application 变量名
# app 是 manage.py 程序文件内的一个变量,这个变量的类型是 Flask的 application 类
callable = app

# 处理器数
processes = 4

# 线程数
threads = 2

#状态检测地址
stats = 127.0.0.1:9191

daemonize=/var/log/uwsgi.log
  • 启动 : uwsgi --ini test_uwsgi.ini
  • After using uwsgi, change the
app.run(host='0.0.0.0',port=4000)

To

app.run()

建议:

According to the uwsgi service start (start) stop (stop) reload (reload) article to configure uwsgi. Then you can use the following commands to operate uwsgi:

uwsgi --ini uwsgin.ini # Start
uwsgi --reload uwsgi/uwsgi.pid # Restart
uwsgi --connect-and-read uwsgi/uwsgi.status # Connect and view the status
uwsgi --stop uwsgi/uwsgi.pid # Stop the service

  1. Install and configure nginx on the cloud server.
  • Install nginx:

yum install nginx

  • Configure nginx.conf. Use to find / grep nginx.conffind the path of nginx.conf and modify the server{} as follows:
    server {
    
    
        listen       80 default_server;
    #    listen       [::]:80 default_server;
        server_name  test;
    #    root         /usr/share/nginx/html;
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        location /static {
    
    
    #            alias /var/www/execution/static;
        }
        location / {
    
    
    #			这里指向uwsgi里socket=*设置的地址   
                uwsgi_pass 127.0.0.1:8000;
                include uwsgi_params;
        }
        error_page 404 /404.html;
            location = /40x.html {
    
    
        }
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
    
    
        }
    }
  • Verify that the configuration is correct: nginx -t
  • Check the version number of Nginx: nginx -V
  • Start Nginx: nginx -c /etc/nginx/nginx.conf
  • Quickly stop or shut down Nginx: nginx -s stop
  • Stop or close Nginx normally: nginx -s quit
  • Configuration file modification and reload command: nginx -s reload
  1. Install mysql:
  • Download and add the repository: The
    download address can be found at https://www.mysql.com/downloads/ ->Yum Repository -> Red Hat Enterprise Linux 7 / Oracle Linux 7 (Architecture Independent), RPM Package(mysql80-community- release-el7-1.noarch.rpm), click Download on the right -> "No thanks, just start my download" under the lowest is the download address.

sudo yum localinstall https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm

  • Set the installation version, the default is mysql5.7

vi /etc/yum.repos.d/mysql-community.repo

Change the enabled under the original version to 0, and change the enabled under the version you want to install to 1.

  • Install MySQL package

yum install mysql-community-server

  • After the installation is complete, enable and start the MySQL service type:

sudo systemctl enable mysqld
sudo systemctl start mysqld

  • MySQL security:
    When the MySQL server is started for the first time, a temporary password is generated for the MySQL root user. You can find the password by running the following command:

sudo grep ‘temporary password’ /var/log/mysqld.log

Run the mysql_secure_installation command to improve the security of MySQL installation and reset the root password. Then there will be some operations such as deleting anonymous users and temporary databases, whether to allow root remote access, etc., all the way "y" is enough:

sudo mysql_secure_installation

Connect to mysql and create a database:

mysql -u root -p
CREATE DATABASE test;


To configure multi-project self-starting, please refer to "Alibaba Cloud ECS deployment python and flask (2): use uwsgi emperor +Nginx deployment to automatically start multiple web projects"

Guess you like

Origin blog.csdn.net/qq_41090453/article/details/83451321