Skip to content

Instantly share code, notes, and snippets.

@sdn0303
Last active December 23, 2017 14:50
Show Gist options
  • Save sdn0303/098dad278c7b8fc7c12d526f48461dd1 to your computer and use it in GitHub Desktop.
Save sdn0303/098dad278c7b8fc7c12d526f48461dd1 to your computer and use it in GitHub Desktop.
Vagrant+CentOS7+Linuxbrew+Nginx+Python3環境構築メモ ref: https://qiita.com/sdn0303/items/0c32fcb760a3d5f9bad8
$ mkdir -p Vagrant/Centos7
$ cd ~/Vagrant/Centos7
# 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)
[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;
}
# 起動
[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
[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
# /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
# gunicorn経由でapiをバックグラウンド起動させる
[vagrant@localhost vagrant]$ gunicorn -b 0.0.0.0:8000 sample_api:app &
# プロセス確認
[vagrant@localhost vagrant]$ ps aux | grep gunicorn
# 使用したいボックス名を指定してinit
$ vagrant init centos7.1
# するとVagrantfileが生成される
$ ls
Vagrantfile
$ vim Vagrantfile
・・・
- 35 # config.vm.network "private_network", ip: "192.168.33.10"
+ 35 config.vm.network "private_network", ip: "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@localhost ~]$ sudo yum groupinstall 'Development Tools' && sudo yum install ruby curl file git python-setuptools openssl-devel
[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
[vagrant@localhost ~]$ sudo systemctl stop firewalld
[vagrant@localhost ~]$ sudo systemctl disable firewalld
[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
[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;
}
}
# -*- 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...')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment