Skip to content

Instantly share code, notes, and snippets.

@megane9988
Last active June 19, 2018 05:44
Show Gist options
  • Save megane9988/330e3ebcebb48bdd9ea5 to your computer and use it in GitHub Desktop.
Save megane9988/330e3ebcebb48bdd9ea5 to your computer and use it in GitHub Desktop.
amazonlinux + nginx + php + mysql + WordPress + Git

amazonlinux + nginx + php + mysql + WordPress + Git

ec2 インスタンス取得

  • t2.micro
  • Security Group は ssh と http を any で通るようにする
    • 可能ならssh は固定IPだけで通るようにする
  • 鍵は、すでにあればそれでも、新たに制作してもOK

立ち上がったら

  • EIPを取得
  • VPCで取る
  • 割り当てる
  • 鍵を.ssh に入れておく
  • 鍵のパーミッションは 600にしておく
chmod 600 ~/.ssh/鍵名
  • .ssh/config に登録
Host nginxpress
    Hostname        割り当てたIP
    Port         22
    User           ec2-user
    IdentityFile   ~/.ssh/利用スル鍵

を利用してssh で接続する

ssh nginxpress

とりあえずアップデートしておく

sudo yum update

nginxのインストール

利用している OS にあったり上記の RPM をインストールして yum リポジトリを登録します、ここでは CentOS 6 のリポジトリを登録する例を示します。

# rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
  • nginxをパッケージからインストールする
sudo yum install -y nginx
  • インストール後
  • 自動起動設定
sudo chkconfig nginx on
  • nginx起動
sudo service nginx start
  • 割り当てたIPにアクセスしてみてWebサーバとしてnginxが動き、ディフォルトの画面が表示されたことを確認
  • つづいて Web用のルートディレクトリを変更する。デフォでは
/usr/share/nginx/html/
  • 設定するファイルは
/etc/nginx/conf.d/の中身の*.confファイル
  • 以下の内容をvirtual-ドメイン名.confなどで保存
