在阿里云服务器上配置CentOS+Nginx+Python+Flask环境

项目运行环境

阿里云(单核CPU, 1G内存, Ubuntu 14.04 x64 带宽1Mbps), 具体购买和ssh连接阿里云本文不做描述。

实用工具

首先进入阿里云后先要升级下apt-get, 并下载所需软件

1
2
sudo apt-get update
sudo apt-get install vim git wget tmux

我还会使用zsh和oh-my-zsh来替换bash

1
2
3
4
sudo apt-get install zsh
 
# 终端下打以下命令
wget --no-check-certificate https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | sh

替换bash为zsh

1
chsh -s /bin/zsh

重新连接阿里云就可以看到效果, 具体主题可以根据自己喜好更改主目录下的.zshrc即可

安装python相关模块

使用python的pip包管理工具

1
2
3
sudo apt-get install python-setuptools
sudo apt-get install python-pip
sudo pip install python-virtualenv

在主目录下创建文件夹code并把所有项目放进这个目录里

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
mkdir code
 
# 使用virtualenv
vritualenv venv
 
# 开启virtualenv
source /venv/bin/activate
 
# 关闭virtualenv
deactivate
 
# 在开启virtualenv的状态下使用pip会使用venv中的pip,也就是所有模块都会安装到venv中
# 如果想要安装全局模块就使用sudo pip
 
# 安装flask, 安装到venv中
pip install flask
 
# 两个是不同的, 安装到系统中
sudo pip install flask

使用Flask

现在我们已经安装了Flask, 现在我们写一个小的程序来运行一下。
这里我们在一次安装flask的相关模块,并把他们写入到requirements.txt这个文件中。

1
2
3
4
5
6
7
8
9
10
11
# 安装flask的相关模块
pip install -r requirements
 
# 创建一个manage.py文件, 用作管理我们的应用, 然后创建我们的一个应用。
touch manage.py
 
## 创建一个叫bamboo的应用
mkdir bamboo
 
## 创建一个应用的文件, 接下来我们要编辑这个文件了。
touch bamboo/__init__.py bamboo/app.py

项目目录结构是这样的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/root/code/
├── bamboo
│  ├── app.py
│  ├── config.py
│  ├── __init__.py
│  ├── static
│  ├── templates
│  └── views
├── gunicorn.conf
├── logs
│  ├── access.log
│  ├── error.log
│  ├── supervisord.log
│  ├── supervisor_err.log
│  └── supervisor_out.log
├── manage.py
├── requirements.txt
├── run.py
├── sweep.py
└── venv

编辑bamboo/app.py和bamboo/views/main.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
## app.py
from flask import Flask
 
def create_app():
  app = Flask(__name__)
 
  # do something
 
  register_blueprints(app)
  return app
 
 
# 用来注册视图的路由
def register_blueprints(app):
  from bamboo.views import main, error
  for i in (main, error):
    app.register_blueprint(i.bp)
 
## main.py
from flask import render_template, Blueprint
 
bp = Blueprint('main', __name__)
 
 
@bp.route('/')
@bp.route('/index')
def index():
  return '<h1>Hello, Bamboo!</h1>'

安装Gunicorn

Gunicorn是一个wsgi服务器, 我们将通过它来启动我们的web服务。

1
2
# 注意我们是通过pip来安装,所以该模块所有文件都是在venv里面
pip install gunicorn

可以进入目录vevn/bin下查看是否存在gunicorn命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 这里我们编辑下manage.py文件,具体文件路径请查看上文中的目录结构
## manage.py
 
from bamboo.app import create_app
from flask.ext.script import Manager, Server, Shell
 
app = create_app()
manager = Manager(app)
 
def make_shell_context():
  # 用于shell使用
  pass
 
manager.add_command('shell', Shell(make_context=make_shell_context))
manager.add_command('runserver', Server(
  use_debugger=True,
  use_reloader=True,
  host='127.0.0.1',
  port=5000
))
 
if __name__ == '__main__':
  manager.run()

运行一下manage.py

