自宅サーバー ≫ Linux Server ≫ CentOS 7 ≫ Webサーバー (apache)

 
apache のインストール
バージョン: Apache/2.4.6 (CentOS)
 
 
[root@centos]# yum -y install httpd
 
ホームページ用ディレクトリの作成
 
 
[root@centos]# useradd xxxx    (xxxx と言うユーザー及びディレクトリを追加)
 [root@
centos]# passwd xxxx     (xxxx のパスワード設定)
 New UNIX password:
******
     パスワード入力(* は実際には表示されません)
 Retype new password: ******
   パスワード再入力(* は実際には表示されません)
 
**** ディレクトリの中にホームページ公開用のサブディレクトリ www を作成
 
 
[root@centos]# mkdir /home/****/www
 
[root@centos]# chown ****. /home/****/www  (所有者の指定)
 
[root@centos]# chmod 775 /home/****/       (アクセス権の変更)
 
[root@centos]# chmod 775 /home/****/www/   (アクセス権の変更)
 
※ 新しくディレクトリを作成しなくても標準の /var/www/html を使う場合は上記は必要ない
※ 下記の設定もディレクトリ部分は変更の必要はない
apache の設定
 
 
[root@centos]# vi /etc/httpd/conf/httpd.conf
 
 
途中略
 
 #
 # Listen: Allows you to bind Apache to specific IP addresses and/or
 # ports, instead of the default. See also the <VirtualHost>
 # directive.
 #
 # Change this to Listen on specific IP addresses as shown below to
 # prevent Apache from glomming onto all bound IP addresses.
 #
 #Listen 12.34.56.78:80
 Listen
80   <--- 書き換え(通常は変更の必要はない ポートを指定する場合のみ変更)
 
 
途中略
 
 #
 # ServerAdmin: Your address, where problems with the server should be
 # e-mailed. This address appears on some server-generated pages, such
 # as error documents. e.g. admin@your-domain.com
 #
 ServerAdmin
root@localhost   <--- 書き換え(エラーページに表示される管理者のメールアドレスを指定)
   
 ↓
 ServerAdmin
xxxx@yyyy.zzz
 
 #
 # ServerName gives the name and port that the server uses to identify itself.
 # This can often be determined automatically, but we recommend you specify
 # it explicitly to prevent problems during startup.
 #
 # If your host doesn't have a registered DNS name, enter its IP address here.
 #
 #ServerName
www.example.com:80   <--- コメント解除して書き換え(サーバー名を指定)
  
 ↓
 ServerName
www.papa-net.info:80
 
 #
 # Deny access to the entirety of your server's filesystem. You must
 # explicitly permit access to web content directories in other
 # <Directory> blocks below.
 #
 <Directory />
     AllowOverride none
     Require all denied
 </Directory>

 #
 # Note that from this point forward you must specifically allow
 # particular features to be enabled - so if something's not working as
 # you might expect, make sure that you have specifically enabled it
 # below.
 #
 
 #
 # 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 "
/var/www/html"   <--- 書き換え(HPファイルを格納するフォルダーを 変更する場合は書き換え)
  
 ↓
 DocumentRoot "/home/****/www"
 
 ## Relax access to content within /var/www.
 #
 <Directory "/var/www">
   <--- 書き換え(HPファイルを格納するフォルダーを 変更する場合は書き換え)
  
 ↓
 <Directory "/home/****">
     AllowOverride None
     # Allow open access:
     Require all granted
 </Directory>

 # Further relax access to the default document root:
 <Directory "
/var/www/html">   <--- 書き換え(HPファイルを格納するフォルダーを変更する場合は書き換え)
 
  ↓
 <Directory "/home/****/www">
     #
     # 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 <--- 書き換え(index ファイルが無い場合フォルダ内を閲覧
 
    ↓                                                           できないようにする)
     Options FollowSymLinks           なお、CGIファイルを 任意の場所で起動したい場合は ExecCGI を追記

     #
     # 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.
     #
     Require all granted
 </Directory>

 #
 # DirectoryIndex: sets the file that Apache will serve if a directory
 # is requested.
 #
 <IfModule dir_module>
     DirectoryIndex index.html
   <--- 追記(index.htm や index.php 等必要なものを追加する)
       ↓
     DirectoryIndex index.html index.htm index.php index.html.var

 </IfModule>
 
 
途中略
 
 <IfModule alias_module>
     #
     # Redirect: Allows you to tell clients about documents that used to
     # exist in your server's namespace, but do not anymore. The client
     # will make a new request for the document at its new location.
     # Example:
     # Redirect permanent /foo http://www.example.com/bar

     #
     # Alias: Maps web paths into filesystem paths and is used to
     # access content that does not live under the DocumentRoot.
     # Example:
     # Alias /webpath /full/filesystem/path
     #
     # If you include a trailing / on /webpath then the server will
     # require it to be present in the URL. You will also likely
     # need to provide a <Directory> section to allow access to
     # the filesystem path.

     #
     # ScriptAlias: This controls which directories contain server scripts.
     # ScriptAliases are essentially the same as Aliases, except that
     # documents in the target 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/ "
