新さくらのVPSでWebサーバを構築するテンプレ


先日リニューアルされた「さくらのVPS」でWebサーバ(LAMP)を構築した際の基本設定をテンプレートとして公開します。参考になればいいなと。

管理者アカウントの作成

# useradd -G wheel userName
# passwd userName

# vi /etc/pam.d/su
--
以下の行のコメントアウトを外す
# auth required pam_wheel.so use_uid
--

# visudo
--
以下の行のコメントアウトを外す
# %wheel ALL=(ALL) ALL
--

SSH設定

# mkdir /home/userName/.ssh
# chown userName. /home/userName/.ssh

クライアント側で公開鍵を作成し、サーバに公開鍵を転送
--
$ ssh-keygen -t rsa
$ scp .ssh/id_rsa.pub userName@host:/home/userName/.ssh
--

# cat /home/userName/.ssh/id_rsa.pub >> /home/userName/.ssh/authorized_keys
# chown userName. /home/userName/.ssh/authorized_keys
# chmod 700 /home/userName/.ssh
# chmod 600 /home/userName/.ssh/*

# vi /etc/ssh/sshd_config
--
Port 10022 (標準の22のままは望ましくないので、他の番号に変更。ここではとりあえず10022)
PermitRootLogin no
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
PasswordAuthentication no
--

# /etc/init.d/sshd restart

接続元ホストの許可・拒否

# vi /etc/hosts.allow
--
ALL: 127.0.0.1
sshd: .jp (hogehoge.ne.jpみたいに使ってるプロバイダで指定する方がより安全)
--

# vi /etc/hosts.deny
--
sshd: ALL
--

iptablesの設定

# vi /etc/sysconfig/iptables
--
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:RH-Firewall-1-INPUT - [0:0]
-A INPUT -s 58.22.0.0/15 -j DROP
-A INPUT -s 61.135.0.0/16 -j DROP
-A INPUT -s 123.125.71.0/24 -j DROP
-A INPUT -s 119.63.192.0/255.255.248.0 -j DROP
-A INPUT -s 180.76.0.0/255.255.0.0 -j DROP
-A INPUT -s 220.181.0.0/255.255.0.0 -j DROP
-A INPUT -j RH-Firewall-1-INPUT
-A FORWARD -j RH-Firewall-1-INPUT
-A RH-Firewall-1-INPUT -i lo -j ACCEPT
-A RH-Firewall-1-INPUT -p icmp --icmp-type any -j ACCEPT
-A RH-Firewall-1-INPUT -p 50 -j ACCEPT
-A RH-Firewall-1-INPUT -p 51 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp --dport 5353 -d 224.0.0.251 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp -m udp --dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m tcp --dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 10022 -j ACCEPT
-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited
COMMIT
--



# /etc/rc.d/init.d/iptables restart

Apacheの導入

# yum install httpd
# ln -s /usr/bin/perl /usr/local/bin/perl
# chown -R userName:apache /var/www
# chmod 2775 /var/www/cgi-bin
# rm -f /etc/httpd/conf.d/welcome.conf
# rm -f /var/www/error/noindex.html
# /sbin/chkconfig httpd on

# vi /etc/httpd/conf/httpd.conf
--
ServerTokens Prod (「OS」のままだと危ないので、必ず変更)
ServerName serverName

Options FollowSymLinks
AllowOverride All


DirectoryIndex index.php index.html

ServerSignature Off(「On」のままだと危ないので、必ず変更)
--
その他、 「最低限やっておくといいApacheチューニング 」「簡単にできるWebサーバの負荷軽減」「Apacheチューニング MaxClientsの適正値」を参考に設定してください。

logrotateの設定

# vi /etc/logrotate.conf
--
以下の行をコメントアウトして、ログを圧縮する
# compress
--

# vi /etc/logrotate.d/httpd
--
以下の行を追加
daily
rotate 30
compress

以下の行をコメントアウト
notifempty
--

リポジトリの変更

MySQLやPHPなど、新しいバージョンが使えるようリポジトリを変更

# rpm --import http://apt.sw.be/RPM-GPG-KEY.dag.txt
# rpm -ivh http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm
# rpm -ivh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-5.noarch.rpm
# rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

MySQLの導入

# yum --enablerepo=remi install mysql-server

# vi /etc/my.cnf
--
以下の行を追加

[mysqld]
character-set-server=utf8
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqldump]
default-character-set=utf8
--

  • よく、"skip-character-set-client-handshake"の設定例があるけど、セキュリティ上望ましくない。


# /sbin/chkconfig mysqld on
# /etc/init.d/mysqld start

MySQLの初期設定
# mysql_secure_installation

PHPの導入

# yum --enablerepo=remi install php
# yum --enablerepo=remi install php-mbstring php-mysql php-pear php-gd php-pecl-apc

# touch /var/log/php_error.log
# chmod 757 /var/log/php_error.log

# vi /etc/php.ini
--
expose_php = off(「On」のままだと危ないので、必ず変更)

error_log = /var/log/php_error.log

mbstring.language = Japanese # コメントアウトを解除
mbstring.internal_encoding = UTF-8
mbstring.http_input = pass
mbstring.http_output = pass
mbstring.encoding_translation = Off # コメントアウトを解除

[Date]
date.timezone = Asia/Tokyo
--

# pear channel-update pear.php.net
# pear upgrade --force PEAR
# pear upgrade-all

PEAR:Cache_Liteの導入

# pear install Cache_Lite
# mkdir /tmp/cache
# chown -R apache:apache /tmp/cache
# chmod 777 /tmp/cache

APCの設定

# mkdir /var/www/apc
# cp /usr/share/doc/php-pecl-apc-VERSION/apc.php /var/www/apc/apc.php

# vi /var/www/apc/apc.conf.php
--
defaults('ADMIN_USERNAME','user');
defaults('ADMIN_PASSWORD','password');
--

# vi /etc/httpd/conf.d/apc.conf
--
Alias /apc "/var/www/apc"
<Location "/apc">
Order deny,allow
Deny from all
Allow from 127.0.0.1
Allow from .jp(hogehoge.ne.jpみたいに使ってるプロバイダで指定する方がより安全)
</Location>
--

  • (2012.04.08 訂正)ブログに掲載する時に、apc.confの「<Location "/apc”>と</Location>」が消えてしまっていたので訂正しました。

# /etc/init.d/httpd restart

管理者メールアドレスの登録

# sed -i '/^root:/d' /etc/aliases
# echo "root: MAIL-ADDRESS" >> /etc/aliases
# newaliases


  • (2012.04.08 訂正)はてなブックマークのコメントで、変更を反映させる newaliases が抜けているとのご指摘を頂きました。必要なコマンドですので追記しました。

このブログの人気の投稿

oh-my-zshの導入とか、zshとvimの環境をナウでヤングでシンプルな感じにした

Pagesの差し込み印刷が便利すぎて、Officeが完全に要らなくなった