1
2
3
4
5
6
# 不适用gunicorn运行
python manage.py runserver
 
# 使用gunicorn 运行
# 这里需要注意一下, 冒号前面的是文件名也就是manage.py而后面的是应用的名称。我在这里载过跟头。因为服务器需要接受一个wsgi的应用而manager = Manager(app)不是一个wsgi应用所以报错
gunicorn manage:app

我们创建一个gunicorn配置文件gunicorn.conf并把他放到code目录下

1
2
3
4
## gunicorn.conf
 
workers=3
bind='127.0.0.1:5000'

安装Nginx

我们通过nginx来反向代理我们的服务。

1
2
# 首先下载一个nginx
sudo apt-get install nginx

我们可以访问我们的域名或IP地址,如果出现Nginx的提示就表示安装成功了。
nginx安装在/etc/nginx/目录下, 我们把项目的配置文件放到/etc/nginx/sites-available/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
server{
  listen 80;
  server_name xxx.xxx.xxx.xxx;
 
  # nginx log信息, 需要创建logs目录
  access_log /root/code/logs/access.log
  error_log /root/code/logs/error.log
 
  location / {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header HOST $host;
    proxy_redirect off;
    if (!-f $request_filename){
      proxy_pass http://127.0.0.1:5000;
      break;
    }
  }
}

需要创建一个软链接到/etc/nginx/sites-enabled/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 这里bamoo只是文件名你也可以取自己的项目名称
ln -s /etc/nginx/sites-available/bamboo /etc/nginx/sites-enabled/bamboo
 
# 然后检查nginx有没有错误
nginx -t
 
# 重新启动服务
service nginx restart
安装Supervisor
 
supervisor用来监控进程,并在进程挂掉的时候自动重启它。
 
# 这里需要把它安装到系统中
sudo pip install supervisor
 
