さくらのクラウドの CentOS 5.7 で LAMP 環境構築及び設定・チューニングまとめ

今回は最小構成時の設定例を紹介します。以下の記事の説明簡略版です。設定でわからないところがあれば以下の記事を参考にしてみてください。さくらのクラウドの設定は少しだけ違うので注意してください。

さくらのクラウドを自由に設定したら、あとはIPアドレスを確認してSSH でログインして直ぐ作業が始められます。

1
ssh hoge@123.456.78.90

CentOS をアップグレード。

1
yum upgrade

vim をインストールする。

1
yum -y install vim-enhanced

.bashrc を設定する。

1
vim ~/.bashrc

以下はサンプルです。

1
2
3
4
5
alias vi='vim'
alias ls="ls -G"
alias ll="ls -alF"
alias la="ls -A"
alias l="ls -CF"

再読込。

1
source .bashrc

ファイアーウォール設定。

1
vi /etc/sysconfig/iptables

以下は設定例です。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
*filter
:INPUT   ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT  ACCEPT [0:0]
:RH-Firewall-1-INPUT - [0:0]
 
-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
 
# SSH, HTTP, FTP1, FTP2, MySQL
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 10022 -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 20    -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 21    -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 3306  -j ACCEPT
 
-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited
 
COMMIT

ファイアーウォール有効化。

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

デーモンを止める。以下は設定例です。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/sbin/chkconfig auditd off
/sbin/chkconfig autofs off
/sbin/chkconfig avahi-daemon off
/sbin/chkconfig bluetooth off
/sbin/chkconfig cups off
/sbin/chkconfig firstboot off
/sbin/chkconfig gpm off
/sbin/chkconfig haldaemon off
/sbin/chkconfig hidd off
/sbin/chkconfig isdn off
/sbin/chkconfig kudzu off
/sbin/chkconfig lvm2-monitor off
/sbin/chkconfig mcstrans off
/sbin/chkconfig mdmonitor off
/sbin/chkconfig messagebus off
/sbin/chkconfig netfs off
/sbin/chkconfig nfslock off
/sbin/chkconfig pcscd off
/sbin/chkconfig portmap off
/sbin/chkconfig rawdevices off
/sbin/chkconfig restorecond off
/sbin/chkconfig rpcgssd off
/sbin/chkconfig rpcidmapd off
/sbin/chkconfig smartd off
/sbin/chkconfig xfs off
/sbin/chkconfig yum-updatesd off

リポジトリを追加する。

1
2
3
wget http://rpms.famillecollet.com/el5.x86_64/remi-release-5-8.el5.remi.noarch.rpm
wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el5.rf.x86_64.rpm
rpm -Uvh remi-release-5-8.el5.remi.noarch.rpm rpmforge-release-0.5.2-2.el5.rf.x86_64.rpm

rpmforge を無効化する。rpmforge の項目が enabled=1 になっているので0にします。

1
vi /etc/yum.repos.d/rpmforge.repo

LAMP 環境を構築する。インストールされる PHP のバージョンを確認してインストールするようにします。リポジトリがうまく設定されていなければ古い PHP などがインストールされます。

1
yum --enablerepo=remi,epel,rpmforge install httpd-devel php-devel php-mysql php-pear mysql-server

Web 公開ディレクトリを作成。

1
mkdir /var/www/vhosts

Apache の設定をします。

1
vi /etc/httpd/conf/httpd.conf

バーチャルホストを有効化。

1
NameVirtualHost *:80

バーチャルホストを設定する。

1
2
3
4
5
6
7
8
9
10
11
<VirtualHost *:80>
    DocumentRoot "/var/www/vhosts/localhost"
    ServerName localhost
    AllowEncodedSlashes On
    <Directory "/var/www/vhosts/localhost">
        order deny,allow
        allow from All
        Options FollowSymLinks
        AllowOverride All
    </Directory>
</VirtualHost>

使わないモジュールを停止します。以下は設定例です。

149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#LoadModule auth_basic_module modules/mod_auth_basic.so
#LoadModule auth_digest_module modules/mod_auth_digest.so
#LoadModule authn_file_module modules/mod_authn_file.so
#LoadModule authn_alias_module modules/mod_authn_alias.so
#LoadModule authn_anon_module modules/mod_authn_anon.so
#LoadModule authn_dbm_module modules/mod_authn_dbm.so
#LoadModule authn_default_module modules/mod_authn_default.so
LoadModule authz_host_module modules/mod_authz_host.so
#LoadModule authz_user_module modules/mod_authz_user.so
#LoadModule authz_owner_module modules/mod_authz_owner.so
#LoadModule authz_groupfile_module modules/mod_authz_groupfile.so
#LoadModule authz_dbm_module modules/mod_authz_dbm.so
#LoadModule authz_default_module modules/mod_authz_default.so
#LoadModule ldap_module modules/mod_ldap.so
#LoadModule authnz_ldap_module modules/mod_authnz_ldap.so
#LoadModule include_module modules/mod_include.so
LoadModule log_config_module modules/mod_log_config.so
#LoadModule logio_module modules/mod_logio.so
#LoadModule env_module modules/mod_env.so
#LoadModule ext_filter_module modules/mod_ext_filter.so
#LoadModule mime_magic_module modules/mod_mime_magic.so
LoadModule expires_module modules/mod_expires.so
#LoadModule deflate_module modules/mod_deflate.so
LoadModule headers_module modules/mod_headers.so
#LoadModule usertrack_module modules/mod_usertrack.so
LoadModule setenvif_module modules/mod_setenvif.so
LoadModule mime_module modules/mod_mime.so
#LoadModule dav_module modules/mod_dav.so
LoadModule status_module modules/mod_status.so
#LoadModule autoindex_module modules/mod_autoindex.so
#LoadModule info_module modules/mod_info.so
#LoadModule dav_fs_module modules/mod_dav_fs.so
#LoadModule vhost_alias_module modules/mod_vhost_alias.so
LoadModule negotiation_module modules/mod_negotiation.so
LoadModule dir_module modules/mod_dir.so
#LoadModule actions_module modules/mod_actions.so
#LoadModule speling_module modules/mod_speling.so
#LoadModule userdir_module modules/mod_userdir.so
LoadModule alias_module modules/mod_alias.so
LoadModule rewrite_module modules/mod_rewrite.so
#LoadModule proxy_module modules/mod_proxy.so
#LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
#LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
#LoadModule proxy_http_module modules/mod_proxy_http.so
#LoadModule proxy_connect_module modules/mod_proxy_connect.so
#LoadModule cache_module modules/mod_cache.so
#LoadModule suexec_module modules/mod_suexec.so
#LoadModule disk_cache_module modules/mod_disk_cache.so
#LoadModule file_cache_module modules/mod_file_cache.so
#LoadModule mem_cache_module modules/mod_mem_cache.so
#LoadModule cgi_module modules/mod_cgi.so
#LoadModule version_module modules/mod_version.so

mod_autoindex を停止したので関連項目を無効化。

592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
<IfModule mod_autoindex.c>
        IndexOptions FancyIndexing VersionSort NameWidth=* HTMLTable
        #
        # AddIcon* directives tell the server which icon to show for different
        # files or filename extensions.  These are only displayed for
        # FancyIndexed directories.
        #
        AddIconByEncoding (CMP,/icons/compressed.gif) x-compress x-gzip
 
        AddIconByType (TXT,/icons/text.gif) text/*
        AddIconByType (IMG,/icons/image2.gif) image/*
        AddIconByType (SND,/icons/sound2.gif) audio/*
        AddIconByType (VID,/icons/movie.gif) video/*
 
        AddIcon /icons/binary.gif .bin .exe
        AddIcon /icons/binhex.gif .hqx
        AddIcon /icons/tar.gif .tar
        AddIcon /icons/world2.gif .wrl .wrl.gz .vrml .vrm .iv
        AddIcon /icons/compressed.gif .Z .z .tgz .gz .zip
        AddIcon /icons/a.gif .ps .ai .eps
        AddIcon /icons/layout.gif .html .shtml .htm .pdf
        AddIcon /icons/text.gif .txt
        AddIcon /icons/c.gif .c
        AddIcon /icons/p.gif .pl .py
        AddIcon /icons/f.gif .for
        AddIcon /icons/dvi.gif .dvi
        AddIcon /icons/uuencoded.gif .uu
        AddIcon /icons/script.gif .conf .sh .shar .csh .ksh .tcl
        AddIcon /icons/tex.gif .tex
        AddIcon /icons/bomb.gif core
 
        AddIcon /icons/back.gif ..
        AddIcon /icons/hand.right.gif README
        AddIcon /icons/folder.gif ^^DIRECTORY^^
        AddIcon /icons/blank.gif ^^BLANKICON^^
 
        #
        # DefaultIcon is which icon to show for files which do not have an icon
        # explicitly set.
        #
        DefaultIcon /icons/unknown.gif
 
        #
        # AddDescription allows you to place a short description after a file in
        # server-generated indexes.  These are only displayed for FancyIndexed
        # directories.
        # Format: AddDescription "description" filename
        #
        #AddDescription "GZIP compressed document" .gz
        #AddDescription "tar archive" .tar
        #AddDescription "GZIP compressed tar archive" .tgz
 
        #
        # ReadmeName is the name of the README file the server will look for by
        # default, and append to directory listings.
        #
        # HeaderName is the name of a file which should be prepended to
        # directory indexes. 
        ReadmeName README.html
        HeaderName HEADER.html
 
        #
        # IndexIgnore is a set of filenames which directory indexing should ignore
        # and not include in the listing.  Shell-style wildcarding is permitted.
        #
        IndexIgnore .??* *~ *# HEADER* README* RCS CVS *,v *,t
</IfModule>

mod_proxy を無効化したので以下のコマンド実行。

1
mv /etc/httpd/conf.d/proxy_ajp.conf /etc/httpd/conf.d/proxy_ajp.conf.stop

MySQL の設定をする。

1
vi /etc/my.cnf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1
 
# character-set
character-set-server=utf8
skip-character-set-client-handshake
 
ft_min_word_len=1
 
# Disabling symbolic-links is recommended to prevent assorted security risks;
# to do so, uncomment this line:
# symbolic-links=0
 
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
 
[client]
default-character-set=utf8
 
[mysql]
default-character-set=utf8
 
[mysqldump]
default-character-set=utf8

MySQL 自動起動設定・初期化・ root パスワード設定。

1
2
3
/sbin/chkconfig mysqld on
/etc/rc.d/init.d/mysqld start
mysqladmin -u root password 'パスワード'

APC インストール。

1
pecl install APC

APC 有効化するためファイル編集。

1
vi /etc/php.ini

末尾に以下の記述を追加。

1
extension=apc.so

新規ユーザー作成。

1
2
adduser hoge
passwd hoge

新規ユーザーへ sudo 権限付加。

1
/usr/sbin/visudo

末尾に以下の記述。

1
hoge ALL=(ALL) ALL

Web 公開ディレクトリを弄れるように権限追加。

1
chown hoge /var/www/vhosts

SSH 関連の設定をします。

1
vi /etc/ssh/sshd_config

ポート番号を違う番号に変更・root ログイン禁止・空パスワードでのログイン禁止。それぞれの項目はバラバラにあるので検索して書き換えてください。

1
2
3
Port 10022
PermitRootLogin no
PermitEmptyPasswords no

SSH を再起動して設定反映。

1
/etc/init.d/sshd restart

ログアウト後新規ユーザでログイン。

1
ssh hoge@123.456.78.90 -p 10022

公開鍵認証を設定します。authorized_keys に公開鍵の記述を追加。

1
2
3
mkdir ~/.ssh
chmod 700 ~/.ssh
vi ~/.ssh/authorized_keys

パーミッション変更。

1
chmod 600 ~/.ssh/authorized_keys

公開認証鍵のみのログインに限定する場合以下の作業を。

1
vi /etc/ssh/sshd_config

以下のように設定。

1
PasswordAuthentication no

以上です。

コメント

コメントは受け付けていません。