Cent OS Apache 웹서버 설치

Apache 설치

  • 설치 옵션에 따라 Apache Web Server 설치가 이미 되어 있을 수 있습니다.
  • 설치가 되어있다면 아래 처럼 확인 할 수 있습니다.

Cent OS Apache 설치1

  • 설치가 되어 있지 않은 경우는 아래의 명령어로 설치를 진행합니다.
1
$ yum install -y httpd

Cent OS Apache 설치2

Apache 같은 중요 인프라를 직접 소스를 컴파일해서 사용하는 방법도 있으나 직접 소스를 컴파일 하는 방법은 버그나 취약점도 직접 대응 해야하는 문제가 생겨 패키지 설치를 더 권장한다고 합니다.

  • 설치 후 잠시 설정파일과 로그 파일이 위치한 곳을 확인 해보겠습니다.
  • 경로는 /etc/httpd 입니다

Cent OS Apache 설치3

  • 주요 디렉토리 설명
  • conf: 웹 서버의 주요 설정 파일인 httpd.conf, MIME 형식을 지정하기 위한 파일인 magic 파일이 있는 곳
  • conf.d: 아파치의 주요설정을 분리 해서 저장 하는 곳, httpd.conf 설정내용을 분리하여 이곳에 저장하면, httpd.conf 파일에서 불러와서 사용하게 됩니다. httpd.conf 파일 맨 마지막에 IncludeOptional conf.d/*.conf 구문이 있습니다.
  • logs: 로그파일이 저장 되는 디렉토리
  • modules: 아파치 모듈 설치디렉토리
  • 설치가 완료되면 방화벽을 설정해줍니다.
1
2
3
$ firewall-cmd --permanent --add-service=http 
$ firewall-cmd --permanent --add-service=https
$ firewall-cmd --reload

방화벽 관련 파일 경로는 /etc/firewalld/zones/publlic.xml 파일에 설정 됩니다.

  • 이제 서비스를 활성화 시키고 부팅시 실행이 되게 해줍니다.
1
$ systemctl enable httpd
  • 서비스를 시작합니다
1
$ systemctl start httpd

Cent OS Apache 설치4

  • 서비스 접속을 웹브라우저를 통해 ip를 입력하고 접속 확인을 합니다.
  • http://{아이피주소}

Cent OS Apache 설치5

  • 웹브라우저에서 위 화면이 오면 Apache 설치 및 구동이 완료되었습니다.
  • 글을 마치기 전에 배포 디렉토리를 살펴보겠습니다.
  • /etc/httpd/conf/httpd.conf 파일을 vi로 열어보면 아래와 같은 구문이 보입니다.
  • 경로는 /var/www/html 에 문서를 위치하면 화면에 뿌려 주게됩니다.

Cent OS Apache 설치6

1
2
3
<IfModule dir_module>     
DirectoryIndex index.html
</IfModule>

/var/www/html 경로의 디렉토리에 들어가시면 파일이 존재하지 않습니다.
여기에 vi 에디터로 index.html 파일을 생성하고 저장하신 후 다시 사이트로 들어가시면 자신이 저장한 index.html파일이 화면에 뿌려지는 것을 확인하실 수 있습니다.

  • httpd.conf 수정으로 기본 home 디렉토리의 경로를 변경시 403 Forbidden You don’t have permission to access / on this server. 접근 권한 에러가 나타날 수 있습니다. SELinux 방화벽 해제를 하고 접근이 되는지 확인해 볼 수 있습니다.
1
$ setenforce 0

httpd.conf 파일 수정

1
2
3
4
5
6
7
8
9
10
11
12
13
# If you wish httpd to run as a different user or group, you must run
# httpd as root initially and it will switch.
# // 변경 전
# User/Group: The name (or #number) of the user/group to run httpd as.
# It is usually good practice to create a dedicated user and group for
# running httpd, as with most system services.
#
User apache
Group apache

# // 변경 후
User nobody
Group nobody
1
2
3
4
5
6
7
8
9
10
11
# 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 {서버아이피주소}:80
1
2
3
4
5
6
7
8
9
10
11
12
13
# Relax access to content within /var/www.
# // 변경 전
<Directory "/var/www">
AllowOverride None
# Allow open access:
Require all granted
</Directory>

# // 변경 후
<Directory "/">
AllowOverride None
Require all granted
</Directory>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Further relax access to the default document root:
# // 변경 전
<Directory "/var/www/html">
#
# 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.
#
Require all granted
</Directory>

# // 변경 후
<Directory "/var/www/html">
#
# 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.
#
Order allow,deny
Allow from all
</Directory>
1
2
3
4
5
6
7
8
9
10
11
# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
# // 변경 전
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>

# // 변경 후
<IfModule dir_module>
DirectoryIndex index.html index.html.var index.php index.u
</IfModule>
1
2
3
4
5
6
7
8
9
10
11
12
# The following lines prevent .htaccess and .htpasswd files from being 
# viewed by Web clients.
# // 변경 전
<Files ".ht*">
Require all denied
</Files>

# // 변경 후
<Files ~ "^\.ht">
Order allow,deny
Deny from all
</Files>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# "/var/www/cgi-bin" should be changed to whatever your ScriptAliased
# CGI directory exists, if you have that configured.
# // 변경 전
<Directory "/var/www/cgi-bin">
AllowOverride None
Options None
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

# 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>

# // 변경 후
<Directory "/var/www/cgi-bin">
AllowOverride None
Options None
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
AddType application/x-httpd-php .php .html .htm .inc
AddType application/x-httpd-php-source .phps

#
# 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
AddHandler php7-script .php .u

# 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>
  • httpd.conf 파일 수정 후 에러가 발생하면 httpd -t 명령어로 httpd.conf 몇째 줄이 틀렸는지 확인 해 볼 수 있습니다.
1
2
$ cd /etc/httpd/conf/ // httpd.conf 파일이 있는 경로로 이동
$ httpd -t

가상 호스트

각각의 도메인마다 다른 서비스를 하고 싶을때 아파치의 VirtualHost를 사용하면 편리합니다.

1
2
3
4
5
6
7
# Supplemental configuration
#
# Load config files in the "/etc/httpd/conf.d" directory, if any.
IncludeOptional conf.d/*.conf

# 가상호스트 .conf 파일 경로 추가
Include /etc/httpd/conf/extra/httpd-vhosts.conf
  • etc/httpd/conf/extra 디렉토리안에 httpd-vhosts.conf 파일을 생성 후 아래와 같이 작성합니다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 도메인1
<VirtualHost *:80>
ServerAdmin example@gmail.com
DocumentRoot /home/www/
ServerName example.com
ServerAlias example.co.kr
ServerAlias www.example.com
ServerAlias www.example.co.kr
ErrorDocument 404 /errors/error_404.php # 커스텀 404 에러 페이지
# ErrorLog logs/dummy-host.example.com-error_log
# CustomLog logs/dummy-host.example.com-access_log common

<Directory /home/www/main/>
Options FollowSymLinks
AllowOverride FileInfo
</Directory>

#<Directory /home/www/admin/>
# Options Indexes MultiViews FollowSymLinks
# AllowOverride All
#</directory>

<Files ~ "\.inc$">
Order allow,deny
Deny from all
</Files>

ErrorLog /dev/null
CustomLog /dev/null common
</VirtualHost>

# 도메인2
<VirtualHost *:80>
ServerAdmin example@gmail.com
DocumentRoot /home/event/
ServerName example2.co.kr

<Directory /home/event/>
Options FollowSymLinks
AllowOverride FileInfo
</Directory>
</VirtualHost>

DNS 서버가 있다면 이 과정은 필요가 없습니다.
DNS 서버가 없다는 가정 하에, local에서 테스트를 하기 위해서는 hosts 파일을 수정해야 합니다.

우선 브라우저를 실행하는 것은 윈도우이기 때문에 윈도우의 hosts 파일 C:\Windows\System32\drivers\etc 에서 도메인을 등록합니다.
(관리자 권한으로 실행해서 저장해야 합니다.)

1
2
3
4
5
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost

192.168.209.142 example.com

서버의 IP 주소를 확인한 후, 위와 같이 IP와 URL을 작성합니다.
즉 example.com 으로 접근하면 192.168.209.142 임을 알 수 있도록 도메인에 등록한 것입니다.

참조

공유하기