さくらのVPSを契約してから、LAMP環境を構築するまでの手順を記録しておきます。
OSはCentOS 6.5 x86_64です。
事前準備
何はともあれ、まずはyum update
yum update
OSの言語を日本語にします。etc/sysconfig/i18n
を開き
LANG="ja_JP.UTF-8"
に変更して保存。
SELinuxはオフにします。vi/selinux/config
を開き
SELINUX=disabled
に変更して保存。
epel,remiレポジトリの追加
標準レポジトリはPHPやMySQLのバージョンが古いので、新しいバージョンをインストールするためにepel,remiレポジトリを追加します。
wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
rpm -ivh epel-release-6-8.noarch.rpm
rpm -ivh remi-release-6.rpm
作業用ユーザ設定
rootでずっと作業したくないので、作業用ユーザhogeを追加します。
useradd hoge
passwd hoge
<ユーザhogeに設定したいパスワードを入力>
ユーザhogeをwheelグループに追加
usermod -G wheel hoge
ユーザhogeがsudoを実行できるように設定を変更します。
visudo
開いたテキストファイル中の
#%wheel ALL=(ALL) ALL
の先頭の#を削除する。
公開鍵認証
hogeユーザで実行します。
mkdir ~/.ssh
chmod 700 ~/.ssh
手元のPCで実行します。
ssh-keygen -t rsa -v
chmod 600 .ssh/id_rsa.pub
scp ~/.ssh/id_rsa.pub hoge@IP.ADD.RE.SS:~/.ssh/authorized_keys
ssh -i ~/.ssh/id_rsa hoge@IP.ADD.RE.SS
完了したら、ssh hoge@IP.ADD.RE.SS
で、パスワードを聞かれることなくサーバにログインできることを確認。
サーバのセキュリティを高めます。
- SSHポート番号変更
- パスワード認証を禁止
- rootユーザのログインを禁止
/etc/ssh/sshd_config
を開き
Port 【10000以降で適当なポート番号】
PasswordAuthentication no
PermitRootLogin no
SSHDを再起動します。
service sshd restart
こちらの記事が非常に参考になりました。ありがとうございますm(__)m
「さくらのVPS」CentOS の初期設定の作業のまとめ&解説(iptablesによるファイアウォール編)「さくらのVPS」CentOS の初期設定の作業のまとめ&解説(iptablesによるファイアウォール編)
外部からMySQLへのアクセスを許可するために以下を追加します。
-A INPUT -p tcp --dport 3306 -j ACCEPT
-A INPUT -p tcp --sport 3306 -j ACCEPT
サーバーから外部へのHTTPS接続を許可するために以下を追加します。
-A INPUT -p tcp --sport 443 -j ACCEPT
-A OUTPUT -p tcp --dport 443 -j ACCEPT
インストール
OS起動時の自動起動設定も合わせて。
yum install httpd
chkconfig httpd on
DocumentRootの所有者がrootになっているので、apacheの実行ユーザに修正する。
chown -R apache:apache /var/www/html/
/etc/httpd/conf/httpd.conf
ServerTokens Prod
ServerSignature Off
Options -Indexes FollowSymLinks
インストール
yum --enablerepo=remi install php php-devel php-mysql php-gd php-mbstring
php.iniの編集
error_log = /var/log/php.log
mbstring.language = Japanese
mbstring.internal_encoding = UTF-8
mbstring.http_input = auto
mbstring.detect_order = auto
expose_php = Off
date.timezone = Asia/Tokyo
apacheの再起動
service httpd restart
インストール
yum --enablerepo=remi install mysql-server
my.cnfの編集
/etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
character_set_server=utf8
default-storage-engine=InnoDB
innodb_file_per_table
# Settings user and group are ignored when systemd is used (fedora >= 15).
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mysqld according to the
# instructions in http://fedoraproject.org/wiki/Systemd
user=mysql
# Semisynchronous Replication
# http://dev.mysql.com/doc/refman/5.5/en/replication-semisync.html
# uncomment next line on MASTER
;plugin-load=rpl_semi_sync_master=semisync_master.so
# uncomment next line on SLAVE
;plugin-load=rpl_semi_sync_slave=semisync_slave.so
# Others options for Semisynchronous Replication
;rpl_semi_sync_master_enabled=1
;rpl_semi_sync_master_timeout=10
;rpl_semi_sync_slave_enabled=1
# http://dev.mysql.com/doc/refman/5.5/en/performance-schema.html
;performance_schema
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[mysql]
default-character-set=utf8
[mysqldump]
default-character-set=utf8
OS起動時の自動起動設定も合わせて。
service mysqld start
chkconfig mysqld on
mysql_secure_installation