server {

# ポート開放とドメインの設定
listen       80;
server_name  week.wp-d.org;

location / {
# 該当ドメインのルートディレクトリ設定
    root   /var/www/vhosts/wpdweek;
# パーマネントリンクが設定できるように
    index index.html index.htm index.php ;
        if (-f $request_filename) {
        expires 30d;
        break;
        }
        if (!-e $request_filename) {
        rewrite ^.+?(/wp-.*) $1 last;
        rewrite ^.+?(/.*\.php)$ $1 last;
        rewrite ^ /index.php last;
        }
        location ~* ^.+.(jpg|jpeg|gif|png|css|js|flv|swf|ico|xml)$ {
        access_log  off;
        expires 30d;
        root /var/www/vhosts/wpdweek;
        }
    }
# phpが動くように
    location ~ \.php$ {
        root           /var/www/vhosts/wpdweek;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

とする。

  • 保存し、nginxを再起動
sudo /etc/init.d/nginx restart
  • ルートの場所にフォルダを作る
sudo mkdir /var/www
sudo mkdir /var/www/vhosts
  • 権限を変更する
sudo chown ec2-user /var/www/vhosts/
  • ファイルをなにか作って表示させる
vi /var/www/html/index.html
  • 割り当てたIPにアクセスしてみて追加したファイルが表示されていればOK

phpが使えるようにする

sudo yum install php php-fpm php-mbstring
  • php-fpm の設定
sudo vi /etc/php-fpm.d/www.conf
  • userとgroupをapacheからnginxに変更します

  • nginx.conf の設定

sudo vi /etc/nginx/nginx.conf
  • location ~ .php$あたりのコメントアウトをまるまるはずす

  • rootを

root           html;
↓
root           /var/www/html;
  • fastcgi_paramのSCRIPT_FILENAMEを
/scripts$fastcgi_script_name;
↓
$document_root$fastcgi_script_name;

に変更します。

  • php-fpm を立ち上げる
sudo /etc/rc.d/init.d/php-fpm start
  • nginxを再起動
sudo /etc/init.d/nginx restart
  • ルートフォルダ上にテストのphpを書いてアクセスしてみる
sudo vi /var/www/html/megane.php
<?php
phpinfo();
?>
  • 動けばOK

mysqlをインストールする

  • mysqlをインストール
sudo yum install mysql
  • mysqlサーバをインストール
sudo yum install mysql-server
  • mysqlを起動
sudo /etc/init.d/mysqld start
  • 自動起動の設定をONにする
sudo /sbin/chkconfig mysqld on
  • phpでmysqlを使えるようにする拡張モジュールをインストールする
sudo yum install php-mysql
  • nginxを再起動
sudo /etc/init.d/nginx restart

mysqlを設定する

  • rootユーザーのパスワードを設定
sudo mysqladmin -u root password パスワード
  • パスワード設定の確認のためrootユーザーでmysqlへのログインを試みる
mysql -u root -p
  • ログイン出来ればOK
  • ログアウトするときは
quit
  • つづいてWordPress用のデータベースを作成
sudo mysqladmin -u root -p create データベース名

wp cliのインストール

curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
php wp-cli.phar --info
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp
wp --info

WordPressのインストール

  • ルートディレクトリに移動
cd /var/www/html
  • コアファイルのダウンロード
wp core download --locale=ja
  • コンフィグファイルの作成
cp wp-config-sample.php wp-config.php
  • コンフィグファイルの設定
vi wp-config.php
  • ユーザ名
  • データベース名
  • パスワード
  • それぞれ記載する

phpとmysqlが拡張できるように調整

  • php.iniにおいて、php-mysql をつかえるようにする
sudo vi /etc/php.ini
# extension=mysql.so
  • のコメントアウトをとる

php fpmを再起動

sudo /etc/rc.d/init.d/php-fpm stop
sudo /etc/rc.d/init.d/php-fpm start
  • 画面からアクセスして、Wordpressをインストール

WordPressのパーマリンク設定をカスタム構造にするための設定

  • そのままだと、パーマネントリンクの変更が行えないので
  • nginx.confを編集
sudo vi /etc/nginx/nginx.conf
   server {
        listen       80;
        server_name  localhost;
        root         /var/www/html;

        #charset koi8-r;

        #access_log  /var/log/nginx/host.access.log  main;
        location / {
        }

このlocationのところに

location / {
    if (-f $request_filename) {
    expires 30d;
    break;
    }
if (!-e $request_filename) {
    rewrite ^.+?(/wp-.*) $1 last;
    rewrite ^.+?(/.*\.php)$ $1 last;
    rewrite ^ /index.php last;
    }
location ~* ^.+.(jpg|jpeg|gif|png|css|js|flv|swf|ico|xml)$ {
    access_log  off;
    expires 30d;
    root /var/www/html;
    }
}

を追記して保存

  • nginxをリスタートして適用
sudo /etc/init.d/nginx restart

phpと mysqlの設定を元に戻す

これをしないと wp cli使った時に 

PHP Warning:  Module 'mysql' already loaded in Unknown on line 0

というエラーが出る

sudo vi /etc/php.ini
extension=mysql.so
  • 再度コメントアウト

php fpmを再起動

sudo /etc/rc.d/init.d/php-fpm stop
sudo /etc/rc.d/init.d/php-fpm start

画像をアップロードできるように

sudo mkdir /var/www/html/wp-content/uploads

所有者をnginxに変更して権限を設定

sudo chown nginx /var/www/html/wp-content/uploads
sudo chmod 700 /var/www/html/wp-content/uploads

画像をリサイズするためのモジュールを追加

sudo yum -y install php-gd
sudo yum install ImageMagick ImageMagick-devel
  • nginxのリスタート
sudo /etc/init.d/nginx restart

ドメインを割り当てる

  • 利用ドメイン
    • hoge.com
  • ルートディレクトリ
    • /var/www/vhosts/hoge/

とした場合

sudo vi /etc/nginx/conf.d/virtual.conf

下記をまるまるコピペ

server {

# ポート開放とドメインの設定
listen       80;
server_name  hoge.com;

location / {
# 該当ドメインのルートディレクトリ設定
    root   /var/www/vhosts/hoge;
# パーマネントリンクが設定できるように
    index index.html index.htm index.php ;
        if (-f $request_filename) {
        expires 30d;
        break;
        }
        if (!-e $request_filename) {
        rewrite ^.+?(/wp-.*) $1 last;
        rewrite ^.+?(/.*\.php)$ $1 last;
        rewrite ^ /index.php last;
        }
        location ~* ^.+.(jpg|jpeg|gif|png|css|js|flv|swf|ico|xml)$ {
        access_log  off;
        expires 30d;
        root /var/www/vhosts/hoge;
        }
    }
# phpが動くように
    location ~ \.php$ {
        root           /var/www/vhosts/hoge;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

nginxを再起動

sudo /etc/init.d/nginx restart

Gitをインストール

sudo yum install git

consoleを色分けして見やすくする

vi ~/.bash_profile

下記を、現在記載のある内容の下にすべてコピペ

#!/usr/bin/env bash

# Check that terminfo exists before changing TERM var to xterm-256color
# Prevents prompt flashing in Mac OS X 10.6 Terminal.app
if [ -e /usr/share/terminfo/x/xterm-256color ]; then
    export TERM='xterm-256color'
fi

# Turn off standout; turn off underline
tput sgr 0 0

# Base styles and color palette
# If you want to check color code, run `./testcolor.sh'
BOLD=$(tput bold)
RESET=$(tput sgr0)
BLACK=$(tput setaf 0)
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
BLUE=$(tput setaf 4)
MAGENTA=$(tput setaf 5)
CYAN=$(tput setaf 6)
WHITE=$(tput setaf 255)
ORANGE=$(tput setaf 172)

style_user="\[${RESET}${ORANGE}\]"
style_host="\[${RESET}${YELLOW}\]"
style_path="\[${RESET}${GREEN}\]"
style_chars="\[${RESET}${WHITE}\]"
style_branch="${CYAN}"

if [[ "$SSH_TTY" ]]; then
    # connected via ssh
    style_host="\[${BOLD}${RED}\]"
elif [[ "$USER" == "root" ]]; then
    # logged in as root
    style_user="\[${BOLD}${RED}\]"
fi

is_git_repo() {
    $(git rev-parse --is-inside-work-tree &> /dev/null)
}

is_git_dir() {
    $(git rev-parse --is-inside-git-dir 2> /dev/null)
}

get_git_branch() {
    local branch_name

    # Get the short symbolic ref
    branch_name=$(git symbolic-ref --quiet --short HEAD 2> /dev/null) ||
    # If HEAD isn't a symbolic ref, get the short SHA
    branch_name=$(git rev-parse --short HEAD 2> /dev/null) ||
    # Otherwise, just give up
    branch_name="(unknown)"

    printf $branch_name
}

# Git status information
prompt_git() {
    local git_info git_state uc us ut st

    if ! is_git_repo || is_git_dir; then
        return 1
    fi

    git_info=$(get_git_branch)

    # Check for uncommitted changes in the index
    if ! $(git diff --quiet --ignore-submodules --cached); then
        uc="+"
    fi

    # Check for unstaged changes
    if ! $(git diff-files --quiet --ignore-submodules --); then
        us="!"
    fi

    # Check for untracked files
    if [ -n "$(git ls-files --others --exclude-standard)" ]; then
        ut="?"
    fi

    # Check for stashed files
    if $(git rev-parse --verify refs/stash &>/dev/null); then
        st="$"
    fi

    git_state=$uc$us$ut$st

    # Combine the branch name and state information
    if [[ $git_state ]]; then
        git_info="$git_info[$git_state]"
    fi

    printf "${WHITE} on ${style_branch}${git_info}"
}


# Set the terminal title to the current working directory
PS1="\[\033]0;\w\007\]"
# Build the prompt
PS1+="\n" # Newline
PS1+="${style_user}\u" # Username
PS1+="${style_chars}@" # @
PS1+="${style_host}\h" # Host
PS1+="${style_chars}: " # :
PS1+="${style_path}\w" # Working directory
PS1+="\$(prompt_git)" # Git details
PS1+="\n" # Newline
PS1+="${style_chars}\$ \[${RESET}\]" # $ (and reset color)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment