ウェブサイト検索

CentOS/RHEL 8/7 に Lighttpd、PHP、および MariaDB をインストールする方法


Lighttpdは、他のウェブサーバーに比べてメモリの使用量が少なく、スピードが重要な環境向けに設計されたオープンソースの安全で高速、柔軟かつ最適化されたウェブサーバーです。

1つのサーバーで最大10,000の接続を同時に処理でき、効果的なCPU負荷管理を備え、FastCGI、SCGI、認証、出力圧縮、URLリライティングなどの高度な機能セットが付属しています。

Lighttpd は、優れた io インフラストラクチャによって、同じハードウェアで他の代替ウェブサーバーよりも数倍のパフォーマンスをスケールすることができるため、すべての Linux サーバーにとって優れたソリューションです。

このチュートリアルでは、ホスト名linux-console.netおよびIPアドレス192.168.0.103を持つCentOS/RHEL 8/7ディストリビューションにLighttpd、PHP、PHP-FPM、およびMariaDBをインストールする方法を説明します。

ステップ1: Lighttpdウェブサーバーをインストールする

1. Lighttpdをインストールするには、まず、次のyumコマンドを使用してシステムソフトウェアパッケージと使用可能なリポジトリを更新する必要があります。

# yum -y update

2. 次に、システムでEPELリポジトリを有効にし、次のコマンドを使用してソフトウェアパッケージを更新する必要があります。

# yum -y install epel-release
yum -y update

3. EPELリポジトリを有効にしたら、次のコマンドを実行してLighttpdをインストールできます。

# yum install lighttpd

4. Lighttpd パッケージがインストールされると、サービスを開始して自動的に起動時に開始するように設定し、以下のコマンドを使用して状態を確認してください。

# systemctl start lighttpd
systemctl enable lighttpd
systemctl status lighttpd

5. それでは、次のコマンドを使用してシステムにインストールされているLighttpdのバージョンを確認してください。

# lighttpd -v

lighttpd/1.4.55 (ssl) - a light and fast webserver

6. 今、ファイアウォールでHTTPとHTTPSのトラフィックを許可する必要があります。

# firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload

7. 今、ブラウザを開いて、次のURLにアクセスして、あなたのLighttpdウェブサーバーが動作しているか確認してください。

http://Your-Domain.com
OR
http://Your-IP-addr

Lighttpdのデフォルトのドキュメントルートディレクトリは/var/www/lighttpd/であり、メインの設定ファイルは/etc/lighttpd/lighttpd.confにあります。

ステップ2:CentOS 7にMySQLとしてMariaDBをインストールする

8. 次に、以下のコマンドを使用してLighttpdのMySQLサポートをインストールします。

# yum -y install mariadb mariadb-server

9. インストールが完了したら、次のコマンドを使用してMariaDBを起動し、有効にし、状態を確認します。

# systemctl start mariadb.service
systemctl enable mariadb.service
systemctl status mariadb.service

10. 最後に、次のコマンドを発行してMariaDBのインストールを保護する必要があります。

# mysql_secure_installation

あなたは、MariaDBのインストールに関するいくつかの異なる質問と、それをどのように保護したいかについてのプロンプトが表示されます。データベースのルートユーザーのパスワードを変更したり、テストデータベースを無効にしたり、匿名ユーザーを無効にしたり、リモートからのルートログインを無効にしたりすることができます。

サンプル出力
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): Enter OK, successfully used password, moving on... Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorization. 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] y ... 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] y ... 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] y - 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!

11. MySQLサーバーに接続して、ターミナルで以下のコマンドを使用してデータベースサーバー上の既存のデータベースを確認してみてください。

# mysql -u root -p
サンプル出力
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.60-MariaDB MariaDB Server

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

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

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

MariaDB [(none)]>

ステップ3: CentOS 7にFastCGIを使用してPHPおよびPHP-FPMをインストールする

12. FastCGI サポートを持つ PHP-FPM を有効にするには、まず、次のコマンドを使用して PHP と必要な拡張をインストールする必要があります。

# yum -y install php php-mysqlnd php-pdo php-gd php-mbstring

13. PHPをインストールしたら、次にPHP-FPMとFastCGIのサポートをLighttpdに有効にします。そのためには、これらのパッケージもインストールする必要があります。

# yum -y install php-fpm lighttpd-fastcgi

14. では、/etc/php-fpm.d/www.confというファイルを開いてください。

# vi /etc/php-fpm.d/www.conf

Lighttpdにユーザーとグループを設定してください。

; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
; RPM: apache Choosed to be able to access some dir as httpd
user = lighttpd
; RPM: Keep a group allowed to write in log dir.
group = lighttpd

また、PHP-FPMはデフォルトで/var/run/php/php7.0-fpm.sockソケットを使用しますが、PHP-FPMをTCP接続を使用するように設定する必要があります。listen行を以下のようにしてください:

;listen = /var/run/php/php7.0-fpm.sock
listen = 127.0.0.1:9000 

15. 次に、PHP-FPMサービスを開始し、次のコマンドを使用して、起動時に自動的に開始されるように設定してください。

# systemctl start php-fpm.service
systemctl enable php-fpm.service

ステップ4: LighttpdでFastCGIを使用したPHPおよびPHP-FPMの有効化

ここでは、LighttpdのPHPサポートを有効にするために、3つのファイル/etc/php.ini、/etc/lighttpd/modules.conf、および/etc/lighttpd/conf.d/fastcgi.confを修正する必要があります。

/etc/php.iniの最初のファイルを開いてください。

# vi /etc/php.ini

cgi.fix_pathinfo=1という行のコメントを外してください。

; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI.  PHP's
; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
; what PATH_INFO is.  For more information on PATH_INFO, see the cgi specs.  Setting
; this to 1 will cause PHP CGI to fix its paths to conform to the spec.  A setting
; of zero causes PHP to behave as before.  Default is 1.  You should fix your scripts
; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://php.net/cgi.fix-pathinfo
cgi.fix_pathinfo=1

次に、/etc/lighttpd/modules.confという名前の2番目のファイルを開きます。

# vi /etc/lighttpd/modules.conf

次の行でinclude “conf.d/fastcgi.conf”と書かれた部分のコメントを外してください。

##
## FastCGI (mod_fastcgi)
##
include "conf.d/fastcgi.conf"

次に、/etc/lighttpd/conf.d/fastcgi.confという名前の3番目のファイルを開きます。

# vi /etc/lighttpd/conf.d/fastcgi.conf

ファイルの下部に次のコンテナを追加して保存してください。

fastcgi.server += ( ".php" =>
        ((
                "host" => "127.0.0.1",
                "port" => "9000",
                "broken-scriptfilename" => "enable"
        ))
)

Lighttpd サービスを再起動して変更を反映し、PHP サポートを有効にします。

# systemctl restart lighttpd

ステップ5: LighttpdでFastCGIサポートを使用したPHPとPHP-FPMのテスト

17. 前述したように、Lighttpd のデフォルトのドキュメントルートは/var/www/lighttpd/です。ここでは、このディレクトリにphpinfo.phpファイルを作成します。

# vi /var/www/lighttpd/info.php

次の行を追加してください。この小さなコードは、PHPのインストール情報とそのバージョンを表示します。

<?php
phpinfo();
?>

18. ブラウザを開いて、次のリンクに移動してください。

http://Your-Domain.com/info.php
OR
http://Your-IP-addr/info.php

PHP、PHP-FPM、およびMySQLの作業情報と、すでに有効になっている他の多くのモジュールが表示されます。

関連記事: