在阿里云服务器上配置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. 64位win2003/win2008系统IIS6.0/7.5配置PHP的方法

    64位win2003/win2008系统IIS6.0/7.5配置PHP的方法 32位的win2003系统配置PHP,估计很多人都已经驾轻就熟了,不过当遇到64位的系统时,估计又会遇上新的问题了.本文记 ...

  2. 读取Android设备的MAC地址

    读取Android设备的MAC地址   AndroidUtil.java package com.csdn.android.util; import com.csdn.android.framewor ...

  3. CCF 炉石传说(模拟)

    试题编号: 201612-3 试题名称: 炉石传说 时间限制: 1.0s 内存限制: 256.0MB 问题描述 <炉石传说:魔兽英雄传>(Hearthstone: Heroes of Wa ...

  4. 2017浙江省赛 A - Cooking Competition ZOJ - 3958

    地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3958 题目: "Miss Kobayashi's Drag ...

  5. intellij-idea+maven搭建scala环境

    一 . 安装JDK 1. 下载地址: http://download.oracle.com/otn-pub/java/jdk/8u181-b13/96a7b8442fe848ef90c96a2fad6 ...

  6. APP图标设计小技巧:在iOS上快速获得APP图标的真实预览图

    严格来说,这并不是一篇关于前端开发的文章,因为涉及到的知识非常浅.这只是一个向设计狮们分享的小经验,只是其中用到了一些前端内容. 最近接了个私活,了解到一个初创公司正在高价悬赏Logo(主要用于APP ...

  7. 在eclipse中new 对象后怎么通过快捷键自动生成返回对象

    如题,每次new 对象的时候不想手动补全返回对象,可以实现快捷键生成返回对象.new  对象后可以按住ctrl+1,如下图: 选择第一行即可.

  8. Visual C++的DLL

    动态链接库 (DLL) 是作为共享函数库的可执行文件. 动态链接提供了一种方法,使进程可以调用不属于其可执行代码的函数. 函数的可执行代码位于一个 DLL 中,该 DLL 包含一个或多个已被编译.链接 ...

  9. MVC中使用分部视图参数,改变分部视图连接样式

    MVC中使用分部视图参数,改变分部视图连接样式! Controller代码 [ChildActionOnly] public ActionResult Navigator(int tag) { ret ...

  10. 如何用纯 CSS 创作一个跳 8 字型舞的 loader

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/gKNMMm 可交互视频 此视频是可 ...