「インストール - Apache2(SUSE)」の版間の差分

ナビゲーションに移動 検索に移動
編集の要約なし
編集の要約なし
1行目: 1行目:
== 概要 ==
== 概要 ==
openSUSE 15.1でWebサーバを構築する。<br>
このページでは、SUSEにおいて、Webサーバを構築する手順を記載する。<br>
<br><br>
<br><br>


10行目: 10行目:
Webサーバが動作しているか確認するため、下記のようなテストページを作成する。<br>
Webサーバが動作しているか確認するため、下記のようなテストページを作成する。<br>
  sudo vi /srv/www/htdocs/index.html
  sudo vi /srv/www/htdocs/index.html
   
<br>
  index.html(テストページ)
  <syntaxhighlight lang="html">
  index.htmlファイル
<nowiki>
 
  <html>
  <html>
     <body>
     <body>
       <h1>Welcome to openSUSE 15.1 Web Site</h1>
       <h1>Welcome to SUSE Web Site</h1>
     </body>
     </body>
  </html>
  </html>
  </nowiki>
  </syntaxhighlight>
<br>
<br>
Webブラウザに http://localhost と入力する。<br>
Webブラウザに http://localhost と入力する。<br>
27行目: 27行目:
CGIファイルのディレクトリのパスは/srv/www/cgi-bin/である。<br>
CGIファイルのディレクトリのパスは/srv/www/cgi-bin/である。<br>
  sudo vi /srv/www/cgi-bin/test.cgi
  sudo vi /srv/www/cgi-bin/test.cgi
<br>
<syntaxhighlight lang="sh">
test.cgiファイル
   
   
  test.cgi
  #!/usr/bin/env bash
   
   
#!bin/bash
  echo "Content-type:text/html"
  echo "Content-type:text/html"
  echo "openSUSE Test CGI"
  echo "SUSE Test CGI"
</syntaxhighlight>
<br>
<br>
また、一般ユーザでも実行できるようにするため、以下のコマンドを実行する。<br>
また、一般ユーザでも実行できるようにするため、以下のコマンドを実行する。<br>
43行目: 46行目:
== PHPのインストール ==
== PHPのインストール ==
次に、PHPをインストールする。<br>
次に、PHPをインストールする。<br>
PHPのインストール手順においては、[[インストール - PHP7|こちらのページ]]を参照すること。<br>
PHPのインストール手順においては、[[インストール - PHP7]]を参照すること。<br>
<br>
<br>
PHPが正常に動作するか確認するため、/srv/www/htdocsディレクトリに、以下のようなPHPファイルを作成する。<br>
PHPが正常に動作するか確認するため、/srv/www/htdocsディレクトリに、以下のようなPHPファイルを作成する。<br>
51行目: 54行目:
<br>
<br>
  <syntaxhighlight lang="php">
  <syntaxhighlight lang="php">
test.phpファイル
  <?php
  <?php
   phpinfo();
   phpinfo();
57行目: 62行目:
<br>
<br>
Webブラウザを起動して、http://localhost/test.php と入力する。<br>
Webブラウザを起動して、http://localhost/test.php と入力する。<br>
インストール済みのPHPの情報が表示されていれば、Apacheが正しく動作している。<br>
インストール済みのPHPの情報が表示されていれば、Apache2が正しく動作している。<br>
<br><br>
<br><br>


69行目: 74行目:
Apache2の再起動は、以下のコマンドを実行する。<br>
Apache2の再起動は、以下のコマンドを実行する。<br>
  sudo systemctl restart apache2
  sudo systemctl restart apache2
<br>
Apache2の自動起動は、以下のコマンドを実行する。<br>
Apache2の自動起動は、以下のコマンドを実行する。<br>
  sudo systemctl enable apache2
  sudo systemctl enable apache2
<br><br>
== 仮想ホストの構築 ==
このセクションでは、SUSEにおいて、仮想ホストの構築手順を記載する。<br>
<br>
まず、/etc/apache2/vhosts.dディレクトリにアクセスする。<br>
このディレクトリは、仮想ホストの設定ファイル(*.confファイル)を配置するディレクトリである。<br>
cd /etc/apache2/vhosts.d
<br>
vhost.templateファイルをコピーして、vhost-sample.confファイルを作成する。<br>
以下に示すように、vhost-sample.confファイルの内容を編集する。<br>
<u>以下の例では、/home/<ユーザ名>/htdocsディレクトリをドキュメントルートとしている。</u><br>
# /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>
<br>
# /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>
<br>
次に、IPアドレスと仮想ホストのサーバ名の名前解決を行うため、/etc/hostsファイルを編集する。<br>
sudo vi /etc/hosts
<br>
/etc/hostsファイル
# Syntax:
#   
# IP-Address  Full-Qualified-Hostname  Short-Hostname
#
...略...
127.0.0.1  localhost
127.0.0.1  <仮想ホストのサーバ名>
...略...
<br>
仮想ホストの設定ファイル(*.confファイル)を反映させるため、Apache2を再起動する。<br>
sudo systemctl restart apache2
<br><br>
<br><br>


__FORCETOC__
__FORCETOC__
[[カテゴリ:SUSE]]
[[カテゴリ:SUSE]]

案内メニュー