1. 程式人生 > >阿里雲ECS上環境搭建(virtualenv+flask+gunicorn+supervisor+nginx)

阿里雲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. app = Flask(__name__)  
  3. @app.route('/')  
  4. def hello_world():  
  5.     return 'Hello World!'  
  6. if __name__ == '__main__':  
  7.     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.     root /home/bob/dylan/;  
  5.     access_log /home/bob/logs/access.log;  
  6.     error_log /home/bob/logs/access.log;  
  7.     location / {  
  8.         proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;  
  9.         proxy_set_header Host $http_host;  
  10.         proxy_redirect off;  
  11.         if (!-f $request_filename) {  
  12.             proxy_pass http://127.0.0.1:8000;  
  13.             break;  
  14.         }  
  15.     }  
  16. }  

     重啟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. ~$ wget -c http://dylan.com  
  6. ~$ cat index.html   
  7. <html>  
  8. <head>  
  9. <title>Welcome to nginx!</title>  
  10. </head>  
  11. <body bgcolor="white" text="black">  
  12. <center><h1>Welcome to nginx!</h1></center>  
  13. </body>  
  14. </html>  

     獲取的並不是Hello world,而是nginx的資訊,顯然是dylan應用配置沒有被nginx載入,檢視nginx的配置

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

     我們當時是把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/