CentOS system Python Environmental Construction and Project Deployment

Contents

  • One, Python
    • 1. Install Python3 from source
    • 2. SCL install Python3
    • 3. Virtual environment venv
    • 4. Install Flask
    • 5. Install gunicorn
  • Second, install Nginx
    • 1. Install Nginx
    • 2. Important instructions
  • Three, set up Nginx server
    • 1. Create directory structure
    • 2. Configure server
    • 3. Flask + Nginx + Gunicorn
    • li>

  • 4. Supervisor
    • 1. Install supervisor
    • 2. Custom configuration example
    • 3. Used
    • 4. Boot
  • Five. Flask+Gunicorn+Nginx+Supervisord
    • 1. Directory structure
    • 2. Main program
    • 3. Define startup items
    • 4. Define supervisord
    • 5. Configure nginx
    • 6 . Command
  • Better solution
    • Redefine supervisord
    • Command
  • < li>Reference

一、Python

1. Source installation Python3

# Developer Tools
$ sudo yum -y install yum-utils
$ sudo yum-builddep python

# Download and unzip
$ wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz
$ tar -zxf Python-3.6. 8.tgz
$ cd Python-3.6.8/

# Compile and install
$ ./configure
$ make
$ sudo make install
$ python3 --version

# Set the default version
$ alias python='/usr/local/bin/python3.6'

2. SCL install Python3

# 1. Activate SCL$ sudo yum install centos-release-scl# 2. Install python3$ sudo yum install rh-python36# 3. Use python3$ python --versionPython 2.7.5$ scl enable rh-python36 bash$ python --versionPython 3.6.3# 4. Install development tools$ sudo yum groupinstall'Development Tools'

Note :

Set the python3 version here. If you reopen the session, the default python2.7 will be restored

Set the default

$ scl enable python36  
$ scl enable python36 bash

3. virtual environment venv

$ mkdir myapp$ cd myapp$ scl enable rh-python36 bash$ python -m venv env$ source env/bin/activate(env) [[emailprotected]_0_6_centos myapp]$

4 . Install Flask

hello.py

from flask import Flaskapp = Flask(__name__)@app.route('/') def hello_world(): return'Hello World!'
(env) [[emailprotected]_0_6_centos myapp]$pip install --upgrade pip(env) [[emailprotected]_0_6_centos myapp]$pip install Flask(env) [[emailprotected]_0_6_centos myapp]$ export FLASK_APP=hello(env) [[emailprotected]_0_6_centos myapp]$ flask run(env) [[emailprotected]_0_6_centos myapp]$ deactivate< /pre> 

5. Install gunicorn

(env) [[email protected]_0_6_centos myapp]$ pip3 install gunicorn
(env) [[emailprotected] _0_6_centos myapp]# gunicorn hello:app

Second, install Nginx

1. Install Nginx

# 1. Install Nginxyum -y install nginxsystemctl enable nginxsystemctl start ngi nxsystemctl status nginx# 2. Release port firewall HTTP (`80`) and HTTPS (`443`) ports.firewall-cmd --permanent --zone=public --add-service=httpfirewall-cmd --permanent --zone =public --add-service=httpssudo firewall-cmd --reload# 3. Browser `http://YOUR_IP`

2. Important instructions

# Start the service
sudo systemctl start nginx·#No output
sudo service start nginx #release command

# Startup
sudo systemctl enable nginx

# Shut down the service
sudo systemctl stop nginx
sudo service stop nginx

# Restart
sudo systemctl restart nginx
sudo service restart nginx

# Reload

When changing the Nginx configuration, you need to reload or restart Nginx
sudo systemctl reload nginx
sudo service reload nginx< br />
# Test syntax error

sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

# View nginx status

sudo systemctl status nginx

# View nginx version
sudo nginx -v

# Output Nginx version and configuration options
sudo nginx -V

Command summary:

sudo systemctl stop nginxsudo systemctl start nginxsudo systemctl restart nginxsudo systemctl reload nginxsudo systemctl disable nginxsudo systemctl enable nginx

three, set up Nginx server

1. create directory structure

/var/www/
├── example.com
│ └── public_html
├── example2.com
│ └── public_html
├── example3.com
│ └── public_html
  1. New file
mkdir -p /var/ www/example.com/public_html
  1. Create index.html
sudo nano /var/www/example.com/ public_html/index.html

