【XAMPP】CentOS6系のサーバーにXAMPPをインストールして公開する

既存サーバーを複製して、webアプリケーションが動くサーバーを作ることになりました。
しかし、既存サーバーはXAMPPで構築されていました。
ホスティング会社も異なるため、単純な複製もできません。

この記事では、一からVPSにXAMPPサーバーを建てるまでのフローを紹介します。

また、記録していたメモが消えてしまい、一部リンクの紹介になっている箇所があります
御了承ください。

イントロダクション

この記事で得られる事

  • XAMPPでwebサーバーを公開できる。

環境

XAMPPは7.2.2をインストールします。

種類 バージョン
OS CentOS 6.5
apache 2.4.29
MariaDB 10.1.30
php 7.2.2

サーバは、さくらのVPSです。 vps.sakura.ad.jp

手順

  1. XAMPPのインストール
  2. XAMPP起動
  3. XAMPPのスクリプト設定
  4. XAMMPの初期設定
  5. CentOSにPATHを通す
  6. apacheの公開設定
  7. MySQLの初期設定
  8. phpの初期設定

サーバー要件

インストールを進める上で、下記の要件を満たせる環境を作ります。

  • 作業ユーザーのhomeディレクトリを公開ディレクトリに変更
  • laravelが使える環境

XAMPPのインストール

最新バージョンの確認

公式ページにアクセスします。

www.apachefriends.org

2018/3/11現在でのバージョンは7.2.2でした。
公式ページから、「Linux向けXAMPP」のボタンのリンクをコピーします。

ソースをダウンロードしてインストール

先ほどコピーしたURLからlinuxのrunファイルをダウンロードします。
linuxのファイルなので、ダウンロードしたファイルを実行するとインストールが始まります。

$ wget https://www.apachefriends.org/XAMPP-files/7.2.2/XAMPP-linux-x64-7.2.2-0-installer.run
$ ./XAMPP-linux-x64-7.2.2-0-installer.run

これで、/opt/lampp配下に、XAMPPのファイルが作られます。

XAMPP起動

lampの起動ファイルは/opt/lampp/lamppにありますので、次のコマンドで起動します。

$ /opt/lampp/lampp start
Starting XAMPP for Linux 7.1.14-0...
XAMPP: Starting Apache...ok.
XAMPP: Starting MySQL...ok.

これでXAMPPの、apache、mysql、phpが使えるようになります。

もし、libc.so.6が足りない場合

(ブログ用にまとめていたのですが、途中で消えてしまいました…)

筆者の場合は発生したのですが、libc.so.6がないとエラーに弾かれる事があります。
足りないモジュールがないか、yum provides libc.so.6で確認してもおそらく問題がないと思います。

その場合、ABIのバージョンが使用している、libc.so.6がないという意味です。 同じモジュールでも、ABIのバージョンごとに、ディレクトリが別れます。

$ more /proc/version
Linux version 2.6.32-696.20.1.el6.x86_64 (mockbuild@x86_64_01.bsys.centos.org) (
gcc version 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC) ) #1 SMP Fri Jan 26 17:51:45
 UTC 2018

根本的な解決にはなりませんが、解決方法はあります。 既存のlibc.so.6が想定しているABIのバージョンまで上げます。
XAMPPでLD_ASSUME_KERNELをしている箇所がありますので、そこを書き換えます。

解決方法については、次のサイトを参照してください。

CentOS 6.9 (64bit) に xampp linux x64 5.6.31 をインストール « 宮腰ブログ

XAMPPのスクリプト設定

実行ファイルを/etc/rc.d/init.d/に置く事で、serviceで呼び出せるようになります。

$ cp /opt/lampp/lampp /etc/rc.d/init.d/

以降、lamppの実行は、service lampp <lamppオプション>のように実行が可能です。 次のコマンドは、lamppの起動コマンドです。

$ service lampp start

自動起動設定

システムの根幹にXAMPPがあって、再起動等で止まってしまうと困るので、自動起動設定をします。

$ chekconfig add lampp

XAMMPの初期設定

デフォルトの状態だと、後悔するのには怖い状態ですのでリスクをなるべく減らします。

セキュリティ設定

$ service lampp security

設定値については…大変恐縮ですが、記録し忘れてしまいました。
参考までに、参考にしたサイトを記載します。

XAMPP for LINUXを初心者が導入してみた セキュリティ・Apache設定編 | 株式会社まちいろ

ftp接続を停止

ftpの接続を拒否します。

$ service lampp stopftp

apacheの公開設定

現状だと、XAMPPのappacheが設定しているドキュメントルートのページを表示しています。
そのため、デフォルトのドキュメントルート設定は/opt/lampp/htdocsです。

今回の構成は、作業ユーザーのディレクトリを公開設定にしたいので、/home/<作業ユーザー>にしていきます。

まずhttpd.confを変更していきます。
lamppのconfigは/opt/lampp/etc/httpd.confにあります。
DocumentRootを、指定するディレクトリに変えます。

/opt/lampp/etc/httpd.conf

#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "/home/<作業ユーザー>"
<Directory "/home/<作業ユーザー>">

次に、apache本体で設定しているconfigを変更します。
htaccessの有効と、ファイル一覧を非表示を設定します。

/opt/lampp/apache2/conf/httpd.conf

<Directory "/home/<作業ユーザー>">
    Options -Indexes +FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

最後に、sslの設定をします。
後回しにしても良いのですが、忘れそうなので今のうちにやっておきます。

/opt/lampp/etc/extra/httpd-ssl.conf

#   General setup for the virtual host
DocumentRoot "/home/<作業ユーザー>"
ServerName www.example.com:443
ServerAdmin you@example.com
ErrorLog "/opt/lampp/logs/error_log"
TransferLog "/opt/lampp/logs/access_log"

XAMPP(apache)の再起動

apacheの再起動をします。XAMPPの場合はXAMPP自体に再起動のコマンドが用意されているので、今回はそれを使用します。
前述した、serviceへの登録が済んで入れば次のコマンドで実行できると思います。

$ service lampp restart

作業ディレクトリのパーミッション変更

apacheは起動しているので、これで外部とIPは繋がっています。
しかし、ディレクトリが外部から出見れる権限になっていませんので、権限を変更します。

$ chmod 755 home/<作業ユーザー>

これで、IPをブラウザに打ち込めば表示できるようになります。

CentOSにPATHを通す

次に、インストールしたミドルウェアをコマンドラインから実行しやすく設定します。

XAMPPのミドルウェアは、/opt/lampp/binにまとまっています。
そのため、XAMPPのミドルウェアをコマンドラインから動かす場合は、パスを通します。

$ touch ~/etc/profile.d/XAMPP.sh
$ export PATH="$PATH:/opt/lampp/bin" > ~/etc/profile.d/XAMPP.sh
$ source ~/etc/profile

これで、今後はphp、mysqlはXAMPPと同じものを使う事ができます。

$ php -v
php -v
PHP 7.1.14 (cli) (built: Feb 15 2018 18:28:23) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
$ mysql
…
MariaDB [(none)]>

以上の方法は、全ユーザーを対象としていますが、個別でユーザーにパスを通す事も可能です。
今回はやりません。

また、既にyumでミドルウェアをインストールした場合、おそらくミドルウェアがバッティングします。
今回は、XAMPPのミドルウェアを使う前提なので、yumでインストールしているミドルウェアは消した状態です。
消し方については、私が以前記載した次の記事を参照してください。

blog.websandbag.com

MySQLの初期設定

laravelを動かす想定ですので、MySQL(MariaDB)は使える状態が必須です。
デフォルトのままだと、リスクがありますので設定を行います。

初期設定を行うには、mysql_secure_installationを行い、次の設定を行います。

  • rootユーザーのパスワード設定
  • anonimousユーザー削除
  • rootユーザーのリモートログインを禁止
  • testデータベースの削除
$ mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we\'ll need the current
password for the root user.  If you\'ve just installed MariaDB, and
you haven\'t set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

You already have a root password set, so you can safely answer \'n\'.

rootユーザーのパスワード設定

Change the root password? [Y/n] Y (パスワードの変更をするかの設定)
New password: <rootのパスワード入力>
Re-enter new password: <rootのパスワード入力>
Password updated successfully!
Reloading privilege tables..
 ... Success!

anonimousユーザー削除

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] Y (anonymousユーザーの削除)
 ... Success!

rootユーザーのリモートログインを禁止

Normally, root should only be allowed to connect from \'localhost\'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] Y (rootユーザーのリモートログイン禁止)
 ... Success!

testデータベースの削除

By default, MariaDB comes with a database named \'test\' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] Y (testデータベースの削除)
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

最後に、上記までの設定を適用したら、初期設定は終わりです。

Reload privilege tables now? [Y/n] Y (今すぐ同期するかの確認)
 ... Success!

Cleaning up...

All done!  If you\'ve completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

ログイン確認

これで設定は終わりです。
念のため、rootログインできるか確認します。

$ mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 20
Server version: 10.1.30-MariaDB Source distribution

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type \'help;\' or \'\h\' for help. Type \'\c\' to clear the current input statement.

MariaDB [(none)]>

これで、MySQLの初期設定は終わりです。
後は、必要に応じてデータベース作ったり、ユーザーを追加したりしてください。

phpの初期設定

タイムゾーンを変更

php.iniのdate.timezoneを変更します。
おそらく、デフォルトはEurope/Berlinなので、Asia/Tokyoに変更します。

/opt/lampp/etc/php.ini

[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone=Asia/Tokyo

設定したら、文法チェックをして再起動します。

$ apachectl configtest
Syntax OK
$ service lampp restart
XAMPP: Stopping Apache...ok.
XAMPP: Stopping MySQL...ok.
XAMPP: Starting Apache...ok.
XAMPP: Starting MySQL...ok.

タイムゾーンの確認

次のコマンドで、現在設定されているtimezoneを確認します。
確認すると、Asia/Tokyoが選ばれているので反映されています。

$ php -i | grep timezone
Default timezone => Asia/Tokyo
date.timezone => Asia/Tokyo => Asia/Tokyo

©︎2017-2018 WebSandBag