目的
GIPは1つ。そのサーバで複数のWebサイトをサポートする。
環境
- Ubuntu 14.04
- apache2 version: 2.4.7-1ubuntu4.1
- php5 version: 5.5.9+dfsg-1ubuntu4.5
- php5-mysql version: 5.5.9+dfsg-1ubuntu4.11
- mysql-server-5.6 version: 5.6.19-0ubuntu0.14.04.
- mysql-client-core-5.6 version: 5.6.19-0ubuntu0.14.04.
- sysv-rc-conf version: 0.99-7
- Wordpress version: latest
- ntp version: 1:4.2.6.p5+dfsg-3ubunt
時刻同期
ntp使用
$ sudo apt-get install ntp
/etc/ntp.conf
# server 0.ubuntu.pool.ntp.org
# server 1.ubuntu.pool.ntp.org
# server 2.ubuntu.pool.ntp.org
# server 3.ubuntu.pool.ntp.org
server ntp1.jst.mfeed.ad.jp iburst
server ntp2.jst.mfeed.ad.jp iburst
server ntp3.jst.mfeed.ad.jp iburst
$ sudo service ntp stop
$ sudo service ntp start
$ ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*ntp1.jst.mfeed. 133.243.236.17 2 u 844 1024 377 2.900 -2.852 0.374
+ntp2.jst.mfeed. 133.243.236.17 2 u 223 1024 377 3.195 -3.600 0.362
+ntp3.jst.mfeed. 133.243.236.17 2 u 845 1024 377 2.782 -3.345 0.653
-juniperberry.ca 193.79.237.14 2 u 984 1024 377 244.422 -14.140 2.015
タイムゾーン
$ sudo ln -sf /usr/share/zoneinfo/Japan /etc/localtime
作業用ユーザ作成
$ sudo adduser hoge
$ sudo password hoge
Enter new UNIX password: **************
Retype new UNIX password: **************
作業用ユーザへsudo権限付与
$ sudo visudo
/etc/sudoers.
# User privilege specification
root ALL=(ALL:ALL) ALL
hoge ALL=(ALL:ALL) AL
$ sudo gpasswd -a hoge sudo
/etc/group.
sudo:x:27:ubuntu,hoge
rootログイン不可・SSHポート変更・ssh接続設定
/etc/ssh/sshd_config.
# Port 22
Port [1024~65535]
# PermitRootLogin without-password
PermitRootLogin no
# 最終行の前に追記
AllowUsers ubuntu hoge
公開鍵設置用Dir(サーバ接続に鍵認証を使用する場合)
$ mkdir .ssh
$ chmod 700 .ssh
$ touch .ssh/authorized_keys
$ chmod 600 .ssh/authorized_keys
Apache
/etc/apache2/conf-available/security.conf
# ServerTokens Minimal
# ServerTokens OS
# ServerTokens Full
ServerTokens ProductOnly
# Set to one of: On | Off | EMail
ServerSignature Off
# ServerSignature On
# Set to one of: On | Off | extended
TraceEnable Off
# TraceEnable On
Wordpress設置用DIR作成
$ sudo mkdir -p /var/www/sites-available/test/htdocs
ドキュメントルート作成
$ sudo mkdir /var/www/sites-enable
シンボリックリンク作成
$ cd /var/www/sites-enable
$ sudo ln -s ../sites-available/test test
設定ファイル作成
$ cd /etc/apache2/sites-available
$ sudo touch test.conf
test.conf
ServerName ***.***.***.***
<VirtualHost *:80>
ServerName test
ErrorLog ${APACHE_LOG_DIR}/test-error.log
CustomLog ${APACHE_LOG_DIR}/test-access.log combined
DocumentRoot /var/www/sites-enable/test/htdocs
<Directory "/var/www/sites-enable/test/htdocs">
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
MySQL
mysql起動
$ sudo sysv-rc-conf mysql on
$ sudo service mysql start
MySQL サーバーをセキュリティで保護
$ sudo mysql_secure_installation
現在のrootアカウントのパスワードを入力
Enter current password for root (enter for none):
OK, successfully used password, moving on...
パスワード変更
Change the root password? [Y/n] Y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
匿名ユーザアカウントの削除
Remove anonymous users? [Y/n] Y
... Success!
Rootログイン無効化
Disallow root login remotely? [Y/n] Y
... Success!
テストデータベース削除
Remove test database and access to it? [Y/n] Y
- Dropping test database...
権限テーブルをリロードし、変更を保存
Reload privilege tables now? [Y/n] Y
... Success!
All done! If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!
ユーザ・DB作成
$ mysql -u root -p
MySQL データベースのユーザーとパスワードを作成
mysql> create user 'wordpress-hoge'@'localhost' identified by 'パスワード';
データベースを作成
mysql> create database `wordpress-db`;
データベースに対して、以前作成した WordPress ユーザーに対する完全な権限を付与
mysql> grant all privileges on `wordpress-db`.* TO "wordpress-hoge"@"localhost";
すべての変更を有効にするため、MySQL 権限をフラッシュ
mysql> flush privileges;
mysql> exit
- mysql起動時に「No directory, logging in with HOME=/」が出るのは、mysqlユーザのHOMEディレクトリが「/nonexistent」になっている為。
$ sudo service mysql stop
$ sudo mkdir /home/mysql
$ sudo chown mysql:mysql /home/mysql
$ sudo usermod -d /home/mysql/ mysql
$ sudo service mysql start
- mysqlログイン時に「Access denied for user ‘root’@’localhost’ (using password: NO)」が出るのは、rootユーザの権限設定がされていないから。
$ sudo service mysql stop
$ sudo mysqld_safe --skip-grant-tables &
$ mysql -u root
現状の確認
mysql> use mysql;
mysql> select * from user;
ユーザテーブル全削除してから設定を実行
mysql> truncate table user;
mysql> flush privileges;
mysql> grant all privileges on *.* to root@localhost identified by 'パスワード' with grant option;
パスワード設定しない場合
mysql> grant all privileges on *.* to root@localhost identified by '' with grant option;
mysql> flush privileges;
結果確認
mysql> select host, user from user;
mysql> quit;
$ sudo kill -KILL mysqld_safe PID
$ sudo kill -KILL mysqld PID
$ sudo service mysql start
$ mysql -u root -p
- mysqlログイン時に「ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/run/mysqld/mysql.sock' (2)」が出るのは、クライアントとMysqlとで通信に使用するファイルの参照先が異なっている為。
/etc/mysql/my.conf
[client]
port = 3306
socket = /var/run/mysqld/mysqld.sock
[mysqld]
user = mysql
pid-file = /run/mysqld/mysqld.pid
socket = /run/mysqld/mysqld.sock
socketファイルの破損が原因であれば、mysqlの再起動でファイルが再生成される。
ソケットを指定してログインすることも可能。
$ mysql --socket=~/mysql.sock -u ****** -p
Mysqlのアップデートが失敗する場合、debian.cnfで異なる定義がされている可能性がある。
/etc/mysql/debian.cnf
# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host = localhost
user = debian-sys-maint
password = ******
socket = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host = localhost
user = debian-sys-maint
password = ******
socket = /var/run/mysqld/mysqld.sock
basedir = /usr
WordPress
$ cd ~/work
$ sudo wget https://ja.wordpress.org/latest-ja.tar.gz
$ sudo tar zxvf latest-ja.tar.gz
$ sudo mv wordpress/* /var/www/sites-available/test/htdocs
$ sudo rm latest-ja.tar.gz
$ cd /var/www/sites-available/test/htdocs
$ sudo cp wp-config-sample.php wp-config.php
wp-config.php
# 作成したDB・USER・USERPASSWORDを記述
define('DB_NAME', 'wordpress-db');
define('DB_USER', 'wordpress-user');
define('DB_PASSWORD', 'your_strong_password');
# https://api.wordpress.org/secret-key/1.1/salt/で作成したデータをAuthentication Unique Keys and Saltsセクションに貼り付ける。
define('AUTH_KEY', '****************************************************************');
define('SECURE_AUTH_KEY', '****************************************************************');
define('LOGGED_IN_KEY', '****************************************************************');
define('NONCE_KEY', '****************************************************************');
define('AUTH_SALT', '****************************************************************');
define('SECURE_AUTH_SALT', '****************************************************************');
define('LOGGED_IN_SALT', '****************************************************************');
define('NONCE_SALT', '****************************************************************');
$table_prefix = 'wp_';
# FTPdefine('WP_DEBUG', false);の下に追記する。
define('FS_METHOD', 'direct');
define('WP_AUTO_UPDATE_CORE', 'minor');
- ブートストラップのwp-load.phpはWordpressをインストールしたディレクトリ内でwp-config.phpを探し、見つからない場合は、1つ上の階層を探す仕組みになっている。これを利用し、セキュリティ対策として、wp-config.phpは1階層上に置く。
wp-load.php
if ( file_exists( ABSPATH . 'wp-config.php') ) {
/** The config file resides in ABSPATH */
require_once( ABSPATH . 'wp-config.php' );
} elseif ( file_exists( dirname(ABSPATH) . '/wp-config.php' ) && ! file_exists( dirname(ABSPATH) . '/wp-settings.php' ) ) {
/** The config file resides one level above ABSPATH but is not part of another install */
require_once( dirname(ABSPATH) . '/wp-config.php' );
} else {
$ sudo mv wp-config.php ../
- hogeとwwwユーザがドキュメントルートでファイルを追加・削除・編集できるようにする。
$ sudo groupadd www
$ sudo usermod -a -G www hoge
$ groups
hoge adm cdrom sudo dip plugdev lpadmin sambashare www
$ sudo chown -R root:www /var/www/sites-available/test
$ sudo chmod 2775 /var/www/sites-available/test
$ find /var/www/sites-available/test -type d -exec sudo chmod 2775 {} +
$ find /var/www/sites-available/test -type f -exec sudo chmod 0664 {} +
- ウェブサーバーはwww-data(ubuntu)apache(RHEL)ユーザーとして実行されるため、ドキュメントルートでファイルを追加・削除・編集出来るように作成したwwwグループに追加する。
$ sudo usermod -a -G www www-data
$ sudo chown -R www-data /var/www/sites-available/test
$ sudo chgrp -R www-data /var/www/sites-available/test
$ sudo chmod 2775 /var/www/sites-available/test
$ find /var/www/sites-available/test -type d -exec sudo chmod 2775 {} +
$ find /var/www/sites-available/test -type f -exec sudo chmod 0664 {} +
$ sudo service apache2 restart
ブラウザへアドレス入力
http://test/wp-admin/install.php
上記手順でtest2,test3・・・を作成する。
wp-config.phpはホスト毎にAuthentication Unique Keys and Saltsセクションと接頭語を異なるものにする。
wp-config.php
# 作成したDB・USER・USERPASSWORDを記述
define('DB_NAME', 'wordpress-db');
define('DB_USER', 'wordpress-user');
define('DB_PASSWORD', 'your_strong_password');
# https://api.wordpress.org/secret-key/1.1/salt/で作成したデータをAuthentication Unique Keys and Saltsセクションに貼り付ける。
define('AUTH_KEY', '****************************************************************');
define('SECURE_AUTH_KEY', '****************************************************************');
define('LOGGED_IN_KEY', '****************************************************************');
define('NONCE_KEY', '****************************************************************');
define('AUTH_SALT', '****************************************************************');
define('SECURE_AUTH_SALT', '****************************************************************');
define('LOGGED_IN_SALT', '****************************************************************');
define('NONCE_SALT', '****************************************************************');
$table_prefix = 'wp2_';
# FTPdefine('WP_DEBUG', false);の下に追記する。
define('FS_METHOD', 'direct');
define('WP_AUTO_UPDATE_CORE', 'minor');