LoginSignup
50
66

More than 5 years have passed since last update.

CentOS6 or 7 + Apache2系のインストール手順

Last updated at Posted at 2017-05-19

概要

CentOS6もしくは7にApache2系インストールする手順。

環境

CentOS6系

  • CentOS 6.7
  • Apache 2.2.15

CentOS7系

  • CentOS 7.1
  • Apache 2.4.6

Apacheのインストール

$ sudo yum install -y httpd

起動と自動起動設定

CentOS6系の場合

$ sudo /etc/init.d/httpd start
$ sudo chkconfig httpd on

インストールされるバージョン

$ httpd -v
Server version: Apache/2.2.15 (Unix)
Server built:   Mar 22 2017 06:52:55

CentOS7系の場合

$ sudo systemctl restart httpd
$ sudo systemctl enable httpd
ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'

インストールされるバージョン

$ httpd -v
Server version: Apache/2.4.6 (CentOS)
Server built:   Apr 12 2017 21:03:28

ServerNameを設定

/etc/httpd/conf/httpd.confで、
ServerNameの項目を探し、ドメインを設定する。
ドメインが存在しない場合、適当な文字列でも動きます。

$ sudo vi /etc/httpd/conf/httpd.conf
/etc/httpd/conf/httpd.conf
# **snip**

ServerName www.example.com:80

# **snip**

参考:Apacheでhttpd: Could not reliably determine the server's fully qualified domain nameと言われる解決方法

Virtualhostの設定

/etc/httpd/conf/httpd.conf とは別に /etc/httpd/conf.d の下に、
各virtualhostごとの設定ファイルを作成する。
DocumentRootは、/var/www/html/sample_project/publicとする。

$ sudo vi /etc/httpd/conf.d/sample_project.conf
/etc/httpd/conf.d/sample_project.conf
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName sample_project_web.com
    ServerAlias www.sample_project_web.com

    DirectoryIndex index.html index.php
    DocumentRoot "/var/www/html/sample_project/public/"

    <Directory "/var/www/html/sample_project/public">
        Options FollowSymLinks
        AllowOverride All
        # Apache2.4未満の場合は不要
        Require all granted
    </Directory>

    ErrorLog /var/log/httpd/sample_project/error.log
    CustomLog /var/log/httpd/sample_project/access.log combined
    # E_ALL & ~E_NOTICE
    php_value error_reporting 6135
</VirtualHost>

Apache2.4未満の場合、Require all grantedの記述があると、
configuration error: couldn't perform authentication. AuthType not set!: /
このようなエラーが出てしまうので、記述しないようにする。
CentOS6系では、Apache2.4未満がインストールされるようなので注意。

参考記事:http://stackoverflow.com

httpへのアクセスをすべてhttpsにリダイレクトする設定

以下は、Apache/2.4.8以前の場合の設定。
SSLCertificateFileは、Apache/2.4.8以降や、nginxでは使用しません。

<VirtualHost *:80>
  ServerName sample_project_web.com:80

  RewriteEngine On
  LogLevel alert rewrite:trace3
  RewriteCond %{HTTPS} off
  RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</VirtualHost>

<VirtualHost *:443>
    ServerAdmin webmaster@localhost
    ServerName sample_project_web.com
    ServerAlias www.sample_project_web.com

    DirectoryIndex index.html index.php
    DocumentRoot "/var/www/html/sample_project/public"

    SSLEngine on
    SSLProtocol all -SSLv2
    SSLCertificateKeyFile   /etc/pki/tls/private/sample_project.key
    SSLCertificateChainFile /etc/pki/tls/private/sample_project.crt_and_key
    SSLCertificateFile      /etc/pki/tls/certs/sample_project.crt

    SetEnv ENV development

    <Directory "/var/www/html/sample_project/public">
        Options FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog /var/log/httpd/sample_project/error.log
    CustomLog /var/log/httpd/sample_project/access.log combined
    # E_ALL & ~E_NOTICE
    php_value error_reporting 6135
</VirtualHost>

アプリケーションファイルの設置ディレクトリの権限を適切にする

アプリケーションファイルが設置されている場所を、apacheの実行ユーザーがreadできる権限を持つ必要がある。

/var/www/htmlを、アプリケーションファイルの所有者に変更する。
下記はvagrantユーザーを使用している例。

$ sudo chown vagrant:vagrant /var/www/html

ディレクトリのパーミッションが755であることを確認する。

$ ll /var/www

drwxr-xr-x 3 root root 27  5月 18 21:03 html

参考:Webサーバーのパーミッションの設定〜ユーザー権限の考え方

Virtualhostのログ出力用ディレクトリを作成

$ sudo mkdir /var/log/httpd/sample_project

Apacheの再起動

confの文法チェック

apacheを再起動する前に、書きコマンドでconfの文法チェックを行う。

$ sudo apachectl configtest
Syntax OK

Apacheの再起動

$ sudo apachectl restart

もしくは

CentOS6系の場合

$ sudo /etc/init.d/httpd restart

CentOS7系の場合

$ sudo systemctl restart httpd

Firewall設定

CentOS7の場合は、firewallの、80番 (http) と 443番 (https)のポートを開ける必要あり。CentOS6では不要。

$ sudo firewall-cmd --permanent --zone=public --add-service=http 
$ sudo firewall-cmd --permanent --zone=public --add-service=https
$ sudo firewall-cmd --reload

Basic認証のかけ方

htpasswdの作成

$ htpasswd -c -b /etc/httpd/conf/.htpasswd ユーザ名 パスワード

confの編集

httpd.confか、conf.d/sample.confに以下を追記。
Require all grantedは削除する。

    <Directory "/var/www/html/sample_project/public">
        Options FollowSymLinks
        AllowOverride All
-       Require all granted

+       AuthUserFile /etc/httpd/conf/.htpasswd
+       AuthGroupFile /dev/null
+       AuthName "Basic Auth"
+       AuthType Basic
+       Require valid-user
    </Directory>

以上

50
66
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
50
66