インストール - Apache2(SUSE)
概要
このページでは、SUSEにおいて、Webサーバを構築する手順を記載する。
Apache2のインストール
パッケージ管理システムからインストール
まず、Apache2をインストールする。
Apacheのサーバルートのディレクトリは、/srv/www/htdocsディレクトリである。
sudo zypper install apache2
Apache2が正常に動作しているかどうかを確認するため、以下に示すようなテストページを作成する。
sudo vi /srv/www/htdocs/index.html
index.htmlファイル
<html>
<body>
<h1>Welcome to SUSE Web Site</h1>
</body>
</html>
Webブラウザに、http://localhost と入力する。
"Welcome to SUSE Web Site"と記載されたWebページが表示されれば、Apache2のインストールは完了である。
ソースコードからインストール
Apache2をビルドするために必要なライブラリをインストールする。
sudo zypper install apr-devel apr-util-devel libexpat-devel libnghttp2-devel zlib-devel libxml2-devel \
libopenssl-devel libopenssl-1_1-devel lua53-devel pcre-devel systemd-devel
Apache2の公式Webサイトにアクセスして、ソースコードをダウンロードする。
ダウンロードしたファイルを解凍する。
tar xf httpd-<バージョン>.tar.bz2 cd httpd-<バージョン>
ビルド用ディレクトリを作成して、Apache2をビルドおよびインストールする。
mkdir build_Apache2 && cd build_Apache2
../configure --prefix=<Apache2のインストールディレクトリ> \
--with-apr=/usr \
--with-apr-util=/usr \
--with-included-apr=/usr/include/apr-1 \ # srclib内のAPRとAPR-Utilもビルド対象にする
--with-mpms-shared=all \ # worker, prefork, eventをビルド対象にする
--with-pcre=<PCREのインストールディレクトリ> \ # 例. /usr/local/src/pcre-<バージョン>/pcre-config
--with-ssl=<OpenSSLのインストールディレクトリ> \ # 例. /usr/local/src/ssl
--enable-dav --enable-dav-fs \
--enable-deflate \
--enable-headers \
--enable-mods-shared=all \ # 動的モジュールをビルド対象にする
--enable-proxy --enable-proxy-ajp \
--enable-rewrite=shared \
--enable-ssl \ # SSLモジュールをビルド対象にする
--enable-speling=shared \
--enable-systemd
make -j $(nproc)
make install
Apache2サービスを操作するユーザとグループを作成する。
また、デフォルトシェルに/sbin/nologinを指定して、当ユーザでのログインを禁止する。
cd <Apache2のインストールディレクトリ> sudo groupadd www sudo useradd www -g www -s /sbin/nologin sudo chown -R www:www .
PHPを使用する場合、リクエストされたphpファイルを実行できるようにする。
この設定が無い場合、リクエストが来てもテキストファイルのように.phpファイルの内容を返してしまう。
Apache2の設定ファイルを以下のように編集する。
sudo vi /<Apache2のインストールディレクトリ>/conf/httpd.conf
# /<Apache2のインストールディレクトリ>/conf/httpd.confファイル LoadModule php_module modules/libphp.so <FilesMatch "\.php$"> SetHandler application/x-httpd-php </FilesMatch> <IfModule php_module> DirectoryIndex index.html default.php index.php AddHandler application/x-httpd-php .php </IfModule>
Apache2のサービスファイルを作成する。
sudo vi /usr/lib/systemd/system/httpd.service
# /usr/lib/systemd/system/httpd.serviceファイル
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)
[Service]
Type=forking
#Type=notify
#EnvironmentFile=/<Apache2の設定ファイルがあるディレクトリ>/conf/httpd.conf
#Environment=LD_LIBRARY_PATH=/usr/local/ssl/lib:/usr/local/lib:/usr/local/lib64:/usr/lib
ExecStart=/<Apache2のインストールディレクトリ>/bin/apachectl start
ExecReload=/<Apache2のインストールディレクトリ>/bin/apachectl graceful
ExecStop=/<Apache2のインストールディレクトリ>/bin/apachectl stop
#ExecStart=/<Apache2のインストールディレクトリ>/bin/httpd \$OPTIONS -DFOREGROUND
#ExecReload=/<Apache2のインストールディレクトリ>/bin/httpd \$OPTIONS -k graceful
#ExecStop=/bin/kill -WINCH \${MAINPID}
# We want systemd to give httpd some time to finish gracefully, but still want it to kill httpd after TimeoutStopSec if something went wrong during the graceful stop.
# Normally, Systemd sends SIGTERM signal right after the ExecStop, which would kill httpd. We are sending useless SIGCONT here to give httpd time to finish.
#KillSignal=SIGCONT
#PrivateTmp=true
[Install]
WantedBy=multi-user.target
Apache2のサービスファイルを有効にする。
sudo systemctl daemon-reload
HTTP/HTTPS通信用にファイアウォールのポートを開放する。
sudo firewall-cmd --zone=public --permanent --add-service=http sudo firewall-cmd --zone=public --permanent --add-service=https sudo firewall-cmd --reload
Aapche2を開始する。
sudo systemctl start httpd
Apache2が正常に動作しているかどうかを確認するため、以下に示すようなテストページを作成する。
初期状態では、Apache2のサーバルートディレクトリは、/<Apache2のインストールディレクトリ>/htdocsディレクトリである。
vi /<Apache2のサーバルートディレクトリ>/index.html # または sudo vi /<Apache2のサーバルートディレクトリ>/index.html
<!-- /<Apache2のサーバルートディレクトリ>/index.htmlファイル -->
<html>
<body>
<h1>Welcome to SUSE Web Site</h1>
</body>
</html>
Webブラウザに、http://localhost と入力する。
"Welcome to SUSE Web Site"と記載されたWebページが表示されれば、Apache2のインストールは完了である。
CGIの設定
まず、/etc/apache2/default-server.confファイルに以下の赤字の設定を追記する。
# "/srv/www/cgi-bin" should be changed to whatever your ScriptAliased
# CGI directory exists, if you have that configured.
#
<Directory "/srv/www/cgi-bin">
AllowOverride None
Options +ExecCGI -Includes
AddHandler cgi-script .cgi .pl
<IfModule !mod_access_compat.c>
Require all granted
</IfModule>
<IfModule mod_access_compat.c>
Order allow,deny
Allow from all
</IfModule>
</Directory>
上記の設定を反映させるため、Apache2を再起動する。
sudo systemctl restart apache2
次に、test1.cgiファイルを作成して表示する。
test1.cgiファイルの内容は、以下の通りである。
以下の例では、CGIファイルのディレクトリのパスは、/srv/www/cgi-bin/である。
sudo vi /srv/www/cgi-bin/test1.cgi
test1.cgiファイル
#!/usr/bin/env bash
echo "Content-Type: text/html"
echo ""
echo "<!doctype html>"
echo "<html><head><title>Test CGI</title></head>"
echo "<body>"
echo "CGI Shell Web Site"
echo "</body>"
echo "</html>"
また、cgiファイルにはPython等も使用できる。
以下の例では、CGIスクリプトにPython3を使用している。
test2.cgi
#!/usr/bin/env python3
print("Content-type: text/html\n")
print("<html><head><title>Test CGI</title></head>\n<body>")
print("<div style=\"width: 100%; font-size: 40px; font-weight: bold; text-align: center;\">")
print("CGI Python3 Web Site")
print("</div>")
print("</body>\n</html>")
また、一般ユーザでも実行できるようにするため、以下のコマンドを実行する。
sudo chmod 755 /srv/www/cgi-bin/test1.cgi sudo chmod 755 /srv/www/cgi-bin/test2.cgi
test1.cgiファイルおよびtest2.cgiファイルを実行するため、Webブラウザに http://localhost/cgi-bin/test1.cgi と入力する。
Webブラウザに"CGI Shell Web Site"と表示されていれば、正常に動作している。
Apache2の起動・停止・再起動
Apache2の起動は、以下のコマンドを実行する。
sudo systemctl start apache2
Apache2の停止は、以下のコマンドを実行する。
sudo systemctl stop apache2
Apache2の再起動は、以下のコマンドを実行する。
sudo systemctl restart apache2
Apache2の自動起動は、以下のコマンドを実行する。
sudo systemctl enable apache2
仮想ホストの構築
このセクションでは、SUSEにおいて、仮想ホストの構築手順を記載する。
まず、/etc/apache2/vhosts.dディレクトリにアクセスする。
このディレクトリは、仮想ホストの設定ファイル(*.confファイル)を配置するディレクトリである。
cd /etc/apache2/vhosts.d
vhost.templateファイルをコピーして、vhost-sample.confファイルを作成する。
以下に示すように、vhost-sample.confファイルの内容を編集する。
以下の例では、1つ目の仮想ホストのドキュメントルートを/home/<ユーザ名>/htdocsディレクトリ、
2つ目の仮想ホストのドキュメントルートを/srv/www/htdocsディレクトリとしている。
# /etc/apache2/vhosts.d/vhost-localhost.confファイル
# 仮想ホスト1
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName localhost
# 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 /srv/www/htdocs
# if not specified, the global error log is used
ErrorLog /var/log/apache2/localhost_error_log
CustomLog /var/log/apache2/localhost_access_log combined
# don't loose time with IP address lookups
HostnameLookups Off
# needed for named virtual hosts
UseCanonicalName Off
# configures the footer on server-generated documents
ServerSignature On
# Optionally, include *.conf files from /etc/apache2/conf.d/
#
# For example, to allow execution of PHP scripts:
#
# Include /etc/apache2/conf.d/php5.conf
#
# or, to include all configuration snippets added by packages:
# Include /etc/apache2/conf.d/*.conf
# ScriptAlias: This controls which directories contain server scripts.
# ScriptAliases are essentially the same as Aliases, except that
# documents in the realname directory are treated as applications and
# run by the server when requested rather than as documents sent to the client.
# The same rules about trailing "/" apply to ScriptAlias directives as to
# Alias.
#
ScriptAlias /cgi-bin/ "/srv/www/cgi-bin/"
# "/srv/www/cgi-bin" should be changed to whatever your ScriptAliased
# CGI directory exists, if you have one, and where ScriptAlias points to.
#
<Directory "/srv/www/cgi-bin">
AllowOverride None
Options +ExecCGI -Includes
AddHandler cgi-script .cgi .pl .py
<IfModule !mod_access_compat.c>
Require all granted
</IfModule>
<IfModule mod_access_compat.c>
Order allow,deny
Allow from all
</IfModule>
</Directory>
# UserDir: The name of the directory that is appended onto a user's home
# directory if a ~user request is received.
#
# To disable it, simply remove userdir from the list of modules in APACHE_MODULES
# in /etc/sysconfig/apache2.
#
<IfModule mod_userdir.c>
# Note that the name of the user directory ("public_html") cannot simply be
# changed here, since it is a compile time setting. The apache package
# would have to be rebuilt. You could work around by deleting
# /usr/sbin/suexec, but then all scripts from the directories would be
# executed with the UID of the webserver.
UserDir public_html
# The actual configuration of the directory is in
# /etc/apache2/mod_userdir.conf.
Include /etc/apache2/mod_userdir.conf
# You can, however, change the ~ if you find it awkward, by mapping e.g.
# http://www.example.com/users/karl-heinz/ --> /home/karl-heinz/public_html/
#AliasMatch ^/users/([a-zA-Z0-9-_.]*)/?(.*) /home/$1/public_html/$2
</IfModule>
#
# This should be changed to whatever you set DocumentRoot to.
#
<Directory "/srv/www/htdocs">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride None
#
# Controls who can get stuff from this server.
#
<IfModule !mod_access_compat.c>
Require all granted
</IfModule>
<IfModule mod_access_compat.c>
Order allow,deny
Allow from all
</IfModule>
</Directory>
</VirtualHost>
# /etc/apache2/vhosts.d/vhost-example.confファイル
# 仮想ホスト2
<VirtualHost *:80>
ServerAdmin webmaster@<サーバ名>
ServerName <サーバ名>
# 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/<ユーザ名>/htdocs
# if not specified, the global error log is used
ErrorLog /home/<ユーザ名>/htdocs/error/error_log
CustomLog /home/<ユーザ名>/htdocs/error/custom_log combined
# don't loose time with IP address lookups
HostnameLookups Off
# needed for named virtual hosts
UseCanonicalName Off
# configures the footer on server-generated documents
ServerSignature On
# Optionally, include *.conf files from /etc/apache2/conf.d/
#
# For example, to allow execution of PHP scripts:
#
# Include /etc/apache2/conf.d/php5.conf
#
# or, to include all configuration snippets added by packages:
# Include /etc/apache2/conf.d/*.conf
# ScriptAlias: This controls which directories contain server scripts.
# ScriptAliases are essentially the same as Aliases, except that
# documents in the realname directory are treated as applications and
# run by the server when requested rather than as documents sent to the client.
# The same rules about trailing "/" apply to ScriptAlias directives as to
# Alias.
#
ScriptAlias /cgi-bin/ "/home/<ユーザ名>/htdocs/cgi-bin/"
# "/srv/www/cgi-bin" should be changed to whatever your ScriptAliased
# CGI directory exists, if you have one, and where ScriptAlias points to.
#
<Directory "/home/<ユーザ名>/htdocs/cgi-bin">
AllowOverride None
Options +ExecCGI -Includes
AddHandler cgi-script .cgi .pl .py
<IfModule !mod_access_compat.c>
Require all granted
</IfModule>
<IfModule mod_access_compat.c>
Order allow,deny
Allow from all
</IfModule>
</Directory>
# UserDir: The name of the directory that is appended onto a user's home
# directory if a ~user request is received.
#
# To disable it, simply remove userdir from the list of modules in APACHE_MODULES
# in /etc/sysconfig/apache2.
#
<IfModule mod_userdir.c>
# Note that the name of the user directory ("public_html") cannot simply be
# changed here, since it is a compile time setting. The apache package
# would have to be rebuilt. You could work around by deleting
# /usr/sbin/suexec, but then all scripts from the directories would be
# executed with the UID of the webserver.
UserDir public_html
# The actual configuration of the directory is in
# /etc/apache2/mod_userdir.conf.
Include /etc/apache2/mod_userdir.conf
# You can, however, change the ~ if you find it awkward, by mapping e.g.
# http://www.example.com/users/karl-heinz/ --> /home/karl-heinz/public_html/
#AliasMatch ^/users/([a-zA-Z0-9-_.]*)/?(.*) /home/$1/public_html/$2
</IfModule>
#
# This should be changed to whatever you set DocumentRoot to.
#
<Directory "/home/suse/htdocs">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride None
#
# Controls who can get stuff from this server.
#
<IfModule !mod_access_compat.c>
Require all granted
</IfModule>
<IfModule mod_access_compat.c>
Order allow,deny
Allow from all
</IfModule>
</Directory>
</VirtualHost>
次に、IPアドレスと仮想ホストのサーバ名の名前解決を行うため、/etc/hostsファイルを編集する。
sudo vi /etc/hosts
/etc/hostsファイル
...略...
# Syntax:
#
# IP-Address Full-Qualified-Hostname Short-Hostname
#
127.0.0.1 localhost
127.0.0.1 <仮想ホストのサーバ名>
...略...
仮想ホストの設定ファイル(*.confファイル)を反映させるため、Apache2を再起動する。
sudo systemctl restart apache2
PHPのインストール
次に、PHPをインストールする。
PHPのインストール手順においては、インストール - PHP7を参照すること。
PHPが正常に動作するか確認するため、/srv/www/htdocsディレクトリに、以下のようなPHPファイルを作成する。
以下のコマンドでテスト用のPHPファイルを作成して表示する。
PHPファイルの内容は下記の通りである。
sudo vi /srv/www/htdocs/test.php
test.phpファイル
<?php
phpinfo();
?>
Webブラウザを起動して、http://localhost/test.php と入力する。
インストール済みのPHPの情報が表示されていれば、Apache2が正しく動作している。