zhk2369 发表于 2016-12-27 09:24:05

阿里云ECS上环境搭建(virtualenv+flask+gunicorn+supervisor+nginx)

  阿里云ECS目前有新用户免费半年使用的活动,就利用闲暇时间申请了一台,具体申请可到http://free.aliyun.com/?spm=5176.383518.1.28.OXp5YZ。
我选择的配置是:
CPU:  1核
内存:  1GB
数据盘:  0G
操作系统:  Ubuntu 12.04 64位
当前使用带宽: 0Mbps

当然由于是免费的,所以带宽是0Mbps,这样就没有外网ip,也不能从外网安装环境,最好通过升级带宽为1Mbps,我的目的是在ECS上搭建flask环境,所以就升级了两天的1Mbps,费用也就是0.36元。这样就有了外网ip,可以通过ssh进行远程登录并管理。

接下来就开始搭建环境:
1、安装pip
    pip是python的包管理工具,具体介绍可以找度娘。
   

~# sudo apt-get install python-pip

  2、新创建一个用户bob
    先按照zsh作为bob的默认shell

~# sudo apt-get install zsh
~# wget --no-check-certificate https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | sh
~# useradd bob -d /home/bob -m -s /bin/zsh
~# passwd bob
  3、为应用创建独立开发环境
  virtualenv则是python的环境管理工具,具体介绍可以找度娘。

~# pip install virtualenv
  通过ssh以bob用户登录

iZ28tu6p6vhZ% virtualenv dylan
New python executable in dylan/bin/python
Installing setuptools, pip...done.
iZ28tu6p6vhZ% cd dylan
iZ28tu6p6vhZ% . ./bin/activate
(dylan)iZ28tu6p6vhZ%

  4、安装Flask,并写一个helloworld服务

(dylan)iZ28tu6p6vhZ% pip install Flask
(dylan)iZ28tu6p6vhZ% vim runserver.py
(dylan)iZ28tu6p6vhZ% chmod a+x runserver.py
  runserver.py内容为:

from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run()
  5、按照gunicorn,并配置应用
  gunicorn是用于部署WSGI应用的,此处也可以使用uWSGI替代

(dylan)iZ28tu6p6vhZ% pip install gunicorn
  为dylan站点进行gunicorn配置

(dylan)iZ28tu6p6vhZ% vim gunicorn.conf
  gunicorn.conf文件内容为:

#工作进程数为3
workers = 3
#绑定本地8000端口
bind = '127.0.0.1:8000'
  6、安装supervisor,并配置
  使用root帐号进行按照

~# useradd bob -d /home/bob -m -s /bin/zsh
~# sudo service supervisor stop
Stopping supervisor: supervisord.
~# sudo service supervisor start
Starting supervisor: supervisord.
  对dylan应用配置supervisor服务

~# touch /etc/supervisor/conf.d/dylan.conf
~# vim /etc/supervisor/conf.d/dylan.conf
  dylan.conf内容为:


command=/home/bob/dylan/bin/gunicorn runserver:app -c /home/bob/dylan/gunicorn.conf
directory=/home/bob/dylan
user=bob
autostart=true
autorestart=true
stdout_logfile=/home/bob/logs/gunicorn_supervisor.log
  然后是supervisor重新读取所有配置

~# sudo supervisorctl reread
ERROR: CANT_REREAD: The directory named as part of the path /home/bob/logs/gunicorn_supervisor.log does not exist.
  但是此时出现了一个错误,提示"/home/bob/logs/guniconf_supervisor.log文件不存在”,查看下/home/bob目录下确实不存在logs这个目录,那么新创建一个logs目录

(dylan)iZ28tu6p6vhZ% mkdir logs

~# sudo supervisorctl reread
dylan: available

  此时提示dylan应用可用,说明dylan的配置正确并生效了。接下来启动dylan应用

~# sudo supervisorctl start dylan
dylan: ERROR (no such process)

  启动dylan失败,提示该进程不存在,那么就重新加载下配置文件之后再启动

~# sudo supervisorctl reread
~# supervisorctl update
~# sudo supervisorctl start dylan
dylan: started
  到此dylan应用已经正常启动了
  7、安装nginx并配置
  安装nginx并启动

~# sudo apt-get install nginx
~# sudo service nginx start
Starting nginx: nginx.
  对dylan应用进行nginx相关的配置

~# touch /etc/nginx/sites-available/dylan.com
~# vim /etc/nginx/sites-available/dylan.com
  dylan.com内容为:

server {
listen   80;
server_name dylan.com;
root /home/bob/dylan/;
access_log /home/bob/logs/access.log;
error_log /home/bob/logs/access.log;
location / {
proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://127.0.0.1:8000;
break;
}
}
}

  重启nginx服务

sudo service nginx restart
Restarting nginx: nginx.
  在自己的pc上wget,看看是否能正确获取到数据

~$ sudo vim /etc/hosts
~$ cat /etc/hosts
127.0.0.1localhost
115.28.xxx.xxxdylan.com
~$ wget -c http://dylan.com
~$ cat index.html
<html>
<head>
<title>Welcome to nginx!</title>
</head>
<body bgcolor="white" text="black">
<center><h1>Welcome to nginx!</h1></center>
</body>
</html>
  获取的并不是Hello world,而是nginx的信息,显然是dylan应用配置没有被nginx加载,查看nginx的配置

# cat /etc/nginx/nginx.conf
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}

  我们当时是把dylan.com放在的sites-available,并不是放在sites-enabled,看下该目录

:/etc/nginx/sites-enabled# ls -l
total 0
lrwxrwxrwx 1 root root 34 Feb3 16:16 default -> /etc/nginx/sites-available/default
  可见sites-enable下的default也是一个指向sites-available目录下default的软链接,那我们也在该目录下创建一个只想dylan.com的软链接,并重启nginx服务

:/etc/nginx/sites-enabled# ln -s /etc/nginx/sites-available/dylan.com ./dylan.com
:/etc/nginx/sites-enabled# ls -l
total 0
lrwxrwxrwx 1 root root 34 Feb3 16:16 default -> /etc/nginx/sites-available/default
lrwxrwxrwx 1 root root 36 Feb3 16:54 dylan.com -> /etc/nginx/sites-available/dylan.com
:/etc/nginx/sites-enabled# sudo service nginx restart
Restarting nginx: nginx.
  然后在本地的pc上请求http://dylan.com,就可以看到返回‘Hello world’的信息了。
  到此,大功告成了。
  相关资料:
  http://itony.me/559.html
  http://beiyuu.com/vps-config-python-vitrualenv-flask-gunicorn-supervisor-nginx/
页: [1]
查看完整版本: 阿里云ECS上环境搭建(virtualenv+flask+gunicorn+supervisor+nginx)