/var/www/cgi-bin/"   <--- 書き換え(CGIファイルを格納するフォルダを指定
  
   ↓                     CGIファイル格納フォルダを複数作る場合はコメント化)
     ScriptAlias /cgi-bin/ "/home/****/www/cgi-bin/"

 </IfModule>

 #
 # "/var/www/cgi-bin" should be changed to whatever your ScriptAliased
 # CGI directory exists, if you have that configured.
 #
 <Directory "
/var/www/cgi-bin">   <--- 書き換え(CGIファイルを格納するフォルダーを指定
   
↓ CGIファイル格納フォルダを複数作る場合は<Directory "/var/www/cgi-bin">~</Directory>を追記
 <Directory "/home/****/www/cgi-bin"> 
     AllowOverride None
     Options
None   <--- 書き換え(CGIファイルを実行内容の指定<CGI・SSI・シンボリックの許可>)
 
    ↓
     Options Includes ExecCGI FollowSymLinks
     Require all granted
 </Directory>

<IfModule mime_module>
     #
     # TypesConfig points to the file containing the list of mappings from
     # filename extension to MIME-type.
     #
     TypesConfig /etc/mime.types

     #
     # AddType allows you to add to or override the MIME configuration
     # file specified in TypesConfig for specific file types.
     #
     #AddType application/x-gzip .tgz
     #
     # AddEncoding allows you to have certain browsers uncompress
     # information on the fly. Note: Not all browsers support this.
     #
     #AddEncoding x-compress .Z
     #AddEncoding x-gzip .gz .tgz
     #
     # If the AddEncoding directives above are commented-out, then you
     # probably should define those extensions to indicate media types:
     #
     AddType application/x-compress .Z
     AddType application/x-gzip .gz .tgz

     #
     # AddHandler allows you to map certain file extensions to "handlers":
     # actions unrelated to filetype. These can be either built into the server
     # or added with the Action directive (see below)
     #
     # To use CGI scripts outside of ScriptAliased directories:
     # (You will also need to add "ExecCGI" to the "Options" directive.)
     #
    
#AddHandler cgi-script .cgi   <--- コメント解除(CGIファイルを実行できるように指定< .pl 等を追加>)
  
   ↓
     AddHandler cgi-script .cgi .pl

     # For type maps (negotiated resources):
     #AddHandler type-map var

     #
     # Filters allow you to process content before it is sent to the client.
     #
     # To parse .shtml files for server-side includes (SSI):
     # (You will also need to add "Includes" to the "Options" directive.)
     #
     AddType text/html .shtml
     AddOutputFilter INCLUDES .shtml
 </IfModule>

 #
 # Specify a default charset for all content served; this enables
 # interpretation of all content as UTF-8 by default. To use the
 # default browser choice (ISO-8859-1), or to allow the META tags
 # in HTML content to override this choice, comment out this
 # directive:
 #
 AddDefaultCharset
UTF-8   <--- 書き換え(文字化け対策で文字コードの書き換え)
  
 ↓
 AddDefaultCharset off
 
 
以下略
 
Perl のインストール
ホームページ内で CGI を実行する時に必要な Perl をインストール
 
 [root@
centos]# yum -y install perl perl-CGI
 
Apache の起動
 
 [root@
centos]# systemctl start httpd
 
Apache の自動起動設定
 
 [root@
centos]# systemctl enable httpd
 
自動起動設定の確認
 
 [root@
centos]# systemctl list-unit-files | grep httpd
 
httpd.service                               enabled    <--- enabled になっていればOK
 
ホームページの表示確認
テストページの作成
 
 
[root@centos]# vi /home/****/www/index.html
 
以下新規作成
 <html>
 <head>
 <title>Test</title>
 </head>
 <body>
 Test Page
 </body>
 </html>

 
ネットワーク内の他のパソコンのからWebサーバーにアクセスしてテストページが表示されればOK
ユーザー認証ページ
ホームページにユーザー認証ページ(パスワードを入力しないと入れないページ)を追加

前提条件 : フォルダ=“pass” ユーザー名=“linuxuser” パスワード=“userpass”

ユーザー認証用 apache の設定
 
 
[root@centos]# vi /etc/httpd/conf/httpd.conf
 
 #
 # 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   <--- 書き換え
    
  ↓
     AllowOverride All
 
 
最終行に以下を追記
 <Directory /home/user/www/pass>   <--- フォルダの指定
 
  SSLRequireSSL(SSLでのアクセスのみを許可する場合に記述 尚、先にSSLサーバーを構築しないとエラーに なる)
    AuthName "this directory is SelectUser Only"
       (ユーザー認証画面に表示されるメッセージ)
    AuthUserFile /etc/httpd/conf/.passwd
        (パスワードファイルの指定)
    AuthType Basic
    AuthGroupFile /dev/null
    Require valid-user

 
