環境
MacOS HighSierra 10.13.1
VagrantとVirtualBoxをインストール
VirtualBox
https://www.virtualbox.org/wiki/Downloads
Vagrant
https://www.vagrantup.com/downloads.html
↑それぞれのサイトから自分の環境に合うものをダウンロードしてくる。
ワークスペース作成
$ mkdir -p Vagrant/Centos7
$ cd ~/Vagrant/Centos7
Box追加
http://www.vagrantbox.es/
↑から使いたいOSイメージのURLをコピーする。
# vagrant box add {vagrantのbox名} {boxのURL}を指定してDL
$ vagrant box add centos7.1 https://github.com/CommanderK5/packer-centos-template/releases/download/0.7.1/vagrant-centos-7.1.box
# 確認
$ vagrant box list
centos7.1 (virtualbox, 0)
仮想環境作成
# 使用したいボックス名を指定してinit
$ vagrant init centos7.1
# するとVagrantfileが生成される
$ ls
Vagrantfile
Vagrantfile編集
35行目あたりにあるIP関連のコメントアウトを外す
$ vim Vagrantfile
・・・
- 35 # config.vm.network "private_network", ip: "192.168.33.10"
+ 35 config.vm.network "private_network", ip: "192.168.33.10"
・・・
これでブラウザから http://192.168.33.10 にアクセスできる様になる。
仮想環境セットアップ
# vagrant立ち上げる
$ vagrant up
# 立ち上がったらsshで入る
$ vagrant ssh
Welcome to your Vagrant-built virtual machine.
[vagrant@localhost ~]$
# yum update
[vagrant@localhost ~]$ sudo yum update
vagrantのコマンド一覧は↓に詳しく載ってます。
https://qiita.com/oreo3@github/items/4054a4120ccc249676d9
Linuxbrewインストール
rubyとその他諸々をインストール
[vagrant@localhost ~]$ sudo yum groupinstall 'Development Tools' && sudo yum install ruby curl file git python-setuptools openssl-devel
linuxbrewインストール
[vagrant@localhost ~]$ sh -c "$(curl -fsSL https://raw.githubusercontent.com/Linuxbrew/install/master/install.sh)"
PATHを通す(linubrewインストール完了時のターミナルに出るPathセットアップのガイドに沿って追加)
[vagrant@localhost ~]$ echo "export PATH="$HOME/.linuxbrew/bin:$PATH"" >> ~/.bash_profile
[vagrant@localhost ~]$ echo "export MANPATH="$HOME/.linuxbrew/share/man:$MANPATH"" >> ~/.bash_profile
[vagrant@localhost ~]$ echo "export INFOPATH="$HOME/.linuxbrew/share/info:$INFOPATH"" >> ~/.bash_profile
# .bash_profile再読み込み
[vagrant@localhost ~]$ source ~/.bash_profile
brewでgitをインストール
[vagrant@localhost ~]$ brew install gcc
[vagrant@localhost ~]$ brew install git
gitのpathを通す({version}にbrewでインストールしたgitのバージョンを置き換えてください)
[vagrant@localhost ~]$ echo "export PATH="/usr/local/Cellar/git/{version}/bin:$PATH"" >> ~/.bash_profile
# バージョン確認
[vagrant@localhost ~]$ git version
2.15.1
↑インストールしたバージョンになればok
nginxインストール
の前にfirewallを無効にする
[vagrant@localhost ~]$ sudo systemctl stop firewalld
[vagrant@localhost ~]$ sudo systemctl disable firewalld
nginx yum レポジトリファイル作成
[vagrant@localhost ~]$ sudo vi /etc/yum.repos.d/nginx.repo
# ↓をコピペして保存
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
# nginxインストール
[vagrant@localhost ~]$ sudo yum install -y nginx
# 確認
[vagrant@localhost ~]$ nginx -v
nginx version: nginx/1.13.7
nginx設定ファイル編集
default.confを修正
[vagrant@localhost ~]$ cd /etc/nginx/conf.d
[vagrant@localhost conf.d]$ sudo vi default.conf
# default.confの中身を↓に修正
server {
listen 80;
server_name 192.168.33.10;
charset utf-8;
location / {
proxy_pass http://0.0.0.0:8000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
nginx.confを修正
[vagrant@localhost conf.d]$ cd ../
[vagrant@localhost nginx]$ sudo vi nginx.conf
# nginx.confを↓に修正(*実際には動かすサービスに合った設定にする)
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
server_tokens off;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
access_log off;
error_log /var/log/nginx/error.log crit;
keepalive_timeout 10;
client_header_timeout 10;
client_body_timeout 10;
reset_timedout_connection on;
send_timeout 10;
limit_conn_zone $binary_remote_addr zone=addr:5m;
limit_conn addr 100;
charset UTF-8;
gzip on;
gzip_http_version 1.0;
gzip_disable "msie6";
gzip_proxied any;
gzip_min_length 1024;
gzip_comp_level 6;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript application/json;
open_file_cache max=100000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
include /etc/nginx/conf.d/*.conf;
}
nginx起動
# 起動
[vagrant@localhost nginx]$ sudo systemctl start nginx
# 自動起動
[vagrant@localhost nginx]$ sudo systemctl enable nginx
# ステータス確認
[vagrant@localhost nginx]$ sudo systemctl status nginx
nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled)
Active: active (running) since 土 2017-12-09 14:47:15 UTC; 7s ago
Docs: http://nginx.org/en/docs/
Process: 1767 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 1769 (nginx)
CGroup: /system.slice/nginx.service
├─1769 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
└─1770 nginx: worker process
12月 09 14:47:15 localhost.localdomain systemd[1]: Starting nginx - high performance web server...
12月 09 14:47:15 localhost.localdomain nginx[1767]: nginx: [warn] duplicate MIME type "application/json" in /etc/nginx/nginx.conf:41
12月 09 14:47:15 localhost.localdomain systemd[1]: PID file /var/run/nginx.pid not readable (yet?) after start.
12月 09 14:47:15 localhost.localdomain systemd[1]: Started nginx - high performance web server.
# ホームディレクトリに戻る
[vagrant@localhost nginx]$ cd
# 再起動をかける場合は
$ sudo systemctl restart nginx
# または
$ sudo systemctl reload nginx
Python3(Anaconda)インストール
pyenv経由でインストールしたいのでまずはpyenvをインストール
[vagrant@localhost ~]$ brew install pyenv
[vagrant@localhost ~]$ pyenv -v
pyenv 1.1.5
# pathを通す
[vagrant@localhost ~]$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
[vagrant@localhost ~]$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
[vagrant@localhost ~]$ echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
# .bash_profile再読み込み
[vagrant@localhost ~]$ source ~/.bash_profile
# インストール可能なバージョン一覧を確認
[vagrant@localhost ~]$ pyenv install --list
# 使いたいバージョンをインストール
[vagrant@localhost ~]$ pyenv install anaconda3-5.0.0
# グローバル環境を3系に変更してバージョンを確認
[vagrant@localhost ~]$ pyenv global anaconda3-5.0.0
[vagrant@localhost ~]$ pyenv versions
system
* anaconda3-5.0.0 (set by /home/vagrant/.pyenv/version)
# 特定のディレクトリのみ3系にする場合
$ cd hoge
$ pyenv local anaconda3-5.0.0
# ターミナル再起動
[vagrant@localhost ~]$ exec $SHELL -l
# Pythonバージョン確認
[vagrant@localhost ~]$ python -V
# もしバージョンが変わらない場合は一旦exitしてvagrantを再起動する
[vagrant@localhost ~]$ exit
$ vagrant reload
$ vagrant ssh
Pyramidで簡単なAPI作って動かしてみる
# /vagrant は共有ディレクトリで最初にローカルで作ったCentos7ディレクトリと同期している
[vagrant@localhost ~]$ cd /vagrant
# gunicornインストール
[vagrant@localhost vagrant]$ pip install gunicorn
# pyramidインストール
[vagrant@localhost vagrant]$ pip install pyramid
# サンプルapi作成
[vagrant@localhost vagrant]$ sudo vi sample_api.py
# -*- Coding: UTF-8 -*-
from pyramid.config import Configurator
from pyramid.response import Response
def hello_world(request):
return Response('Hello %(name)s!' % request.matchdict)
with Configurator() as config:
config.add_route('hello', '/hello/{name}')
config.add_view(hello_world, route_name='hello')
app = config.make_wsgi_app()
if __name__ == '__main__':
from wsgiref.simple_server import make_server
httpd = make_server('', 8000, app)
httpd.server_forever()
print('Serving on port 8000...')
# gunicorn経由でapiをバックグラウンド起動させる
[vagrant@localhost vagrant]$ gunicorn -b 0.0.0.0:8000 sample_api:app &
# プロセス確認
[vagrant@localhost vagrant]$ ps aux | grep gunicorn
ブラウザから http://192.168.33.10/hello/hoge でアクセスして確認できたらok。
参考記事
https://gist.github.com/koudaiii/386eb55a29b1adc19c5e
https://hombre-nuevo.com/python/python0020/
https://qiita.com/thermes/items/926b478ff6e3758ecfea
http://pyramid-tutorial-pylonprojectjp.readthedocs.io/en/latest/#hello-world
追記 2017/12/23
ちなみにbrewとAnacondaは相性が悪いようで↑のセットアップ完了後にbrew doctorを叩くと以下のようなワーニングが出ます。
[vagrant@localhost vagrant]$ brew doctor
Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry and just ignore them. Thanks!
Warning: python is symlinked to python3
This will confuse build scripts and in general lead to subtle breakage.
Warning: "config" scripts exist outside your system or Homebrew directories.
`./configure` scripts often look for *-config scripts to determine if
software packages are installed, and what additional flags to use when
compiling and linking.
Having additional scripts in your path can confuse software installed via
Homebrew if the config script overrides a system or Homebrew provided
script of the same name. We found the following "config" scripts:
/home/vagrant/.pyenv/shims/icu-config
/home/vagrant/.pyenv/shims/libpng16-config
/home/vagrant/.pyenv/shims/ncursesw6-config
/home/vagrant/.pyenv/shims/pcre-config
/home/vagrant/.pyenv/shims/python-config
/home/vagrant/.pyenv/shims/python3-config
/home/vagrant/.pyenv/shims/python3.6-config
/home/vagrant/.pyenv/shims/python3.6m-config
解決方法は.bash_profileに以下の一行を追加する
[vagrant@localhost vagrant]$ vim ~/.bash_profile
alias brew="env PATH=${PATH/\/home\/vagrant\/\.pyenv\/shims:/} brew"
あとは.bash_profileを再読み込みすればワーニングが消える
[vagrant@localhost vagrant]$ source ~/.bash_profile
[vagrant@localhost vagrant]$ brew doctor
Your system is ready to brew.
もし変わらない場合は一旦exitしてvagrantを再起動してみてください。