Python project to deploy linux server

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
2
3
4
tar -xvJf  Python-3.6.2.tar.xz
cd Python-3.6.2
./configure prefix=/usr/local/python3
make && make install

 

4. After installation, create a soft link

1
2
3
4
5
-s LN / usr / local / python3 / bin / python3 / usr / bin / Python 
# Run the
Python -V # python3 will see the version of
# execute command
python2 -V #'ll see the version of python2

 

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
2
3
vim / usr / bin / yum 
vim / usr / libexec / urlgrabber-EXT-Down
# these two files #! / usr / bin / python changed to #! / usr / bin / python2

 

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
2
3
4
5
certifi==2018.4.16
chardet==3.0.4
idna==2.7
requests==2.19.1
urllib3==1.23

 

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Install the virtual environment 
PIP3 install virtualenv

# Create a virtual environment ENV
virtualenv ENV

# to switch to a virtual environment where the directory
cd ENV

# Enable virtual environment
source ./bin/activate

library # in the list of installed dependence
PIP3 install -r requirements.txt

# column the current virtual environment dependent libraries installed
pip3 list

 

Step Four: Add custom system services (very important)

1
2
# After this command ssh terminal exit, python process will be killed 
python xxx.py &

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
2
3
4
5
6
7
8
9
10
11
[Unit]
Description=robot
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/python3/bin/ENV/bin/python /usr/local/python3/bin/ENV/p3.py &
PrivateTmp=true

[Install]
WantedBy=multi-user.target

 

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!

Guess you like

Origin www.cnblogs.com/grimm/p/11104562.html
Recommended