Recently wrote a plug-in Python, you need to be deployed on a Linux server environment, the use of virtualenv time before the local development, the use of the virtual environment has the advantage that the project does not depend on libraries is global, only the current directory project valid, because I'm a Mac system, and the libraries under linux environment virtualenv required library is not common, it can not simply be copied to the server running the entire env, you need to do some extra work.
Generally speaking, each of the different projects will depend on their libraries, some libraries are not the same version can cause conflicts, in order to solve this problem, you need to use a virtual environment, Python can create their own virtual environment in each project directory, project depends package it in an environment directory, thus avoiding the library version of the conflict, but also facilitate inter same operating system can quickly run a copy of the project.
I am using a written Python3 project, CentOS server on 7 only Python2, so we need to install what Python3 environment
Step 1: Install Python3 environment
1. First, install the build environment (Python3 subsequent need to get the source code to compile python from python's official website)
1 |
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make |
2. Download the source code from the official website python3
1 |
wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tar.xz |
3. sequentially performs decompression> directory after the files are decompressed> compile and install
1 |
tar -xvJf Python-3.6.2.tar.xz |
4. After installation, create a soft link
1 |
-s LN / usr / local / python3 / bin / python3 / usr / bin / Python |
5. Follow-up work, due to the implementation of the CentOS yum command requires the use of python2 version comes, we need to make two modifications
1 |
vim / usr / bin / yum |
Step two: the project will depend on local development environment generates the manifest file
1. In the local development environment, env under execution:
1 |
pip3 freeze >requirements.txt |
The list of documents will be generated under the current project directory, something like this
1 |
certifi==2018.4.16 |
The file is uploaded to the linux server generated
2. Python projects uploaded to the server
1 |
#slightly... |
The third step: to create a virtual environment for the project on the linux server, and install the required project dependencies
1. pip3 change to the directory where the / usr / local / python / bin, run the following command
1 |
# Install the virtual environment |
Step Four: Add custom system services (very important)
1 |
# After this command ssh terminal exit, python process will be killed |
We need to create a custom system services to ensure python program to run in the background.
1. Create a system service
1 |
vim /usr/lib/systemd/system/robot.service |
It reads as follows:
1 |
[Unit] |
ExecStart start command to execute when the service can not use relative paths must be full path.
Here you can also order any written .sh file, here to write .sh file full path is also possible.
2. Enable custom system services
1 |
systemctl enable robot |
3. Start Services
1 |
systemctl start robot |
You can view the process, confirm whether the service is started
1 |
ps aux | grep robot |
Finished!