Use the nano text editor to paste /var/www/example.com/public_html/index.html





Case test


Successful deployment



< /html>
  1. Modify user group
sudo chown -R nginx: /var/www/example.com

2. Configure server h3>

The Nginx server block configuration file must end with .conf and be stored in the directory /etc/nginx/conf.d

1. New example.com.conf
/etc/nginx/conf.d/example.com.conf

server {< br /> listen 80;
listen [::]:80;

root /var/www/example.com/public_html;

index index.html ;

server_name example.com www.example.com;

access_log /var/log/nginx/example.com.access.log;
error_log /var /log/nginx/example.com.error.log;

location / {
try_files $uri $uri/ =404;
}
}
  1. Test configuration
sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
  1. Restart
sudo systemctl restart nginx

image

3. Flask + Nginx + Gunicorn

  1. Configure server
server {

listen 80;
server_name 49.234.220.252;
access_log /var/log/nginx/hello/access.log;
error_log /var/log/nginx/hello/error.log;

location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-Ip $remote_addr;
}

}
  1. Start
$ cd myapp/$ source env/bin/activate# Create log folder (env) $ mkdir -p /var/log/nginx/hello/# Modify owner (env) $ sudo chown -R nginx: /var/log/nginx/hello(env) $ sudo nginx -t(env) $ systemctl reload nginx(env) $ gunicorn hello:app
  1. View process
# pstree -ap|grep gunicorn

four, Supervisor

Process management tool, convenient to monitor, start, stop, restart one or more Processes.

  • Supervisor: The name of the software to be installed.
  • Supervisord: After installing the supervisor software, supervisord is used to start the supervisor service.
  • supervisorctl: used to manage the program in the supervisor configuration file.

1. Install supervisor

yum -y install supervisor
# Generate 2 files
` - /etc/
|-- ...
|-- supervisord.conf # configuration file
`-- supervisord.d/ # configuration folder

Modify supervisord.conf

[unix_http_server]
file=/var/run/supervisor.sock

[supervisord]
logfile=/var/log/supervisor/supervisord.log; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups =10; (num of main logfile rotation backups;default 10)
loglevel=info; (log level;default info; others: debug,warn,trace)
pidfile=/var/run/supervisord. pid; (supervisord pidfile;default supervisord.pid)
nodaemon=false; (start in foreground if true;default false)
minfds=1024; (min. avail startup file descriptors;default 1024)
minprocs=200; (min. avail pr ocess descriptors;default 200)


