Install and deploy python3, nginx and flash projects in Ubuntu 18.04

Time:2021-3-4

Yesterday, the server opened a new administrator account with a weak password. As a result, it was hacked and SSH could not log in. There was no way but to reload the system. Fortunately, there was no important information. The server only ran a bus API and blog. In the morning, the system was reinstalled, and the environment was rebuilt and recorded.

The server is Tencent cloud, the system is Ubuntu 18.04, with python2 and python3. My project is based on python3, so it can be used directly.

1. Install dependency package

sudo apt install python3-pip
sudo apt install build-essential python-dev python-setuptools
sudo apt install build-essential python3-dev python3-setuptools
2. Install virtualenv

Virtualenv is used to build a virtual Python environment, which can create an independent development environment for each Python application, so that they do not affect each other

Install new packages without permission

Different applications can use different suite versions

Suite upgrade does not affect other applications

pip3 install virtualenv
Check whether the installation is successful

virtualenv –version

After successful installation, I will use it next. My project is an API service. I put my project file under / opt and the file name is busapi. I put all my project files in it

Install and deploy python3, nginx and flash projects in Ubuntu 18.04

To install the virtual environment, named venv, you must enter the project directory,

virtualenv venv
In the project directory, a new venv directory will be created, which is the tools, instructions and packages of the basic environment for running python. Then enable the environment.

source venv/bin/activate
After calling the activate command, the word (venv) appears in front of the command character. You can exit the virtual environment through activate.

3. Installation project dependency (flash framework)

My project file has been uploaded to the directory, but the project dependency has not been installed yet. Installation dependency, my dependency file

It’s been put in requirements.txt In the file,

pip install -r requirements.txt
In this way, it is more convenient to install dependent files. If there is less dependence, you can install them one by one.

4. Install and configure uwsgi

At this point, we have installed uwsgi directly in the virtual environment

pip install uwsgi
New in project root config.ini file

vim config.ini
Document content

`[uwsgi]

The address and port used when uwsgi starts

socket = 127.0.0.1:8001

Point to the site directory

chdir = /opt/busAPI/

Python startup program file

wsgi-file = run.py

Application variable name used to start in Python program

callable = app

Number of processors

processes = 4

Number of threads

threads = 2
`

State detection address

stats = 127.0.0.1:9191
The annotation is already very obvious. Just explain that callable = app is run.py A variable in the program file whose type is the application class of flag.
Install and deploy python3, nginx and flash projects in Ubuntu 18.04

You can run tests

uwsgi config.ini
But this is only the form of command start, to make it start with the server and run as a background service is the actual needs of the operating environment. So next we need to install another tool to boot uwsgi.

5. Install the supervisor

sudo apt-get install supervisor

The location of the global configuration file of supervisor is:

/etc/supervisor/supervisor.conf
Under normal circumstances, we don’t need to make any changes to it. We just need to add a new *. Conf file and put it in / etc / Supervisor / conf.d /. Then we need to create a new supervisor configuration of uwsgi (named: busAPI.conf ):

[program:busAPI]

Start command entry

command=/opt/busAPI/venv/bin/uwsgi /opt/busAPI/config.ini

Directory of command program

directory=/opt/busAPI

The user name to run the command

user=root

autostart=true
autorestart=true

Log address

stdout_logfile=/var/log/uwsgi_supervisor.log

Initiation and termination of services

Start the service

sudo service supervisor star

Termination of service

sudo service supervisor stop

View status

sudo service supervisor status
6. Install nginx

sudo apt-get install nginx
To configure nginx, we modify the default file

/etc/nginx/sites-available/default

The default is port 80. I don’t use port 80 here, because the applet must use HTTPS, so I configured port 443 to restart nginx

sudo service nginx restart
Nginx service command

start-up

sudo service nginx start

stop it

sudo service nginx stop

restart

sudo service nginx restart

View status

sudo service nginx status
7. Summary

Record the process from scratch, and there are many pits in the process, but the operation is successful in the end. Summary, convenient for future search.
Source:Biao fan interaction(https://www.biaofun.com/)

Recommended Today

Third party calls wechat payment interface

Step one: preparation 1. Wechat payment interface can only be called if the developer qualification has been authenticated on wechat open platform, so the first thing is to authenticate. It’s very simple, but wechat will charge 300 yuan for audit 2. Set payment directory Login wechat payment merchant platform( pay.weixin.qq . com) — > Product […]