在linux上部署web環境

1.升級python到2.7版本(經過源碼包從新安裝一個2.7版本的python):
wget https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz
tar -zxf Python-2.7.9.tgz
./configure --prefix=/usr/local/python27/
make && make install
which python(查看可執行文件的位置)
ln -s python2.7 python(創建軟連接)


2.讓yum使用舊版2.6版本的python
vim /usr/bin/yum
把首行註釋修改成 #!/usr/bin/python2.6

3.安裝pip
wget https://bootstrap.pypa.io/get-pip.py --no-check-certificate
python get-pip.py
提示錯誤:zipimport.ZipImportError: can't decompress data; zlib not available
此時須要:
a-安裝依賴zlib、zlib-devel
b-從新編譯python:
./configure
在這裏把Modules/Setup文件中的454行左右的,
#zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz
去掉註釋
zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz
make && make install
從新作軟連接,讓系統路徑的python指向新編譯的python2.7版本
python get-pip.py
又失敗了,提示:pip is configured with locations that require TLS/SSL
須要安裝2個依賴:yum -y install openssl openssl-devel
再編譯安裝一次。。。。。
python get-pip.py(終於成功!)
pip被安裝在python的可執行文件的同一目錄。
作軟連接 ln -s /usr/local/python27/bin/pip /usr/bin/pip

4.安裝虛擬環境virtualenv和flask
pip install virtualenv(安裝)
virtualenv --no-site-packages venv(創建虛擬環境,不安裝任何系統中的第三方包)

5.安裝nginx
yum install nginx(提示沒有可用的源)
解決方法:
安裝EPEL源(yum install epel-release)
又報錯:Cannot retrieve metalink for repository: epel. Please verify its path and try again
解決方法:
編輯/etc/yum.repos.d/epel.repo 取消baseurl的註釋,把mirrorlist註釋掉。
設置nginx開機啓動:
chkconfig nginx on
開啓80端口,並重啓iptables:
vim /etc/sysconfig/iptables   而後必定在第一個-A的行的上面一行添加一行,否則會失敗:-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
service iptables restart
啓動nginx:
service nginx start

6.安裝gunicorn
pip install gunicorn(安裝)
gunicorn -w 4 -b 127.0.0.1:端口號 文件名:app(直接運行gunicorn)
vim /etc/nginx/conf.d/default.conf(配置nginx):
server {
    listen 80;

    location / {
        proxy_pass http://127.0.0.1:888; # 這裏是指向 gunicorn host 的服務地址
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

}
重啓nginx,ok!能夠直接從80端口訪問網站。

7.使用supervisor
最後一步,用進程管理工具supervisor把gunicorn做爲服務來管理:
pip install supervisor
echo_supervisord_conf > supervisor.conf   # 生成 supervisor 默認配置文件
vim supervisor.conf                       # 修改 supervisor 配置文件,添加 gunicorn 進程管理
添加:
[program:入口文件名]
command=/root/aff/venv/bin/gunicorn -w4 -b0.0.0.0:888 入口文件名:app
directory=/root/aff

supervisord -c supervisor.conf(啓動supervisor)
supervisorctl shutdown (關閉supervisor)
再啓動時出現錯誤:Shut this program down first before starting supervisord.
解決辦法:
unlink /tmp/supervisor.sock
再啓動。

supervisor的基本使用命令

supervisord -c supervisor.conf                             經過配置文件啓動supervisor
supervisorctl -c supervisor.conf status                    察看supervisor的狀態
supervisorctl -c supervisor.conf reload                    從新載入 配置文件
supervisorctl -c supervisor.conf start [all]|[appname]     啓動指定/全部 supervisor管理的程序進程
supervisorctl -c supervisor.conf stop [all]|[appname]      關閉指定/全部 supervisor管理的程序進程


8.安裝mysql:
yum install -y mysql-server mysql mysql-devel (安裝)
service mysqld start (啓動)
chkconfig mysqld on (開機自啓動)
mysqladmin -u root password 'new-password' (給root設置密碼)

------------------------------------------
9.vim配置

vim配置(/etc/vimrc):
set nu(顯示行號)
set expandtab(把tab轉成空格)
set ts=4(一個tab等於4個空格)
set softtabstop=4(按退格時刪除4個空格)
set autoindent(自動縮進)
解決vim沒有顏色的方法:
緣由是securecrt使用的終端是VT100,而vim的配置中只有終端是xterm纔有顏色。
解決方法(修改配置文件):
vim ~/.bashrc
TERM=xterm
export TERM
而後用source命令重載一下配置文件。

10.重建數據庫
直接使用flask的數據庫模型重建:
python admin.py shell
from admin import db
db.create_all()python

相關文章
相關標籤/搜索