[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///var/run/supervisor.sock


[include]
files = supervisord.d/*.ini

2. Custom configuration example

  1. Test program structure
`-- dosupervisor/
|-- log/ # log folder
| |-- long.err.log #error
| `-- long.out.log #output
`-- long.sh # calling program< br />
# long.sh

#!/bin/bash
while true
do
# Echo current date to stdout
echo `date`
# Echo'error!' to stderr
echo'error!' >&2
sleep 1
done
  1. Configuration< code>long_script.conf
# /etc/supervisord.d/long_script.conf

[program:long_script]
command= /root/dosupervisor/long.sh
autostart=true
autorestart=true
stderr_logfile=/root/dosupervisor/log/long.er r.log
stdout_logfile=/root/dosupervisor/log/long.out.log
  1. Check status
[[emailprotected]_0_6_centos ~]# supervisorctl status
long_script RUNNING pid 16862, uptime 0:00:51
[[emailprotected]_0_6_centos ~]# supervisorctl stop long_script
long_script: stopped
[[email protected]_0_6_centos ~]# supervisorctl status
long_script STOPPED Aug 02 11:22 PM
  1. Scroll to view results
tail -f long.out .log

3. Instructions used

yum -y remove supervisor #Uninstall
supervisord --version
< br /># Initial configuration
echo_supervisord_conf> /etc/supervisord.conf


supervisord: start supervisor
supervisorctl reload: restart supervisor after modifying the configuration file< br />supervisorctl status: view the process status of supervisor supervision
supervisorctl start process name: start XXX process
supervisorctl stop process name: stop XXX process
supervisorctl stop all: stop all processes.
supervisorctl update: According to the latest configuration file, start a new configuration or a modified process, the process without configuration changes will not be affected and restart

# Watch process service
ps -ef | grep supervisord

#Start supervisor, -c make configuration file to read
supervisord -c /etc/supervisord.d/long_script.conf

#Close the supervisor
supervisorctl shutdown

#Reload the supervisor configuration file and restart the supervisor
supervisorctl reload

# Set boot up
systemctl enable supervisord

4. Startup

systemctl enable supervisord

systemctl is-enabled supervisord
systemctl stop supervisord
systemctl start supervisord
systemctl status supervisord
systemctl reload supervisord
systemctl restart supervisord

systemctl restart supervisord
supervisorctl reload

five. Flask+Gunicorn+Nginx+Supervisord

1. Directory structure

$ tree -I "env|__pycache*| *.pyc" -FCL 3
.
|-- hello.py
`-- rungun.sh*

2. Main program

# hello.py

$ cat hello.py
from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello() :
return'

hello word

'

if __name__ =='__main__':
app.run(host='0.0.0.0',debug =True)

3. Define startup item

$ cat rungun.sh 
#!/bin/bash

cd /root/myapp
source env/bin/activate
gunicorn hello:app

4. Define supervisord

$ cat /etc/supervisord.d/hello.ini 
[program:hello]
command=/root/myapp/rungun.sh
autostart=true
autorestart=true
stdout_logfile=/var/log/supervisor/hello/hello_out.log
stderr_logfile=/var/log/supervisor/hello/hello_err.log

5. Configuration nginx

$ cat /etc/nginx/conf.d/hello.conf server {

listen 80;
server_name 49.234.220.252;
access_log / var/log/nginx/hello/access.log;
error_log /var/log/nginx/hello/error.log;

location / {< br /> proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-Ip $remote_addr;
}

}

6. Command

systemctl restart supervisord
supervisorctl reload

better solution

h2>

The above method can only be started, cannot be stopped or restarted

Redefine supervisord

$ cat /etc /supervisord.d/hello.ini 

[program:hello]
command=/root/myapp/env/bin/gunicorn hello:app
directory=/root/myapp
autostart=true
autorestart=true
stdout_logfile=/var/log/supervisor/hello/hello_out.log
stderr_logfile=/var/log/supervisor/hello/hello_err.log

Command

(env) [[email protected]_0_6_centos bin]# supervisorctl reload
Restarted supervisord
(env) [ [email protected]_0_6_centos bin]# supervisorctl stop hello
hello: stopped
(env) [[email protected]_0_6_centos bin]# supervisorctl star t hello
hello: started

Reference

[1]. Deploying a Flask Site Using NGINX Gunicorn, Supervisor and Virtualenv on Ubuntu

[2]. Centos7 installation supervisor detailed tutorial

[3]. python web deployment: nginx + gunicorn + supervisor + flask deployment notes

Contents

  • 1. Python
    • 1. Install Python3 from source
    • 2 . SCL install Python3
    • 3. Virtual environment venv
    • 4. Install Flask
    • 5. Install gunicorn
  • 2. Install Nginx
    • 1. Install Nginx
    • 2. Important instructions
  • 3. Set Nginx server
    • 1. Create directory structure
    • 2. Configure server
    • 3. Flask + Nginx + Gunicorn
  • Four 、Supervisor
    • 1. Install supervisor
    • 2. Custom configuration example
    • 3. Commands used
    • 4. Boot up< /li>
  • 5. Flask+Gunicorn+Nginx+Supervisord
    • 1. Directory structure
    • 2. Main program
    • 3. Define startup items
    • 4. Define supervisord
    • 5. Configure ngin x
    • 6. Command
  • Better solution
    • Redefine supervisord
    • Command
  • Reference

  • One, Python
    • 1. Source installation Python3
    • 2. SCL install Python3
    • 3. Virtual environment venv
    • 4. Install Flask
    • 5. Install gunicorn
  • 2. Install Nginx
    • 1. Install Nginx
    • 2. Important instructions
  • < li>Three, set up Nginx server

    • 1. Create directory structure
    • 2. Configure server
    • 3. Flask + Nginx + Gunicorn
  • 4. Supervisor
    • 1. Install supervisor
    • 2. Custom configuration example
    • 3. Instructions used
    • 4. Boot up
  • Five. Flask+Gunicorn+Nginx+Supervisord
    • 1. Directory structure
    • 2 . Main program
    • 3. Define startup items
    • 4. Define supervisord
    • 5. Configure nginx
    • 6. Command
  • Better solution
    • Redefine supervisord
    • Command
  • Reference
  • li>

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 4887 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.