Deploy python3 and nginx projects on ubuntu18.04

Yesterday [server] (https://cloud.tencent.com/product/cvm?from=10680) opened a new administrator account, and the weak password was used, which led to the hacking, and ssh could not log in. There was no way but to reinstall the system. Fortunately, there was no important information. A bus api and blog, the system was reinstalled in the morning, the environment was re-built, and recorded.

The server is [Tencent Cloud] (https://cloud.tencent.com/product/cvm?from=10680), the system is ubuntu18.04, comes with python2 and python3, my project is based on python3, so it can be used directly.

1. Install dependencies

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 create a virtual python environment, which can create an independent development environment for each Python application so that they do not affect each other. Virtualenv can do:

pip3 install virtualenv

Check if the installation is successful

virtualenv --version

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

Install the virtual environment, named venv, must enter the project directory,

virtualenv venv

A new venv directory will be created in the project directory, which contains the tools, instructions and packages of the basic environment for running python. Then enable the environment.

source venv/bin/activate

The word (venv) will appear before the command character after calling the activate command. You can exit the virtual environment through deactivate.

3. Installation project dependencies (Flask framework)

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

Has been placed in the requirements.txt file,

pip install -r requirements.txt

It is more convenient to install dependent files in this way. If there are few dependencies, 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

Create a new config.ini file in the project root directory

vim config.ini

document content

[ uwsgi]
# The address and port socket used when uwsgi is started=127.0.0.1:8001 
# Point to the website directory chdir=/opt/busAPI/
# python startup program file wsgi-file = run.py
# The application variable name callable used to start the python program= app
# Number of processors=4
# Threads=2
# Status detection address stats=127.0.0.1:9191

The comment is already obvious, just explain that callable=app This app is a variable in the run.py program file. The type of this variable is the application class of Flask.

You can run the test

uwsgi config.ini

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

5. Install Supervisor

sudo apt-get install supervisor

The location of Supervisor's global configuration file is:

/etc/supervisor/supervisor.conf

Under normal circumstances, we don’t need to make any changes to it, just add a new *.conf file and put it under /etc/supervisor/conf.d/, then we will create a new one The supervisor configuration of uwsgi used to start the busAPI project (named: busAPI.conf):

[ program:busAPI]
# Start command entry command=/opt/busAPI/venv/bin/uwsgi /opt/busAPI/config.ini
# The directory where the command program is located=/opt/busAPI#The user name that runs the command user=root   autostart=trueautorestart=true
# Log address stdout_logfile=/var/log/uwsgi_supervisor.log

Start and stop service#

# Start service
sudo service supervisor star
# Termination of service
sudo service supervisor stop
# Check status
sudo service supervisor status

6. Install Nginx

sudo apt-get install nginx

Configure Nginx, we modify the default file

/etc/nginx/sites-available/default

The default is port 80, here I did not use 80, because the applet must be https, so I configured port 443, restart Nginx

sudo service nginx restart

Nginx service commands

# start up
sudo service nginx start
# stop
sudo service nginx stop
# Reboot
sudo service nginx restart
# Check status
sudo service nginx status

7. to sum up

Recording the process from scratch, there were many pits in the middle, but the operation was successful in the end. In summary, it is convenient to find in the future.

For more high-quality content, please pay attention to [Youth Coder]

Recommended Posts

Deploy python3 and nginx projects on ubuntu18.04
Centos7 deploys python3 virtual environment
Deploy python3 and nginx projects on ubuntu18.04
[python] python2 and python3 under ubuntu
Install Python3 on Ubuntu 16.04
Install Python3.7 on Ubuntu
Install Python 3.7 on Ubuntu 18.04 LTS
ubuntu18.04 compile and install python3.8
Jmeter (18)_Ubuntu deploy jmeter and ant
Supervisor + Nginx + Python3 deploy Tornado
Deploy JobConverter + Ibreoffice environment on Ubuntu
How to install Nginx on Ubuntu 20.04
Install and configure MySQL on Ubuntu
How to install Python 3.8 on Ubuntu 18.04
Install and deploy Gerrit under Ubuntu
How to deploy Django on Ubuntu 14.04
How to secure Nginx on Ubuntu 14.04
Install Python virtual environment on Ubuntu 18.04
How to install Nginx on Ubuntu 20.04
Configure python3 environment on centos7 and
Install TensorFlow (python2.7 version) on Ubuntu
How to install Nginx on Ubuntu 16.04
Nginx installation and configuration load (ubuntu12.04)
Install the latest Python 3.6 version on Ubuntu
Detailed tutorial on installing python3.7 for ubuntu18
Error when installing Python module on Ubuntu
Install docker on ubuntu and basic usage
Installation and deployment of Nginx in Ubuntu
Install fonts on Ubuntu and switch default fonts
Configure Nginx Git server on Ubuntu system
Deploy Docker and configure Nginx in CentOS
How to install Python2 on Ubuntu20.04 ubuntu/focal64
Install and uninstall CUDA and CUDNN on Ubuntu
Use python3 to install third on ubuntu
M5Stack is developed and compiled on ubuntu
How to upgrade all Python libraries on Ubuntu 18.04
How to install Pycharm and Ipython on Ubuntu 16.04/18.04
Install JDK and configure environment variables on Ubuntu 16.04
How to install and configure Gogs on Ubuntu 18.04
Ubuntu16 upgrade Python3
ubuntu18.04 install python2
Relearn ubuntu --python3
How to install and configure Cyberpanel on Ubuntu 18.04
How to install and secure phpMyAdmin on Ubuntu 16.04
How to install and configure ownCloud on Ubuntu 16.04
How to install and configure ownCloud on Ubuntu 16.04
ubuntu12.04 install python3
Install and use Git and GitHub on Ubuntu Linux
How to install and configure GitLab on Ubuntu 18.04
How to install and use Docker on Ubuntu 20.04
How to quickly deploy docker on ubuntu server
How to install and use Curl on Ubuntu 18.04
How to install and use Composer on Ubuntu 18.04
How to play happily with Python3 on Ubuntu
lamp on ubuntu
Python and Go
How to install and secure phpMyAdmin on Ubuntu 16.04
How to install and configure Elasticsearch on Ubuntu 16.04
How to install and configure PostGIS on Ubuntu 14.04
How to install and use Composer on Ubuntu 20.04
[Practice] How to install python3.6 on Ubuntu system
How to install and use BaasBox on Ubuntu 14.04