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