</Directory>
 
ユーザー認証ページ用フォルダの作成
 
 
[root@centos]# mkdir /home/xxxx/www/pass/  (フォルダ pass 作成)
 
ドキュメントルートの所有者変更
 
 [root@centos
]# chown xxxx. /home/xxxx/www/pass/  (ドキュメントルートの所有者を xxxx に変更)
 
アクセス権を変更
 
 
[root@centos]# chmod 755 /home/xxxx/www/pass/
 
パスワードファイルの作成
Apache の設定ファイルのある /etc/httpd/conf へ 移動

 
[root@centos]# cd /etc/httpd/conf
 
パスワード設定

 [root@centos conf]#
htpasswd -c .passwd linuxuser  (ユーザー linuxuser パスワードファイルの作成)
 New password:
********                             パスワード入力(* は実際には表示されません)
 Re-type new password:
********                     パスワード再入力(* は実際には表示されません)
 Adding password for user linuxuser
 
なお、ユーザを追加したい場合は htpasswd  .passwd  ユーザ名  (-c を付けない)

作成したパスワードファイル .passwd を Apache の動作権限で読み取れるように所有者情報を変更


 
[root@centos conf]# chown apache.apache .passwd   (所有者情報を変更)
 
apache を再起動
 
 [root@
centos]# systemctl restart httpd
 
サブドメイン・バーチャルホスト
新しくサブドメイン (バーチャルホスト)用に /home/**** に www2 ディレクトリ作成
 
 
[root@centos]# mkdir /home/****/www2
 
ドキュメントルートの所有者変更
 
 [root@centos
]# chown xxxx. /home/****/www2/   (ドキュメントルートの所有者を xxxx に変更)
 
アクセス権を変更
 
 
[root@centos]# chmod 775 /home/****/www2
 
サブドメイン(バーチャルホスト)用設定ファイルの作成
apache の設定
 
 
[root@centos]# vi /etc/httpd/conf/httpd.conf
 
 
途中略
 
 #
 # ServerName gives the name and port that the server uses to identify itself.
 # This can often be determined automatically, but we recommend you specify
 # it explicitly to prevent problems during startup.
 #
 # If your host doesn't have a registered DNS name, enter its IP address here.
 #
 #ServerName
www.papa-net.info:80   <--- 書き換え
  
 ↓
 
NameVirtualHost *:80

 
以下略
 
virtualhost の設定
 
 
[root@centos]# vi /etc/httpd/conf.d/virtualhost.conf    (サブドメイン用設定ファイルの作成)
 
 
以下新規作成 
 

 NameVirtualHost *:80
 
 

 <VirtualHost *:80>
     ServerName www.papa-net.info
 
 </VirtualHost>
 
 

 <VirtualHost *:
80>
     DocumentRoot /home/****/www2
   <--- ホームページファイルの保存場所)
     ServerName ***.papa-net.info
   <--- (新しく追加するドメイン名 )
     ErrorLog logs/****.papa-net.info-error_log
   <--- (エラーログの保存場所)
     CustomLog logs/****.papa-net.info-access_log combined env=!no_log
   <--- (アクセスログの保存場所)
 </VirtualHost>
 
  
 
<Directory "/home/****/www2">   <--- (ホームページファイルの保存場所)
     Options FollowSymLinks
     AllowOverride All
 </Directory>

 
サブドメイン(バーチャルホスト)が別ホスト(別PC)にある場合は
 
 
[root@centos]# vi /etc/httpd/conf.d/virtualhost.conf    (サブドメイン用設定ファイルの作成)
 
 以下新規記述
 

 <VirtualHost *:80>
                         (新しく追加するサブドメインホスト)
    
ServerName ***.papa-net.info           (新しく追加するドメイン名 )
    
ProxyPass / http://192.168.1.***:80/   (ホームページファイルの保存 されているPCのアドレス)
 
</VirtualHost> 
  
 
<Directory "/home/****/www2">                 (ホームページファイルの保存場所)
     Options FollowSymLinks
     AllowOverride All
 </Directory>

 
なお、別ホスト(PC)側でWebサーバーの設定がされている事が前提条件
apache を再起動
 
 [root@
centos]# systemctl restart httpd
 
hosts ファイルの変更
  サブドメイン(バーチャルホスト)を使って複数のホームページを立ち上げた場合に練ってワーク内のクライアントPCからIPアドレスで接続しようとするとメインのホームページすか表示されない
DNSサーバーを構築すれば問題ないが、そこまでやる必要がない場合に hosts ファイルを変更してドメイン名で目的のホームページに行く事ができる

戻る