# 生成配置文件
sudo echo_supervisord_conf > /etc/supervisord.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 编辑/etc/supervisord.conf,并在最后一行加入一下字段
# 这样配置文件会将/etc/supervisor/conf.d下所有.conf结尾的都会导入进来
[include]
files = /etc/supervisor/conf.d/*.conf
 
# 在创建一个配置文件到/etc/supervisor/conf.d/bamboo.conf
[program:bamboo]
command=/root/code/venv/bin/gunicorn manage:app -c /root/code/gunicorn.conf
directory=/root/code/
user=root
autostart=true
autorestart=true
stdout_logfile=/root/code/logs/supervisor_out.log
stderr_logfile=/root/code/logs/supervisor_err.log

通过supervisorctl工具用来管理supervisor维护的进程

1
2
3
4
5
6
7
# reread来检测修改的配置内容, update来更新
# 这样就可以看到bamboo的process启动了
supervisorctl reread
supervisorctl update
 
# 也可以通过supervisorctl status 查看所有的项目进程
bamboo              RUNNING  pid 12928, uptime 2:13:05

在阿里云服务器上配置CentOS+Nginx+Python+Flask环境的更多相关文章

  1. 在阿里云服务器上(centos 8) 安装自己的MQTT服务器 (mosquitto)

    layout: post title: 在阿里云服务器上(centos 8) 安装自己的MQTT服务器 (mosquitto) subtitle: date: 2020-3-2 author: Dap ...

  2. 阿里云服务器上配置并使用: PHP + Redis + Mysql 从配置到使用

    (原创出处为本博客,http://www.cnblogs.com/linguanh/) 目录: 一,下载 二,解压 三,配置与启动 四,测试 Redis 五,配置 phpRedis 扩展 六,综合测试 ...

  3. 阿里云服务器连接以及centos 搭建 web java环境(linux java部署 tomcat部署)

    版权声明:本文为博主原创文章,未经博主允许不得转载. 最近弄了个试用阿里云服务器倒腾了半天终于部署好,分享一下. 1.登入阿里云打开你申请的是云服务器的实例: 点击重置密码---重置密码后重启服务器才 ...

  4. 阿里云服务器上搭建seafile专业版

    因为官方一键安装教程在阿里云服务器上无法安装,由于水平有限,无法解决,所以选择手动安装 参考资料: 1,.腾讯云搭建seafile服务器 2.How to Install Seafile with N ...

  5. 【Linux 操作系统】阿里云服务器 操作实战 部署C语言开发环境(vim配置,gcc) 部署J2EE网站(jdk,tomcat)

    . 作者 :万境绝尘  转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/18964835 . 博客总结 : 设置SecureCRT ...

  6. Linux学习2-在阿里云服务器上部署禅道环境

    前言 以前出去面试总会被问到:测试环境怎么搭建?刚工作1-2年不会搭建测试环境还可以原谅自己,工作3-5年后如果还是对测试环境搭建一无所知,面试官会一脸的鄙视. 本篇以最简单的禅道环境搭建为例,学习下 ...

  7. 阿里云服务器 操作实战 部署C语言开发环境(vim配置,gcc) 部署J2EE网站(jdk,tomcat)

    . 作者 :万境绝尘  转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/18964835 . 博客总结 : 设置SecureCRT ...

  8. 阿里云服务器上安装mysql的心路历程(博友们进来看看哦)

    在阿里云花了100买了一台云服务器,配置如下: CPU: 1核 内存: 512MB 数据盘: 0G 带宽: 1Mbps 阿里云服务器安装mysql搞得我想吐血,搞了一个多星期,现在才搞好,而且,还有许 ...

  9. jdbc连接阿里云服务器上的MySQL数据库 及 数据库IP限制

    问题1:Jdbc 如何连接阿里云服务器上的MySQL数据库? 解决: 上截图: 其中IP是阿里云服务器的公网IP地址. 问题2:   刚开始接手开发的时候,使用Navicat连接阿里云服务器上的数据后 ...

随机推荐

  1. [转]DNS服务器原理详解与Centos6.x下搭建DNS服务器

    转自:http://blog.it985.com/8958.html DNS 数据库的记录:正解,反解, Zone 的意义 通过DNS解析过程详解这篇文章,我们知道了要想访问www.zmit.cn,最 ...

  2. NGUI基本事件

    You can add the following functions to your scripts placed on widgets or in-game objects with a coll ...

  3. RenderScript多输入参数

    https://stackoverflow.com/questions/20783830/how-to-use-renderscript-with-multiple-input-allocations ...

  4. HCNP学习笔记之子网划分 VLSM CIDR

    子网划分.VLSM可变长子网掩码.CIDR无类域间路由是学习网络知识或者说是学习路由知识所必备的,但很多朋友说这三者理论性太强了,不好掌握.本文将结合实例讲解子网划分的方法并对VLSM和CIDR进行简 ...

  5. 从页面到服务器,node实现文件下载

    起因: 新来了一个需求,让用户下载一个200m的zip文件,并且校验用户信息,难点:下载的文件是200M的. 现在维护的系统,以前的文件下载,走的是node的静态文件,用的express框架上自带的静 ...

  6. uboot dm9000驱动故障

    手头有一块6410开发板,已经有别人提供的uboot代码(基于2011.06),但是在检测dm9000时显示下面的输出: Net: No ethernet found. 当然其他网络命令例如ping等 ...

  7. Qt+VS编译出错:Two or more files with the name of moc_Geometry.cpp will produce outputs to the same location

    warning MSB8027: Two or more files with the name of moc_Geometry.cpp will produce outputs to the sam ...

  8. 教你如何挑选深度学习GPU【转】

    本文转载自:https://blog.csdn.net/qq_38906523/article/details/78730158 即将进入 2018 年,随着硬件的更新换代,越来越多的机器学习从业者又 ...

  9. LeetCode——3Sum

    1. Question Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? ...

  10. 素数分布 - nefu 117

    素数个数的位数 - nefu 117 普及一个公式: 位数公式:要求一个数x的位数,用公式:lg(x)+1 素数分布:n/ln(n) 所以直接求解n/ln(n)的位数就可以了 代码如下: #inclu ...