「インストール - Apache2(SUSE)」の版間の差分
| 22行目: | 22行目: | ||
<br> | <br> | ||
Webブラウザに http://localhost と入力する。<br> | Webブラウザに http://localhost と入力する。<br> | ||
" | "Welcome to SUSE Web Site"と記載された下記のWebページが表示されれば、Apache2のインストールは完了である。<br> | ||
<br> | <br> | ||
また、test1.cgiファイルを作成して表示する。test1.cgiファイルの内容は下記の通りである。<br> | |||
CGIファイルのディレクトリのパスは/srv/www/cgi-bin/である。<br> | CGIファイルのディレクトリのパスは/srv/www/cgi-bin/である。<br> | ||
sudo vi /srv/www/cgi-bin/ | sudo vi /srv/www/cgi-bin/test1.cgi | ||
<br> | <br> | ||
<syntaxhighlight lang="sh"> | <syntaxhighlight lang="sh"> | ||
| 36行目: | 36行目: | ||
echo "" | echo "" | ||
echo "<!doctype html>" | echo "<!doctype html>" | ||
echo "<html><head><title> | echo "<html><head><title>Test CGI</title></head>" | ||
echo "<body>" | echo "<body>" | ||
echo " | echo "CGI Shell Web Site" | ||
echo "</body>" | echo "</body>" | ||
echo "</html>" | echo "</html>" | ||
| 44行目: | 44行目: | ||
<br> | <br> | ||
また、cgiファイルにはPython等も使用できる。<br> | また、cgiファイルにはPython等も使用できる。<br> | ||
以下の例では、CGIスクリプトにPython3を使用している。<br> | |||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
test2.cgi | test2.cgi | ||
| 50行目: | 51行目: | ||
print("Content-type: text/html\n") | print("Content-type: text/html\n") | ||
print("<html>\n<body>") | 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("<div style=\"width: 100%; font-size: 40px; font-weight: bold; text-align: center;\">") | ||
print("CGI | print("CGI Python3 Web Site") | ||
print("</div>") | print("</div>") | ||
print("</body>\n</html>") | print("</body>\n</html>") | ||
| 58行目: | 59行目: | ||
<br> | <br> | ||
また、一般ユーザでも実行できるようにするため、以下のコマンドを実行する。<br> | また、一般ユーザでも実行できるようにするため、以下のコマンドを実行する。<br> | ||
sudo chmod 755 /srv/www/cgi-bin/ | sudo chmod 755 /srv/www/cgi-bin/test1.cgi | ||
sudo chmod 755 /srv/www/cgi-bin/test2.cgi | |||
<br> | <br> | ||
test1.cgiファイルおよびtest2.cgiファイルを実行するため、Webブラウザに http://localhost/cgi-bin/test1.cgi と入力する。<br> | |||
Webブラウザに" | Webブラウザに"CGI Shell Web Site"と表示されていれば、正常に動作している。<br> | ||
<br><br> | <br><br> | ||
2021年1月1日 (金) 17:17時点における版
概要
このページでは、SUSEにおいて、Webサーバを構築する手順を記載する。
Apache2のインストール
まず、WebサーバーソフトウェアのApache2をインストールするため、以下のコマンドを実行する。
Apacheのルートディレクトリのパスは/srv/www/htdocs/である。
sudo zypper install apache2
Webサーバが動作しているか確認するため、下記のようなテストページを作成する。
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のインストールは完了である。
また、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"と表示されていれば、正常に動作している。
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が正しく動作している。
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
<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
<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