這一篇來到了專案部署的時候,開發的過程之中就可以隨時的更新到測試伺服器,可以給QA、PM、顧客,查看目前的完成進度,這邊採用nginx+pserve+supervisor方式來部署專案,先介紹會用到的幾個軟體:
nginx_ithome_pellok.py : nginx 設定檔
upstream ithome_pellok {
server 127.0.0.1:5000;
server 127.0.0.1:5001;
}
server {
listen 80;
# optional ssl configuration
#listen 443 ssl;
#ssl_certificate /etc/nginx/ssl/certificate.crt;
#ssl_certificate_key /etc/nginx/ssl/private.key;
# end of optional ssl configuration
server_name _;
location /robots.txt {
alias /home/project/ithome_pellok_2018/ithome_pellok_2018/static/robots.txt;
}
location /static {
root /home/project/ithome_pellok_2018/ithome_pellok_2018;
expires 30d;
add_header Cache-Control public;
access_log off;
}
location / {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
client_max_body_size 100m;
client_body_buffer_size 128k;
proxy_connect_timeout 60s;
proxy_send_timeout 90s;
proxy_read_timeout 90s;
proxy_buffering off;
proxy_temp_file_write_size 64k;
proxy_pass http://ithome_pellok;
proxy_redirect off;
}
}
supervisord.conf : supervisor 設定檔
[unix_http_server]
file=%(here)s/supenv/supervisor.sock
[inet_http_server]
port=127.0.0.1:9001
[supervisord]
pidfile=%(here)s/supenv/supervisord.pid
logfile=%(here)s/supenv/supervisord.log
logfile_maxbytes=50MB
logfile_backups=10
loglevel=info
nodaemon=false
minfds=1024
minprocs=200
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix://%(here)s/supenv/supervisor.sock
[program:ithome_pellok_2018]
autorestart=true
command=%(here)s/env/bin/pserve %(here)s/production.ini http_port=50%(process_num)02d
process_name=%(program_name)s-%(process_num)01d
numprocs=2
numprocs_start=0
redirect_stderr=true
stdout_logfile=%(here)s/log/%(program_name)s-%(process_num)01d.log
production.ini : 正式設定檔
###
# app configuration
# https://docs.pylonsproject.org/projects/pyramid/en/1.9-branch/narr/environment.html
###
[app:ithome_pellok_2018]
use = egg:ithome_pellok_2018#main
pyramid.reload_templates = false
pyramid.debug_authorization = false
pyramid.debug_notfound = false
pyramid.default_locale_name = en
sqlalchemy.url = sqlite:///%(here)s/ithome_pellok_2018.sqlite
[filter:paste_prefix]
use = egg:PasteDeploy#prefix
[pipeline:main]
pipeline =
paste_prefix
ithome_pellok_2018
[server:main]
use = egg:waitress#main
host = 127.0.0.1
port = %(http_port)s
###
# logging configuration
# https://docs.pylonsproject.org/projects/pyramid/en/1.9-branch/narr/logging.html
###
[loggers]
keys = root, ithome_pellok_2018, sqlalchemy
[handlers]
keys = console
[formatters]
keys = generic
[logger_root]
level = WARN
handlers = console
[logger_ithome_pellok_2018]
level = WARN
handlers =
qualname = ithome_pellok_2018
[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine
# "level = INFO" logs SQL queries.
# "level = DEBUG" logs SQL queries and results.
# "level = WARN" logs neither. (Recommended for production systems.)
[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic
[formatter_generic]
format = %(asctime)s %(levelname)-5.5s [%(name)s:%(lineno)s][%(threadName)s] %(message)s
安裝Git
cd /usr/src
wget https://www.kernel.org/pub/software/scm/git/git-2.8.1.tar.gz
tar xzf git-2.8.1.tar.gz
cd git-2.8.1
make prefix=/usr/local/git all
make prefix=/usr/local/git install
vim /etc/bashrc
export PATH=$PATH:/usr/local/git/bin
source /etc/bashrc
git --version
mv /usr/bin/git /usr/bin/git.1.8
ln -s /usr/local/git/bin/git /usr/bin/git
安裝 Nginx
yum install epel-release
yum install nginx
安裝 PostgreSQL
# rpm -Uvh http://yum.postgresql.org/9.5/redhat/rhel-7-x86_64/pgdg-centos95-9.5-2.noarch.rpm
yum -y install postgresql95-server postgresql95
/usr/pgsql-9.5/bin/postgresql95-setup initdb
systemctl start postgresql-9.5
systemctl enable postgresql-9.5
#setting
su - postgres
CREATE ROLE ithome_pellok_2018 PASSWORD 'ithome_123!QAZ' CREATEDB INHERIT LOGIN;
CREATE Database ithome_pellok_2018;
創建使用者
useradd ithome_pellok_2018
password ithome_pellok_2018
su - ithome_pellok_2018
安裝pyenv
#yum install -y gcc gcc-c++ make git patch openssl-devel zlib-devel readline-devel sqlite-devel bzip2-devel
git clone git://github.com/yyuu/pyenv.git ~/.pyenv
echo 'export PATH="$HOME/.pyenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
source ~/.bash_profile
pyenv install 3.5.0
pyenv global 3.5.0
pip install --upgrade pip setuptools virtualenv
pip install cryptography
pip install SQLAlchemy
pip install psycopg2-binary
下載專案
git clone https://bitbucket.org/pellok2002/ithome_pellok_2018.git
cd ithome_pellok_2018
安裝專案所需要的插件
virtualenv env
source env/bin/activate
pip install -r requestment.txt
python setup.py develop
初始化 DB
initialize_ithome_pellok_2018_db production.ini#ithome_pellok_2018
安裝 python 2.7.12
pyenv install 2.7.12
pyenv global 2.7.12
pip install --upgrade pip
pip install virtualenv
virtualenv supenv
source supenv/bin/activate
pip install supervisor
deactivate
mkdir log
supenv/bin/supervisor -c ithome_pellok_2018/supervisor.conf
~/.bash_profile
project_name=ithome_pellok_2018
project_env=/home/ithome_pellok_2018/ithome_pellok_2018/env
project_folder=/home/ithome_pellok_2018/ithome_pellok_2018
supenv_conf=/home/ithome_pellok_2018/ithome_pellok_2018/supervisor.conf
alias start="$project_folder/supenv/bin/supervisorctl -c $supenv_conf start all"
alias stop="$project_folder/supenv/bin/supervisorctl -c $supenv_conf stop all"
alias env="source $project_env/bin/activate"
alias supervisord="$project_folder/supenv/bin/supervisord -c $supenv_conf"
alias supervisorctl="$project_folder/supenv/bin/supervisorctl -c $supenv_conf"
以上幾個步驟是部署專案的過程,比較要注意的是,supervisor 是使用 python 2.7.12 版本,但是專案可能使用 python 3.5.0或其他版本,因為不同的環境所以各有自己的環境.另外以上動作可以分為兩個部分;第一部分是伺服器安裝git、nginx、postgresql,這邊是基本的主機設定,設定完成後可以直接儲存成樣板,下次再使用就可以直接用,第二部分是專案部署的可以寫成部署腳本,做到自動化部署.
pyramid deploy
nginx + pserve + supervisord