一、Python

1. 源安装 Python3

# 开发者工具
$ sudo yum -y install yum-utils
$ sudo yum-builddep python # 下载解压
$ 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/ # 编译安装
$ ./configure
$ make
$ sudo make install
$ python3 --version # 设置默认版本
$ alias python='/usr/local/bin/python3.6'

2. SCL安装 Python3

# 1. 激活SCL
$ sudo yum install centos-release-scl # 2.安装python3
$ sudo yum install rh-python36 # 3.使用python3 $ python --version
Python 2.7.5 $ scl enable rh-python36 bash
$ python --version
Python 3.6.3 # 4. 安装开发工具
$ sudo yum groupinstall 'Development Tools'

注意

此处设定python3版本,如果重新打开会话,会恢复默认的python2.7

设置默认

$ scl enable python36 <command>
$ scl enable python36 bash

3. 虚拟环境venv

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

4. 安装Flask

hello.py

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

5. 安装gunicorn

(env) [root@VM_0_6_centos myapp]$ pip3 install gunicorn
(env) [root@VM_0_6_centos myapp]# gunicorn hello:app

二、安装Nginx

1. 安装Nginx

# 1. 安装Nginx
yum -y install nginx
systemctl enable nginx
systemctl start nginx
systemctl status nginx # 2. 释放端口防火墙HTTP (`80`) and HTTPS (`443`) ports. firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload # 3. 浏览器`http://YOUR_IP`

2. 重要指令

# 开启服务
sudo systemctl start nginx·#无输出
sudo service start nginx #发行版命令 # 开机启动
sudo systemctl enable nginx # 关闭服务
sudo systemctl stop nginx
sudo service stop nginx # 重启
sudo systemctl restart nginx
sudo service restart nginx # 重新加载 更改 Nginx 的配置时,都需要重新加载或重新启动 Nginx
sudo systemctl reload nginx
sudo service reload nginx # 测试语法错误 sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful # 查看nginx状态 sudo systemctl status nginx # 查看nginx版本
sudo nginx -v # 输出 Nginx 版本以及配置选项
sudo nginx -V

指令小结:

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

三、设置 Nginx server

1. 创建目录结构

/var/www/
├── example.com
│ └── public_html
├── example2.com
│ └── public_html
├── example3.com
│ └── public_html
  1. 新建文件
mkdir -p /var/www/example.com/public_html
  1. 创建index.html
sudo nano /var/www/example.com/public_html/index.html

nano文本编辑器粘贴/var/www/example.com/public_html/index.html

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>案例测试</title>
</head>
<body>
<h1>成功部署</h1>
</body>
</html>
  1. 修改用户组
sudo chown -R nginx: /var/www/example.com

2. 配置server

Nginx 服务器块配置文件文件必须以.conf结尾,并存储在目录/etc/nginx/conf.d

1.新建example.com.conf

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

