openSUSE16.1 : Webサーバー Apacheインストール

1. Webサーバー Apacheの導入

1.1 インストール
# zypper -n install apache2
1.2 基本設定
# vi /etc/sysconfig/apache2

146行目:管理者アドレスに変更
APACHE_SERVERADMIN=<管理者E-mail>

163行目:自ドメイン名に変更
APACHE_SERVERNAME="<自ドメイン名>"
# vi /etc/apache2/httpd.conf

197行目:ディレクトリ名のみでアクセスできるファイル名を設定(index.php index.cgi 追加)
DirectoryIndex index.html index.html.var index.php index.cgi
# systemctl start apache2
# systemctl enable apache2
Created symlink '/etc/systemd/system/httpd.service' → '/usr/lib/systemd/system/apache2.service'.
Created symlink '/etc/systemd/system/apache.service' → '/usr/lib/systemd/system/apache2.service'.
Created symlink '/etc/systemd/system/multi-user.target.wants/apache2.service' → '/usr/lib/systemd/system/apache2.service'.

1.3 Firewalld を有効

HTTP サービスの許可が必要です。なお、HTTP は 80/TCP を使用します。HTTPSは443

# firewall-cmd --add-service=http --permanent
# firewall-cmd --add-service=https --permanent
# firewall-cmd --reload
1.4 起動確認

HTMLテストページを作成して動作確認をします。クライアントPC で Web ブラウザを起動し、以下のように作成したテストページにアクセスできれば OK です

# vi /srv/www/htdocs/index.html

<html>
<body>
<div style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;">
Apache Test Page
</div>
</body>
</html>

ブラウザで http://[IPアドレス] にアクセスし、下図のように表示される確認

2. Perlスクリプトを利用する

 Perl スクリプトを CGI として利用できるよう設定します
PerlはOSインストール時にすでにインストールされています

①CGI モジュールを有効にします

# a2enmod cgid

CGI の実行はデフォルトで [/srv/www/cgi-bin/] 配下で許可されています。
よって、例えば [/srv/www/cgi-bin/index.cgi] スクリプトを作成して配置することで、[http://(httpd サーバー)/cgi-bin/index.cgi] へアクセス可能となります。 なお、当該設定は [/srv/www/cgi-bin/] 配下のファイルを全て CGI と扱うため、CGI 以外のファイルは表示不可です。
# 以下の設定により /srv/www/cgi-bin/ 配下では CGI の実行が許可されている

# grep -n "^ *ScriptAlias" /etc/apache2/default-server.conf
72:ScriptAlias /cgi-bin/ "/srv/www/cgi-bin/"

現状では/srv/www/cgi-bin/ディレクトリーがないので作成しておく

# mkdir /srv/www/cgi-bin

②テストスクリプト作成し、動作確認

# cat > /srv/www/cgi-bin/test_script <<'EOF'
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "Hello CGI\n";
EOF

スクリプトファイルに権限を与える

# chmod 705 /srv/www/cgi-bin/test_script

動作確認

# curl http://localhost/cgi-bin/test_script
Hello CGI

「Hello CGI」と表示されれば正常です

3. バーチャルホストの設定

ドメイン [FQDN] 
ディレクトリー /srv/www/htdocs/[FQDN] としてバーチャルホストの設定を行います
/srv/www/htdocs/ 配下に[FQDN] という名前のディレクトリー作成

# cd /srv/www/htdocs/
# mkdir [FQDN] 

[FQDN] ディレクトリー内にidex.htmlファイルを作成する

# vi [FQDN]/index.html

<html>
<body>
<div style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;">
Virtual Host Test Page
</div>
</body>
</html>

/etc/apache2/vhosts.d 配下に virtual_host.conf を新規に作成

# vi /etc/apache2/vhosts.d/virtual_host.conf

<VirtualHost *:80>
ServerName [FQDN]
ServerAdmin <管理者メールアドレス>
DocumentRoot /srv/www/htdocs/[FQDN]
ErrorLog /var/log/apache2/[FQDN].error.log
CustomLog /var/log/apache2/[FQDN].access.log combined
LogLevel warn
</VirtualHost>

hostsファイルの編集

# vi /etc/hosts

#
# hosts         This file describes a number of hostname-to-address
#               mappings for the TCP/IP subsystem.  It is mostly
#               used at boot time, when no name servers are running.
#               On small systems, this file can be used instead of a
#               "named" name server.
# Syntax:
#    
# IP-Address  Full-Qualified-Hostname  Short-Hostname
#

127.0.0.1       localhost localhost.localdomain
::1             localhost localhost.localdomain ipv6-localhost ipv6-loopback
192.168.11.83   suse.korodes.com ←追加
# special IPv6 addresses
fe00::0         ipv6-localnet

ff00::0         ipv6-mcastprefix
ff02::1         ipv6-allnodes
ff02::2         ipv6-allrouters
ff02::3         ipv6-allhosts
# systemctl restart apache2

webブラウザで"http://[FQDN]]"にアクセスすると下図の通り表示されればOK

4. PHPスクリプトを利用する

PHP をインストールして PHP スクリプトが利用できるように設定します

4.1 PHP をインストール・設定ファイル編集
# zypper -n install php8 php8-pear php8-mbstring
# vi /etc/php8/apache2/php.ini

WordPressを導入する場合に備えてアップロード容量を増やしておきます
693行目
post_max_size = 300M

851行目当たり
upload_max_filesize = 200M

964行目:タイムゾーンを日本に変更
date.timezone = 'Asia/Tokyo'
# systemctl restart apache2
4.2 PHP-FPM (FPM : FastCGI Process Manager)のインストール

①インストール

# zypper -n install php8-fpm-apache
# a2enmod proxy_fcgi
# a2enmod -l
actions alias auth_basic authn_core authn_file authz_host authz_groupfile authz_core authz_user autoindex cgi dir env expires include log_config mime negotiation setenvif ssl socache_shmcb userdir reqtimeout cgid php8 proxy proxy_fcgi
# systemctl enable --now php-fpm
# systemctl restart apache2
4.3 PHPテストページを作成して動作確認

テストページを作成

# vi /srv/www/htdocs/[FQDN]/info.php
下記を記入
<?php phpinfo(); ?>

クライアントPC で Web ブラウザを起動し、"http://[FQDN]/info.php"にアクセスし以下のように表示されれば OK です。