FreeBSD 10.2 に Apache、MariaDB、および PHP(FAMP)スタックをインストールする
このチュートリアルでは、FreeBSD 10.2 に Apache、MariaDB、および PHP (FAMP) スタックをインストールする方法について説明します。ご存知の通り、FAMP は FreeBSD、Apache、MySQL/MariaDB、PHP の頭文字を取った略語です。
このチュートリアルの目的のために、次のテスト用マシンを使用します。
- オペレーティングシステム: FreeBSD 10.2 64ビットシステム
- ホスト名: freebsd.ostechnix.local
- IPアドレス: 192.168.1.103/24
さて、では FreeBSD 10.2 に FAMP スタックをデプロイし始めましょう。
1. FreeBSD をアップデートする
他のどのオペレーティングシステムと同様に、ソフトウェアをインストールする前にFreeBSDを更新する必要があります。そのためには、rootユーザーに切り替えます:
$ suそして、次のコマンドを1つずつ実行して、FreeBSDサーバーを更新してください:
# freebsd-update fetch# freebsd-update install2. Apacheをインストールする
コマンドを使用してApacheウェブサーバーをインストールする:
# pkg install apache24次に、Apacheサービスを有効化して起動する必要があります。そのためには、次のコマンドを実行してください:
# sysrc apache24_enable=yes# service apache24 startApacheウェブサービスのテスト
私たちはApacheウェブサーバーをインストールしました。さて、それが動作しているかどうかをテストします。
そのためには、Apacheの設定ファイル /usr/local/etc/apache24/httpd.conf を編集してください。
# nano /usr/local/etc/apache24/httpd.conf次の行を見つけて、以下のように変更してください。自分の値に置き換えてください。
[...]
ServerAdmin ostechnix@ostechnix.local
.
.
.
ServerName localhost:80
[...]それから、以下のように Apache のルートディレクトリにサンプルの HTML ファイルを作成します。
# nano /usr/local/www/apache24/data/index.html以下の内容を追加してください:
<html>
<title>CONGRATULATIONS</title>
<body>
<h2>Welcome OSTechNix web server</h2>
</body>
</html>Apacheウェブサービスを再起動します。
# service apache24 restartサンプル出力:
Performing sanity check on apache24 configuration:
Syntax OK
Stopping apache24.
Waiting for PIDS: 3265.
Performing sanity check on apache24 configuration:
Syntax OK
Starting apache24.さて、ウェブブラウザを開き、次のURLにアクセスしてください:http://IP-address/ または http://localhost/。次のApacheテストページが表示されます。
おめでとうございます!Apacheウェブサーバーは正常に動作しています。
3. MariaDBをインストールする
MariaDBはMySQLのドロップイン置き換えです。MariaDBをインストールするには、次を実行します:
# pkg install mariadb100-server次に、MariaDBの設定ファイルをディレクトリ '/usr/local/share/mysql/' から '/usr/local/etc/' に下記のようにコピーします。
# cp /usr/local/share/mysql/my-medium.cnf /usr/local/etc/my.cnf次に、次のコマンドを使用してMariaDBサービスを有効化し、起動します。
# sysrc mysql_enable=yes# service mysql-server startMariaDBのrootユーザーのパスワードを設定する
ご存知の通り、MariaDB の root ユーザーはインストール時には空のパスワードになっており、これは推奨されません。したがって、MariaDB の root ユーザーを安全にするためには、root ユーザーに強力なパスワードを設定することが必須です。これを行うには、次のコマンドを実行してください:
# mysql_secure_installation「root の現在のパスワードを入力」と表示されたら、ENTER キーを押してパスワードを2回設定してください。その後、デフォルト値を受け入れるために単に Y を押してください。
サンプル出力:
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): ## Press Enter
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] ## Press Enter
New password: ## Enter password
Re-enter new password: ## Re-enter password
Password updated successfully!
Reloading privilege tables..
... Success!
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] ## Press Enter
... Success!
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] ## Press Enter
... Success!
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] ## Press Enter
- 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] ## Press Enter
... 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!これで完了です。MariaDBはインストールされ、安全になりました。続いてPHPをインストールしましょう。
4. PHPをインストールする
PHPをインストールするには、次を実行してください:
# pkg install mod_php56 php56-mysql php56-mysqliPHPをインストールした後、サンプルのPHP設定ファイル /usr/local/etc/php.ini-production を下記のように /usr/local/etc/ ディレクトリにコピーします。
# cp /usr/local/etc/php.ini-production /usr/local/etc/php.ini変更を更新するには、次を実行してください:
# rehash次に、ApacheウェブサーバーでPHPを構成する必要があります。そのためには、Apacheの設定ファイルを編集します:
# nano /usr/local/etc/apache24/httpd.conf以下のように、DirectoryIndex セクションを見つけ、既存の index.html の前に index.php を追加します。
[...]
<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>
[...]そして、Apacheの設定ファイルの最後に次の行を追加します:
<FilesMatch "\.php$">
SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch "\.phps$">
SetHandler application/x-httpd-php-source
</FilesMatch>
ファイルを保存して閉じる。
PHPのテスト
私たちはPHPをインストールし、Apacheウェブサーバーで動作するように設定しました。次に、PHPが動作しているかどうかを確認しましょう。そのためには、Apacheのルートディレクトリにtest.phpファイルを作成します:
# nano /usr/local/www/apache24/data/test.php次の行を追加してください:
<?php phpinfo(); ?>ファイルを保存して閉じる。
変更を適用するために、Apacheウェブサーバーを再起動してください。
# service apache24 restartWebブラウザを開き、http://IP-Address/test.php にアクセスします。PHPテスト構成ページが表示されます。
PHPモジュールをインストールする
PHPの機能を強化するために、PHPモジュール(拡張機能)をインストールする必要があります。これは任意ですので、拡張機能をインストールする必要がない場合はスキップできます。
利用可能なモジュールのリストを表示するには、次のコマンドを実行してください:
# pkg search php56上記出力のコメント欄から各モジュールが何をするかを確認することもできますし、次のコマンドを実行するだけでも確認できます
# pkg search -f php56-curlサンプル出力:
php56-curl-5.6.18
Name : php56-curl
Version : 5.6.18
Origin : ftp/php56-curl
Architecture : freebsd:10:x86:64
Prefix : /usr/local
Repository : FreeBSD [pkg+http://pkg.FreeBSD.org/FreeBSD:10:amd64/quarterly]
Categories : ftp
Licenses : PHP301
Maintainer : ale@FreeBSD.org
WWW : http://www.php.net/
Comment : The curl shared extension for php
[...]PHP拡張機能、例えば php56-curl-5.6.18 をインストールするには、次のように実行します:
# pkg install php56-curlおめでとうございます!この段階で、FAMPスタックはあなたのウェブサイトやウェブベースのアプリケーションをホストする準備が整いました。
Apacheウェブサーバーの代わりにNginxをインストールしたいですか?それなら、次のチュートリアルを参照してください。
- FreeBSD 10.2 に Nginx、MariaDB、PHP(FEMP)スタックをインストールする
ひとまず以上です。このチュートリアルを読んでいただきありがとうございます。ご質問がある場合は、下のコメント欄でお気軽にお尋ねください。できるだけ早く対応いたします。
このチュートリアルが役に立ったと思ったら、ぜひソーシャルネットワークで共有し、OSTechNixをサポートしてください。
乾杯!