server {
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. 测试配置
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. 重启
sudo systemctl restart nginx

3. Flask + Nginx + Gunicorn

  1. 配置服务器
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. 启动

$ cd myapp/
$ source env/bin/activate # 创建日志文件夹
(env) $ mkdir -p /var/log/nginx/hello/ # 修改所有者
(env) $ sudo chown -R nginx: /var/log/nginx/hello (env) $ sudo nginx -t
(env) $ systemctl reload nginx
(env) $ gunicorn hello:app
  1. 查看进程
# pstree -ap|grep gunicorn

四、Supervisor

进程管理工具,方便的监听、启动、停止、重启一个或多个进程。

  • supervisor:要安装的软件的名称。
  • supervisord:装好supervisor软件后,supervisord用于启动supervisor服务。
  • supervisorctl:用于管理supervisor配置文件中program。

1. 安装supervisor

yum -y install supervisor
# 生成2个文件
`-- /etc/
|-- ...
|-- supervisord.conf # 配置文件
`-- supervisord.d/ # 配置文件夹

修改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 process 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. 自定义配置示例

  1. 测试程序结构
`-- dosupervisor/
|-- log/ # 日志文件夹
| |-- long.err.log #错误
| `-- long.out.log #输出
`-- long.sh # 调用程序 # 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. 配置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.err.log
stdout_logfile=/root/dosupervisor/log/long.out.log
  1. 查看状态
[root@VM_0_6_centos ~]# supervisorctl status
long_script RUNNING pid 16862, uptime 0:00:51
[root@VM_0_6_centos ~]# supervisorctl stop long_script
long_script: stopped
[root@VM_0_6_centos ~]# supervisorctl status
long_script STOPPED Aug 02 11:22 PM
  1. 滚动查看结果
tail -f long.out.log

3. 用到的指令

yum -y remove supervisor    #卸载
supervisord --version # 初始化配置
echo_supervisord_conf > /etc/supervisord.conf supervisord : 启动supervisor
supervisorctl reload :修改完配置文件后重新启动supervisor
supervisorctl status :查看supervisor监管的进程状态
supervisorctl start 进程名 :启动XXX进程
supervisorctl stop 进程名 :停止XXX进程
supervisorctl stop all:停止全部进程。
supervisorctl update:根据最新的配置文件,启动新配置或有改动的进程,配置没有改动的进程不会受影响而重启 # 看进程服务
ps -ef | grep supervisord #启动supervisor,-c制定让其读取的配置文件
supervisord -c /etc/supervisord.d/long_script.conf #关闭supervisor
supervisorctl shutdown #重新加载supervisor配置文件,并重启superivisor
supervisorctl reload # 设置开机启动
systemctl enable supervisord

4. 开机启动

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

五、Flask+Gunicorn+Nginx+Supervisord

1. 目录结构

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

2. 主程序

# hello.py

$ cat hello.py
from flask import Flask app = Flask(__name__) @app.route('/')
def hello():
return '<h1>hello word</h1>' if __name__ == '__main__':
app.run(host='0.0.0.0',debug=True)

3. 定义启动项

$ cat rungun.sh
#!/bin/bash cd /root/myapp
source env/bin/activate
gunicorn hello:app

4. 定义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. 配置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 / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-Ip $remote_addr;
} }

6. 命令

systemctl restart supervisord
supervisorctl reload

更优方案

上述方法只能启动,无法停止或者重启

重新定义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

命令

(env) [root@VM_0_6_centos bin]# supervisorctl reload
Restarted supervisord
(env) [root@VM_0_6_centos bin]# supervisorctl stop hello
hello: stopped
(env) [root@VM_0_6_centos bin]# supervisorctl start hello
hello: started

参考

[1]. How To Install Nginx on CentOS 7

[2]. How to Install Python 3 on CentOS 7

[3]. How to install Python3 on CentOS

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

[5]. centos7安装supervisor详细教程

[6]. python web 部署:nginx + gunicorn + supervisor + flask 部署笔记

Centos系统Python环境搭建和项目部署的更多相关文章

  1. Linux环境搭建及项目部署

    一. VMWare安装图解 1.点击下一步 2.接受条款,下一步 3.选择安装目录,不建议有中文目录和空格目录.下一步 4.下一步 5.这两个选项根据可以爱好习惯选择,下一步 6.安装 7.完成 9. ...

  2. Windows系统Python环境搭建

    Python下载 下载地址:https://www.python.org/downloads/ 选择需要下载的版本 以Python3.3.7版本为例,下载64位和32位都分别有三种方式,依次是压缩包, ...

  3. Python环境搭建、python项目以docker镜像方式部署到Linux

    Python环境搭建.python项目以docker镜像方式部署到Linux 本文的项目是用Python写的,记录了生成docker镜像,然后整个项目在Linux跑起来的过程: 原文链接:https: ...

  4. Python环境搭建(windows)

    Python环境搭建(windows) Python简介 Python(英国发音:/ˈpaɪθən/ 美国发音:/ˈpaɪθɑːn/),是一种面向对象.直译式计算机编程语言,具有近二十年的发展历史,成 ...

  5. selenium win7+selenium2.0+python环境搭建

    win7+selenium2.0+python环境搭建 by:授客 QQ:1033553122 步骤1:下载python 担心最新版的支持不太好,这里我下载的是python 2.7(selenium之 ...

  6. 第一章:起步(python环境搭建)

    Python 环境搭建 学习python的第一步,就是要学习python开发环境的配置,在配置好python开发环境后,你需要再安装一款比较趁手的编辑器,事实上,python解释器本身就可以进行一些编 ...

  7. Python学习--Python 环境搭建

    Python环境搭建 Python是跨平台的编程语言,可应用于Windows.Linux.Mac OS X.你可以通过终端窗口输入"python"命令来查看本地是否安装了Pytho ...

  8. Python学习第一弹——Python环境搭建

    一.Python简介: Python,是一种面向对象.解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年.Python语法简洁而清晰,具有 ...

  9. Centos 基础开发环境搭建之Maven私服nexus

    hmaster 安装nexus及启动方式 /usr/local/nexus-2.6.3-01/bin ./nexus status Centos 基础开发环境搭建之Maven私服nexus . 软件  ...

随机推荐

  1. python中关于传递参数模块argprase的一些小坑

    今天在写代码的时候遇到了一个关于parser的一些小坑,记录在此备用. 我们知道在python中可以用argprase来传递一些参数给代码执行,来看下面的例子,假设现在有一个test文件夹,下面有3个 ...

  2. cmake 2.8.12在redhat 4.4下安装

    以前安过,忘了,今天记笔记这里

  3. python web 分页组件

    闲来无事便写了一个易使用,易移植的Python Web分页组件.使用的技术栈是Python.Django.Bootstrap. 既然是易使用.易移植的组件,首先介绍一下其在django框架中的调用方式 ...

  4. Python运算符,逻辑运算

    运算符 计算机可以进行的运算有很多种,可不只加减乘除这么简单,运算按种类可分为算数运算.比较运算.逻辑运算.赋值运算.成员运算.身份运算.位运算,今天我们暂只学习算数运算.比较运算.逻辑运算.赋值运算 ...

  5. np一些基本操作2

    import numpy as nparr1 = np.arange(32).reshape(8,4)print(arr1)arr1 = arr1.reshape(-1);print(arr1)arr ...

  6. pptp,l2tp获取登录用户信息用pppd参数即可

    这个问题困扰了我很久,终于在pppd的man文档里,发现了踪迹.在man中的SCRIPTS下有一系列的参数,其中PEERNAME就是登陆的用户名,并且在/etc/ppp/ip-up和/etc/ppp/ ...

  7. day48作业

    使用Bootstrap框架编写一个简单的web静态页面 效果图: <!DOCTYPE html> <html lang="en"> <head> ...

  8. PHP--封装干净利落的输出dump()函数

    /** * 打印函数 * */ if (! function_exists ( 'dump' )) { function dump($var, $exit = true) { echo '<pr ...

  9. [转载] 使用C/C++语言编写基于DSP程序的注意事项

    原文地址:『转』使用C/C++语言编写基于DSP程序的注意事项作者:skysmile   1.不影响执行速度的情况下,可以使用c或c/c++语言提供的函数库,也可以自己设计函数,这样更易于使用“裁缝师 ...

  10. ArrayList基础知识

    ArrayList简介 ArrayList 的底层是数组队列,相当于动态数组.与 Java 中的数组相比,它的容量能动态增长.在添加大量元素前,应用程序可以使用ensureCapacity操作来增加 ...