0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

CentOS8にApacheを入れてバーチャルホストを設定する

Posted at

環境

クラウド: さくらクラウド

OS: CentOS 8.0.1905 64bit

Apache: 2.4.37

まえおき

Apacheをバーチャルホストを設定したときにやったことをまとめてみました。説明で使うドメインは以下の2つとします。

domain-1.com

domain-2.com

apache設定ファイルの編集

Apacheをインストール

dnf install -y httpd

/etc/httpd/conf/httpd.confを編集

vim /etc/httpd/conf/httpd.conf
ServerRoot "/etc/httpd"
ServerTokens ProductOnly


Listen 80


Include conf.modules.d/*.conf


User apache
Group apache


ServerAdmin motani@eightbeat.co.jp



#全てのディレクトリに対する設定
<Directory />
    AllowOverride None
    Require all denied
    Options FollowSymLinks
</Directory>




# <Directory "/var/www">
#     AllowOverride None
#     Require all granted
# </Directory>


# <Directory "/var/www/html">
#     AllowOverride None
#     Require all granted
# </Directory>


# <Directory "/var/www/vhosts">
#     Options FollowSymLinks
#     AllowOverride All
#     Require all granted
# </Directory>



#dir_moduleモジュールがあった場合の設定
<IfModule dir_module>
    DirectoryIndex index.html

</IfModule>


<Files ".ht*">
    Require all denied

</Files>

#log_config_moduleモジュールがあった場合の設定
<IfModule log_config_module>
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common


<IfModule logio_module>
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio

    </IfModule>
 CustomLog "logs/access_log" combined
</IfModule>


<IfModule alias_module>
    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"

</IfModule>


<Directory "/var/www/cgi-bin">
    AllowOverride None
    Options None
    Require all granted

</Directory>


<IfModule mime_module>
    TypesConfig /etc/mime.types
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType text/html .shtml
    AddOutputFilter INCLUDES .shtml

</IfModule>




<IfModule mime_magic_module>
    MIMEMagicFile conf/magic

</IfModule>


ErrorLog "logs/error_log"
LogLevel warn
AddDefaultCharset UTF-8
EnableSendfile on
IncludeOptional conf.d/*.conf

virtual.confの設定

touch /etc/httpd/conf.d/virtual.conf
vim /etc/httpd/conf.d/virtual.conf
#htdocs配下の設定
<Directory /var/www/vhosts/*/htdocs>
    Options MultiViews SymLinksIfOwnerMatch IncludesNoExec ExecCGI
    AllowOverride Options FileInfo AuthConfig Limit Indexes

    <Limit GET POST PATCH DELETE OPTIONS PROPFIND>
      Require all granted
    </Limit>
    <LimitExcept GET POST PATCH DELETE OPTIONS PROPFIND>
      Require all denied
    </LimitExcept>
</Directory>

domain-1.comの設定ファイルを編集する

touch /etc/httpd/conf.d/domain-1.conf
vim /etc/httpd/conf.d/domain-1.conf
#domain-1.comの設定
<VirtualHost *:80>
    DocumentRoot /var/www/vhosts/domain-1.com
    ServerName domain-1.com
    CustomLog logs/domain-1.com-access_log ltsv
    ErrorLog logs/domain-1.com-error_log ltsv
</VirtualHost>

domain-2.comの設定ファイルを編集する

touch /etc/httpd/conf.d/domain-1.conf
vim /etc/httpd/conf.d/domain-1.conf
#domain-1.comの設定
<VirtualHost *:80>
    DocumentRoot /var/www/vhosts/domain-2.com
    ServerName domain-2.com
    CustomLog logs/domain-2.com-access_log ltsv
    ErrorLog logs/domain-2.com-error_log ltsv
</VirtualHost>

構文があってるかテスト

httpd -t

httpdを再起動

systemctl restart httpd

80番ポートを開ける

firewall-cmd --zone=public --add-service=http

ドキュメントルートの作成

domain-1.comのドキュメントルートとファイルを作成

cd /var
mkdir www/vhosts/domain-1.com
vim /var/www/vhosts/domain-1.com/index.html
chown -R apache:apache www

domain-1.comのドキュメントルートとファイルを作成

cd /var
mkdir www/vhosts/domain-2.com
vim /var/www/vhosts/domain-2.com/index.html
chown -R apache:apache www
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?