amazonlinux + nginx + php + mysql + WordPress + Git
立ち上がったら
chmod 600 ~/.ssh/鍵名
Host nginxpress
Hostname 割り当てたIP
Port 22
User ec2-user
IdentityFile ~/.ssh/利用スル鍵
を利用してssh で接続する
ssh nginxpress
とりあえずアップデートしておく
sudo yum update
利用している OS にあったり上記の RPM をインストールして yum リポジトリを登録します、ここでは CentOS 6 のリポジトリを登録する例を示します。
# rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
sudo yum install -y nginx
sudo chkconfig nginx on
sudo service nginx start
/usr/share/nginx/html/
/etc/nginx/conf.d/の中身の*.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;
}
}
とする。
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
sudo yum install php php-fpm php-mbstring
sudo vi /etc/php-fpm.d/www.conf
sudo vi /etc/nginx/nginx.conf
location ~ .php$あたりのコメントアウトをまるまるはずす
rootを
root html;
↓
root /var/www/html;
/scripts$fastcgi_script_name;
↓
$document_root$fastcgi_script_name;
に変更します。
sudo /etc/rc.d/init.d/php-fpm start
sudo /etc/init.d/nginx restart
sudo vi /var/www/html/megane.php
<?php
phpinfo();
?>
sudo yum install mysql
sudo yum install mysql-server
sudo /etc/init.d/mysqld start
sudo /sbin/chkconfig mysqld on
sudo yum install php-mysql
sudo /etc/init.d/nginx restart
sudo mysqladmin -u root password パスワード
mysql -u root -p
quit
sudo mysqladmin -u root -p create データベース名
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
cd /var/www/html
wp core download --locale=ja
cp wp-config-sample.php wp-config.php
vi wp-config.php
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
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;
}
}
を追記して保存
sudo /etc/init.d/nginx restart
これをしないと 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
sudo /etc/init.d/nginx restart
とした場合
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
sudo yum install git
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)