阿里雲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的環境管理工具,具體介紹可以找度娘。
Python程式碼- ~# pip install virtualenv
通過ssh以bob使用者登入
Python程式碼- 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服務
Python程式碼- (dylan)iZ28tu6p6vhZ% pip install Flask
- (dylan)iZ28tu6p6vhZ% vim runserver.py
- (dylan)iZ28tu6p6vhZ% chmod a+x runserver.py
runserver.py內容為:
Python程式碼- 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替代
Python程式碼- (dylan)iZ28tu6p6vhZ% pip install gunicorn
為dylan站點進行gunicorn配置
Python程式碼- (dylan)iZ28tu6p6vhZ% vim gunicorn.conf
gunicorn.conf檔案內容為:
Python程式碼- #工作程序數為3
- workers = 3
- #繫結本地8000埠
- bind = '127.0.0.1:8000'
6、安裝supervisor,並配置
使用root帳號進行按照
Python程式碼- ~# 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服務
Python程式碼- ~# touch /etc/supervisor/conf.d/dylan.conf
- ~# vim /etc/supervisor/conf.d/dylan.conf
dylan.conf內容為:
Python程式碼- [program:dylan]
- 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重新讀取所有配置
Python程式碼- ~# 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目錄
Python程式碼- (dylan)iZ28tu6p6vhZ% mkdir logs
- ~# sudo supervisorctl reread
- dylan: available
此時提示dylan應用可用,說明dylan的配置正確並生效了。接下來啟動dylan應用
Python程式碼- ~# sudo supervisorctl start dylan
- dylan: ERROR (no such process)
啟動dylan失敗,提示該程序不存在,那麼就重新載入下配置檔案之後再啟動
Python程式碼- ~# sudo supervisorctl reread
- ~# supervisorctl update
- ~# sudo supervisorctl start dylan
- dylan: started
到此dylan應用已經正常啟動了
7、安裝nginx並配置
安裝nginx並啟動
Python程式碼- ~# sudo apt-get install nginx
- ~# sudo service nginx start
- Starting nginx: nginx.
對dylan應用進行nginx相關的配置
Python程式碼- ~# touch /etc/nginx/sites-available/dylan.com
- ~# vim /etc/nginx/sites-available/dylan.com
dylan.com內容為:
Python程式碼- 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服務
Python程式碼- sudo service nginx restart
- Restarting nginx: nginx.
在自己的pc上wget,看看是否能正確獲取到資料
Python程式碼- ~$ sudo vim /etc/hosts
- ~$ cat /etc/hosts
- 127.0.0.1 localhost
- 115.28.xxx.xxx dylan.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的配置
Python程式碼- # 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,看下該目錄
Python程式碼- :/etc/nginx/sites-enabled# ls -l
- total 0
- 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程式碼- :/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 Feb 3 16:16 default -> /etc/nginx/sites-available/default
- lrwxrwxrwx 1 root root 36 Feb 3 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/