Alibaba Cloud server deploys flask+gunicorn project

1 Introduction:

  • server

  • Alibaba Cloud's lightweight application server, the system is Centos 7.3.

  • The related software used is: powershell

Insert picture description here
1. If you want to use software to remotely connect to the Alibaba Cloud server, you need to reset a password for the server on the Alibaba Cloud console first.
2. When connecting to the server remotely, it will not be displayed when the password is entered.
Just press Enter after the input is complete.
3. It is best to understand some of the most basic Linux commands such as

cd,pwd,kill,ls, mkdir...

2. Install Python 3.6

First install the dependency package

yum -y groupinstall "Development tools"

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

Then download different versions of Python3 according to your needs, I downloaded Python3.6.2

wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tar.xz

If the speed is not fast enough, you can go directly to the official website to download, use WinSCP and other software to upload to the specified location on the server, my storage directory is /usr/local/python3, use the command:
other version address : https://www.python.org /ftp/python/
Create an empty folder:

mkdir /usr/local/python3 

Then unzip the compressed package, enter the directory, install Python3

tar -xvJf  Python-3.6.2.tar.xz
cd Python-3.6.2
./configure --prefix=/usr/local/python3
make && make install

After the installation is complete, you will be prompted to install pip and other tools.

The words "successfully installed" appear.

So far, Python 3.6 has been installed,

Because the version of python that comes with centos7 is python2.

Many packages now only support python3, so we have to install a virtual environment. The python version in the virtual environment is 3.6

Then we have to install virtualenv (python environment isolation tool).
After that, the deployment of our flask and the download of the python package must be carried out in this virtual environment!
Remember to enter the virtual environment every time you reconnect to centos remotely before operating.

3. Install virtualenv

pip  install virtualenv

建立python3独立环境
virtualenv  -p /usr/local/python3/bin/python3.6 /py3env

进去python3独立环境
source /py3Env/bin/activate

This is the case after successfully entering the independent environment.
Insert picture description here
Finally we enter:

python -V

So far, the installation of python3 and the installation of the independent environment are complete

4. Build LNMP environment (Mysql, Nginx, Php)

1. Turn off the firewall and selinux to
open the file selinux

vim  /etc/sysconfig/selinux

Change SELINUX=enforcing in the file to disabled, and then execute "setenforce 0" to close selinux without restarting.

SELINUX=disabled

Turn off the fire wall

systemctl stop firewalld.service

2. Install the software
2.1. MYSQL installation
Download the repo source of mysql

wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm

Install the mysql-community-release-el7-5.noarch.rpm package

rpm -ivh mysql-community-release-el7-5.noarch.rpm


The download address for installing the MYSQL wget command may be invalid. Go to the official website to find the address of the package to be downloaded.
mysql official website: http://dev.mysql.com/

sudo chown -R root:root /var/lib/mysql

​ Restart the service:

systemctl restart mysql.service

Login and change the password:

mysql -u root
mysql > use mysql;
mysql > update user set password=password('123456') where user='root';
mysql > flush privileges;
mysql > exit;

123456 is the mysql
password. Just set the password. Don’t forget it. (It’s troublesome to forget)
4.
You can choose github or Bitbucket to install git . Of course, you can also build a git server by yourself, but I don’t think it is necessary. I chose Bitbucket mainly because its private library is free

sudo yum install git

The follow-up will be no different from our local development. Configure the ssh key and clone code, it will not be expanded. The project directory is recommended to be placed under /home/www/
5. Upload the flask project file to the server
Copy the local code to the remote

scp -r 本地文件路径 [email protected]:远程保存路径

note: Single file, such as. .txt file, just go to Jin directly, remove -r

6. Install the Python package

cd /usr/local/python3/bin

yum install mysql-devel gcc gcc-devel python-devel

pip install -r requirements.txt

note: Generate requirements.txt file

pip freeze > requirements.txt

7. Debug the Mysql script file
. It will be troublesome to rebuild the database and table on the server, and sometimes mysql already has some data on the local computer.

So a quicker way is to export the sql script file on the local database, then upload the script file to the server, and it will run.

After running, the entire database can be moved to the server

I use Navicat for Mysql to export sql files

Right-click the blog to dump the sql file and
Insert picture description here
upload the file to the server after obtaining the blog.sql file, as in the above steps.
If the location of the blog.sql file is /usr/local/python3/bin/blog.sql,
first enter mysql

mysql -u root -p

输入密码
在Centos下里面的是首先要新建一个和文件相同名字的数据库。

mysql>create database blog;

然后输入指令:

mysql> use blog;

mysql>source /usr/local/python3/bin/blog.sql;
mysql>exit;

Then it's OK.

5. Install gunicorn

pip install gunicorn

6. Run the flask project

gunicorn -w 3 -b x.x.x.x:8000 manage:app

Pay attention to replace xxxx with the intranet ip address and
you're done!

Extranet wants to visit your flask website

Just go through the IP address of the public network.

X.X.X.X:8000

If you want to stop the operation of this program,
view the process

ps -aux | grep gunicorn

Then kill these 3 processes just fine

kill -9 进程号

If you reconnect to the server, remember to enter the independent environment before operating!

source /py3Env/bin/activate

Guess you like

Origin blog.csdn.net/qq_43030934/article/details/106998600