阿里云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的包管理工具,具体介绍可以找度娘。
   

Python代码   收藏代码
  1. ~# sudo apt-get install python-pip  

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

 

   

Python代码   收藏代码
  1. # sudo apt-get install zsh  
  2. ~# wget --no-check-certificate https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | sh  
  3. ~# useradd bob -d /home/bob -m -s /bin/zsh  
  4. ~# passwd bob   

 

 3、为应用创建独立开发环境

    virtualenv则是python的环境管理工具,具体介绍可以找度娘。

   

Python代码   收藏代码
  1. ~# pip install virtualenv   

    通过ssh以bob用户登录

 

   

Python代码   收藏代码
  1. iZ28tu6p6vhZ% virtualenv dylan  
  2. New python executable in dylan/bin/python  
  3. Installing setuptools, pip...done.  
  4. iZ28tu6p6vhZ% cd dylan   
  5. iZ28tu6p6vhZ% . ./bin/activate  
  6. (dylan)iZ28tu6p6vhZ%   

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

 

   

Python代码   收藏代码
  1. (dylan)iZ28tu6p6vhZ% pip install Flask  
  2. (dylan)iZ28tu6p6vhZ% vim runserver.py  
  3. (dylan)iZ28tu6p6vhZ% chmod a+x runserver.py  

     runserver.py内容为:

 

   

Python代码   收藏代码
  1. from flask import Flask  
  2.   
  3. app = Flask(__name__)  
  4.  
  5. @app.route('/')  
  6. def hello_world():  
  7.     return 'Hello World!'  
  8.   
  9. if __name__ == '__main__':  
  10.     app.run()  

 5、按照gunicorn,并配置应用

 

    gunicorn是用于部署WSGI应用的,此处也可以使用uWSGI替代

   

Python代码   收藏代码
  1. (dylan)iZ28tu6p6vhZ% pip install gunicorn  

     为dylan站点进行gunicorn配置

Python代码   收藏代码
  1. (dylan)iZ28tu6p6vhZ% vim gunicorn.conf  

     gunicorn.conf文件内容为:

Python代码   收藏代码
  1. #工作进程数为3  
  2. workers = 3  
  3. #绑定本地8000端口  
  4. bind = '127.0.0.1:8000'  

 6、安装supervisor,并配置

 

    使用root帐号进行按照

Python代码   收藏代码
  1. ~# useradd bob -d /home/bob -m -s /bin/zsh  
  2. ~# sudo service supervisor stop  
  3. Stopping supervisor: supervisord.  
  4. ~# sudo service supervisor start  
  5. Starting supervisor: supervisord.  

     对dylan应用配置supervisor服务

Python代码   收藏代码
  1. ~# touch /etc/supervisor/conf.d/dylan.conf  
  2. ~# vim /etc/supervisor/conf.d/dylan.conf   

     dylan.conf内容为:

Python代码   收藏代码
  1. [program:dylan]  
  2. command=/home/bob/dylan/bin/gunicorn runserver:app -c /home/bob/dylan/gunicorn.conf  
  3. directory=/home/bob/dylan  
  4. user=bob  
  5. autostart=true  
  6. autorestart=true  
  7. stdout_logfile=/home/bob/logs/gunicorn_supervisor.log  

     然后是supervisor重新读取所有配置

Python代码   收藏代码
  1. ~# sudo supervisorctl reread  
  2. 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目录

Python代码   收藏代码
  1. (dylan)iZ28tu6p6vhZ% mkdir logs  

    

Python代码   收藏代码
  1. ~# sudo supervisorctl reread  
  2. dylan: available  

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

Python代码   收藏代码
  1. ~# sudo supervisorctl start dylan  
  2. dylan: ERROR (no such process)  

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

Python代码   收藏代码
  1. # sudo supervisorctl reread  
  2. # supervisorctl update  
  3. # sudo supervisorctl start dylan  
  4. dylan: started  

     到此dylan应用已经正常启动了

7、安装nginx并配置

    安装nginx并启动

Python代码   收藏代码
  1. ~# sudo apt-get install nginx  
  2. ~# sudo service nginx start  
  3. Starting nginx: nginx.  

     对dylan应用进行nginx相关的配置

Python代码   收藏代码
  1. ~# touch /etc/nginx/sites-available/dylan.com  
  2. ~# vim /etc/nginx/sites-available/dylan.com   

     dylan.com内容为:

Python代码   收藏代码
  1. server {  
  2.     listen   80;  
  3.     server_name dylan.com;  
  4.       
  5.     root /home/bob/dylan/;  
  6.     access_log /home/bob/logs/access.log;  
  7.     error_log /home/bob/logs/access.log;  
  8.   
  9.     location / {  
  10.         proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;  
  11.         proxy_set_header Host $http_host;  
  12.         proxy_redirect off;  
  13.         if (!-f $request_filename) {  
  14.             proxy_pass http://127.0.0.1:8000;  
  15.             break;  
  16.         }  
  17.     }  
  18. }  

     重启nginx服务

Python代码   收藏代码
  1. sudo service nginx restart  
  2. Restarting nginx: nginx.  

 

    在自己的pc上wget,看看是否能正确获取到数据

Python代码   收藏代码
  1. ~$ sudo vim /etc/hosts  
  2. ~$ cat /etc/hosts  
  3. 127.0.0.1   localhost  
  4. 115.28.xxx.xxx  dylan.com  
  5.   
  6. ~$ wget -c http://dylan.com  
  7. ~$ cat index.html   
  8. <html>  
  9. <head>  
  10. <title>Welcome to nginx!</title>  
  11. </head>  
  12. <body bgcolor="white" text="black">  
  13. <center><h1>Welcome to nginx!</h1></center>  
  14. </body>  
  15. </html>  

     获取的并不是Hello world,而是nginx的信息,显然是dylan应用配置没有被nginx加载,查看nginx的配置

Python代码   收藏代码
  1. # cat /etc/nginx/nginx.conf  
  2.         ##  
  3.         # Virtual Host Configs  
  4.         ##  
  5.   
  6.         include /etc/nginx/conf.d/*.conf;  
  7.         include /etc/nginx/sites-enabled/*;  
  8. }  

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

Python代码   收藏代码
  1. :/etc/nginx/sites-enabled# ls -l  
  2. total 0  
  3. lrwxrwxrwx 1 root root 34 Feb  3 16:16 default -> /etc/nginx/sites-available/default  

     可见sites-enable下的default也是一个指向sites-available目录下default的软链接,那我们也在该目录下创建一个只想dylan.com的软链接,并重启nginx服务

Python代码   收藏代码
  1. :/etc/nginx/sites-enabled# ln -s /etc/nginx/sites-available/dylan.com ./dylan.com  
  2. :/etc/nginx/sites-enabled# ls -l  
  3. total 0  
  4. lrwxrwxrwx 1 root root 34 Feb  3 16:16 default -> /etc/nginx/sites-available/default  
  5. lrwxrwxrwx 1 root root 36 Feb  3 16:54 dylan.com -> /etc/nginx/sites-available/dylan.com  
  6. :/etc/nginx/sites-enabled# sudo service nginx restart  
  7. 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/

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值