このチュートリアルでは、Apache、MariaDB、および PHP を FreeBSD 11 にインストールする方法を示します。FAMP が FreeBSD、Apache、MySQL/MariaDB、PHP の頭字語であることをご存じない場合。 FreeBSD 11 は、この記事が作成された時点での最新バージョンです。
前提条件
このガイドを始める前に、少なくとも Linux または UNIX の基本的な知識、Linux または UNIX の基本的なシェル コマンドの知識、root ユーザーとしてのログイン、そしてもちろん PC またはサーバーに FreeBSD 11 がインストールされている必要があります。
ステップ 1 – FreeBSD を更新する
ソフトウェアのインストールを続行する前に、まず FreeBSD 11 オペレーティング システムを更新する必要があります。
FreeBSD サーバーで次のコマンドを実行します。
$ su # freebsd-update fetch # freebsd-update install
最新のアップデートをインストールした場合は、次のような出力が得られるはずです
# freebsd-update fetch src component not installed, skipped Looking up update.FreeBSD.org mirrors... 4 mirrors found. Fetching public key from update5.freebsd.org... done. Fetching metadata signature for 11.0-RELEASE from update5.freebsd.org... done. Fetching metadata index... done. Fetching 1 metadata files... done. Inspecting system... done. Preparing to download files... done. No updates needed to update system to 11.0-RELEASE-p0.
# freebsd-update install src component not installed, skipped No updates are available to install. Run '/usr/sbin/freebsd-update fetch' first.
ステップ 2 – Apache ウェブサーバーをインストールする
pkg コマンドを使用して apache 2.4 をインストールします。
# pkg install apache24
お使いのオペレーティング システムで初めて pkg コマンドを使用する場合は、パッケージ管理ツールをインストールしてくださいという通知が表示されます。[y] を選択してください
# pkg install apache24 The package management tool is not yet installed on your system. Do you want to fetch and install it now? [y/N]: y
次に、次のコマンドを使用して、起動時に apache を実行できるようにします。
# sysrc apache24_enable=yes
次のコマンドを使用して apache サービスを開始します:
# service apache24 start
Apache Web サービスをテストする:
Apache をテストする前に、Apache 構成を構成する必要があります。 nano /usr/local/etc/apache24/httpd.conf を使用してこのファイルを編集します .
# nano /usr/local/etc/apache24/httpd.conf
IP アドレスまたはホスト名に基づいて、次のようにこの変数を見つけて編集します。この例では、IP アドレス 192.168.14.126 を使用しています:
ServerName 192.168.14.126:80
終了してファイルを保存します。次に、以下のコマンドを使用して apache サービスを再起動します:
# service apache24 restart
ここで、Web ブラウザーを開き、http://IP-address/ または http://localhost/ に移動します。 Apache テスト ページが表示されます。
ステップ 3 – MariaDB をインストールする
MariaDB は MySQL のドロップイン代替品です。プロセス名、構文、構成は同じです。インストールするには、次の pkg コマンドを実行します:
# pkg install mariadb100-server
ディレクトリ「/usr/local/share/mysql/」から MariaDB のサンプル構成をコピーします ‘/usr/local/etc/ に ':
# cp /usr/local/share/mysql/my-medium.cnf /usr/local/etc/my.cnf
MariaDB サービスを有効にして開始します:
# sysrc mysql_enable=yes # service mysql-server start
デフォルトの MariaDB インストールの root パスワードは空でした。セキュリティ上の理由から、次のコマンドを使用して root パスワードを作成します:
# mysql_secure_installation
「Enter current password for root」というプロンプトが表示されたら、ENTER キーを押してパスワードを 2 回設定します。次に、Y を押してデフォルト値を受け入れます。
出力例:
# 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. Set root password? [Y/n] Y New password: Re-enter new 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] ... 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] ... 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] - 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] ... 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!
ステップ 4 – PHP をインストールする
ダイナミック コンテンツをサーバーに追加するには、PHP が必要です。PHP をインストールするには、次のコマンドを実行します:
# pkg install mod_php56 php56-mysql php56-mysqli
インストールが成功したら、php.ini をコピーする必要があります サンプル構成:
# cp /usr/local/etc/php.ini-production /usr/local/etc/php.ini
このコマンドで変更を更新します:
# rehash
PHP を Apache Web サーバーで構成する必要があるため、nano を使用して 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>
ファイルを保存して閉じます。構成を変更するには、Apache ウェブサーバーを再起動する必要があります:
# service apache24 restart
ステップ 5 – PHP のテスト
PHP をテストするために、nano を使用してサンプルの php スクリプトを作成します:
# nano /usr/local/www/apache24/data/test.php
次の行を追加してください:
<?php phpinfo(); ?>
ファイルを保存して閉じます。 Web ブラウザーを開き、サーバー アドレス http://IP-Address/test.php に移動します。
ステップ 6 – PHP モジュール (拡張機能) をインストールする
PHP スクリプトを完全に機能させるために、追加の PHP モジュール (拡張機能) をインストールする必要がある場合があります。拡張機能をインストールする必要がない場合は、この手順をスキップできます。利用可能なモジュールのリストを表示するには、以下を実行してください:
# pkg search php56
出力例:
# pkg search php56 mod_php56-5.6.26 PHP Scripting Language php56-5.6.26 PHP Scripting Language php56-bcmath-5.6.26 The bcmath shared extension for php php56-bz2-5.6.26 The bz2 shared extension for php php56-calendar-5.6.26 The calendar shared extension for php php56-ctype-5.6.26 The ctype shared extension for php php56-curl-5.6.26 The curl shared extension for php php56-dba-5.6.26 The dba shared extension for php php56-dom-5.6.26 The dom shared extension for php php56-exif-5.6.26 The exif shared extension for php php56-extensions-1.0 "meta-port" to install PHP extensions php56-fileinfo-5.6.26 The fileinfo shared extension for php php56-filter-5.6.26 The filter shared extension for php php56-ftp-5.6.26 The ftp shared extension for php php56-gd-5.6.26 The gd shared extension for php php56-gettext-5.6.26 The gettext shared extension for php php56-gmp-5.6.26 The gmp shared extension for php php56-hash-5.6.26 The hash shared extension for php php56-iconv-5.6.26 The iconv shared extension for php php56-imap-5.6.26 The imap shared extension for php php56-interbase-5.6.26 The interbase shared extension for php php56-json-5.6.26 The json shared extension for php php56-ldap-5.6.26 The ldap shared extension for php php56-mbstring-5.6.26 The mbstring shared extension for php php56-mcrypt-5.6.26 The mcrypt shared extension for php php56-mssql-5.6.26 The mssql shared extension for php
上記の出力のコメント セクションから各モジュールの動作を確認するか、次のコマンドを実行するだけです。
# pkg search -f php56-curl
出力例:
# pkg search -f php56-curl php56-curl-5.6.26 Name : php56-curl Version : 5.6.26 Origin : ftp/php56-curl Architecture : freebsd:11:x86:64 Prefix : /usr/local Repository : FreeBSD [pkg+http://pkg.FreeBSD.org/FreeBSD:11:amd64/quarterly] Categories : ftp Licenses : PHP301 Maintainer : [email protected] WWW : http://www.php.net/ Comment : The curl shared extension for php Shared Libs required: libcurl.so.4 Annotations : cpe : cpe:2.3:a:php:php:5.6.26:::::freebsd11:x64 Flat size : 90.1KiB Pkg size : 26.3KiB Description : PHP, which stands for "PHP: Hypertext Preprocessor" is a widely-used Open Source general-purpose scripting language that is especially suited for Web development and can be embedded into HTML. Its syntax draws upon C, Java, and Perl, and is easy to learn. The main goal of the language is to allow web developers to write dynamically generated webpages quickly, but you can do much more with PHP. WWW: http://www.php.net/
php 拡張機能をインストールするには、たとえば php56-curl をインストールし、次のコマンドで実行します:
# pkg install php56-curl
インストール後に変更を適用するには、Apache Web サービスを再起動する必要があります:
# service apache24 restart
おめでとうございます。Apache、MariaDB、および PHP (FAMP) スタックを FreeBSD 11 に正常にインストールしました。これで、Web サイトまたは Web ベースのアプリケーションをホストする準備が整いました。