본문 바로가기

Linux Server 구축/1-2. Web Server

Apache WebServer 설정


Section 2 : 'Main' server configuration 참고사항 : http://bban2.tistory.com/133

-------------------------------------------------------------------------------------------

1. 개인 사용자들의 홈페이지 디렉토리를 설정하는 방법


1-1. Apache  환경설정파일 설정

- 참고
개인 사용자들의 용량 및 게시물의 제한은 quota를 이용해서 설정한다.

[root@localhost conf]# pwd
/etc/httpd/conf
[root@localhost conf]# vi httpd.conf

351 <IfModule mod_userdir.c>
352     #
353     # UserDir is disabled by default since it can confirm the presence
354     # of a username on the system (depending on home directory줌
355     # permissions).
356     #
357#     UserDir disable   - 개인 사용자 홈페이지 디렉토리를 사용하지 않는다. 사용할 경우 주석처리를 해준다.
358
359     #
360     # To enable requests to /~user/ to serve the user's public_html
361     # directory, remove the "UserDir disable" line above, and uncomment
362     # the following line instead:
363     #
364     UserDir public_html  - 개인 사용자 홈페이지 디렉토리를 public_html로 지정한다. 사용할 경우 주석처리를 해제해준다.
365
366 </IfModule>
367
368 #
369 # Control access to UserDir directories.  The following is an example
370 # for a site where these directories are restricted to read-only.
371 #
372 #<Directory /home/*/public_html>  - 개인 사용자 홈페이지 디렉토리로 사용자들의 index.html 파일이 위치하는 경로이다.

-----------------------------------------------------------------------------------------------------

1-2. test1 사용자로 접속

- 사용자가 없을 경우 생성한다.
[root@localhost html]# useradd test1
[root@localhost html]# passwd test1
Changing password for user test1.
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully.

[root@localhost html]# su - test1  - test1 사용자로 전환한다.
[test1@localhost ~]$ pwd
/home/test1
[test1@localhost ~]$ mkdir public_html   - 개인 사용자 홈페이지 디렉토리인 public_html 디렉토리를 생성한다.
[test1@localhost ~]$ ls
public_html
[test1@localhost ~]$ cd public_html/
[test1@localhost public_html]$ vi index.html  - 파일 생성
[test1@localhost public_html]$ cat index.html
Hello~~
Here's test1's Homepage
Nice to meet you!! ^^

-----------------------------------------------------------------------------------------------------

1-3. 루트에서 test1 사용자 폴더에 대한 접근 퍼미션을 설정

[root@localhost ~]# pwd
/root
[root@localhost ~]# cd /home
[root@localhost home]# ll
합계 20
drwx------ 2 root  root  16384 11월  8 22:49 lost+found
drwx------ 4 test1 test1  4096 12월 13 22:35 test1
[root@localhost home]# chmod 705 test1   - test1 사용자 폴더의 퍼미션을 제 3자가 들어올 수 있도록 수정해준다.
[root@localhost home]# ll
합계 20
drwx------ 2 root  root  16384 11월  8 22:49 lost+found
drwx---r-x 4 test1 test1  4096 12월 13 22:35 test1
[root@localhost home]# cd test1
[root@localhost test1]# ll
합계 4
drwxrwxr-x 2 test1 test1 4096 12월 13 22:35 public_html

-----------------------------------------------------------------------------------------------------

1-4. 서비스 재시작


[root@localhost html]# service httpd restart
httpd 를 정지 중:                                          [  OK  ]
httpd (을)를 시작 중:                                      [  OK  ]

-----------------------------------------------------------------------------------------------------

1-5. 웹브라우저에서 test1 홈페이지 확인



- 참고
문제가 발생시에는 # tail -f /var/log/httpd/error_log 를 이용해 무엇이 문제인지 알 수 있다.

-----------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------

2. 특정 디렉토리에 대한 설정


2-1. Apache  환경설정파일 설정

#vi /etc/httpd/conf/httpd.conf

<Directory "/var/www/html">
     Options Indexes FollowSymLinks
     AllowOverride None
     Order allow,deny
     Allow from all
</Directory>

특정 디렉토리를 제어할 때 사용하는 지시어로 <Directory/></Directory>의 형태로 작성하며,
각각의 디렉토리에 관한 권한등의 여러 설정등을 할 수 있다.

즉, 외부에서 나의 홈페이지 (/var/www/html/index.html) 에 접근하는 것을 허용 또는 차단할 수 있다.

우선 DNS 서버를 상대방의 DNS 서버명을 적어준다.
예를들어 /etc/resolv.conf 파일에서 nameserver 10.10.33.2를 추가해 준다음 10.10.33.2 로 접속을 하면 상대방의 index.html 파일을 볼 수 있다.

[root@localhost conf]# pwd
/etc/httpd/conf
[root@localhost conf]# vi httpd.conf
308 <Directory "/var/www/html">
309
310 #
311 # Possible values for the Options directive are "None", "All",
312 # or any combination of:
313 #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
314 #
315 # Note that "MultiViews" must be named *explicitly* --- "Options All"
316 # doesn't give it to you.
317 #
318 # The Options directive is both complicated and important.  Please see
319 # http://httpd.apache.org/docs/2.2/mod/core.html#options
320 # for more information.
321 #
322     Options Indexes FollowSymLinks
323
324 #
325 # AllowOverride controls what directives may be placed in .htaccess files.
326 # It can be "All", "None", or any combination of the keywords:
327 #   Options FileInfo AuthConfig Limit
328 #
329     AllowOverride None  - 사용자 인증에 관한 지시자이다. 현재 설정은 사용자 인증 지시자를 사용하지 않는다.
330
331 #
332 # Controls who can get stuff from this server.
333 #
334     Order allow,deny  - 해당 디렉토리에 대한 IP, DNS 필터링의 순서를 설정한다. allow / deny는 호스트의 접근을 허용/거부 한다.
335     Allow from all
336 #   Allow from 10.10.33.2   
337 #   deny from all
338
339 </Directory>

위와 같이 설정되어 있다면 누구나 10.10.33.2의 index.html 파일에 접근이 가능하다.

하지만 order allow,deny 라고 설정되어 있고
allow from all의 설정을 deny from all(또는 IP 주소) 라고 설정을 바꾸어 주면 모든 사용자가 10.10.33.2의 index.html 파일을 볼수 없다.


-----------------------------------------------------------------------------------------------------

- 참고
Order allow,deny  - 우선순위는 deny ---> allow 순이다.

예1)
335       Order deny,allow
337       Allow from 10.10.2.106
338       deny from all

이렇게 설정이 되어 있다면 order의 순서에 따라 allow의 설정이 우선적으로 되고 deny의 설정이 그 다음으로 적용된다.
즉, 10.10.2.106 은 접속이 되고 나머지 IP대역들은 차단이 된다.

예2)
334      Order allow,deny
337       Allow from 10.10.2.106
338       deny from all

이렇게 설정이 되어 있다면 order의 순서에 따라 deny의 설정이 우선적으로 되고 allow의 설정이 그 다음으로 적용된다.
즉, 모든 IP대역들이 차단된다. allow의 설정은 무시된다.

예3)
334     Order allow,deny or Order deny,allow

이렇게 설정을 한다면 order 의 순서에 따라 deny의 설정이 우선시 되게 되는데 설정이 없으므로 기본설정에 의해 모든 IP대역이 차단된다.
order deny,allow로 설정한다면 당연히 모든 IP대역이 허용된다.


예4)
334     Order allow,deny or Order deny,allow
336     Allow from all or deny from all

이렇게 설정을 한다면 order 의 순서에 따라 deny가 우선이 되지만 설정값이 없으므로 allow값이 적용되게 된다.
order deny,allow로 설정한다면 denmy값이 적용된다.

-----------------------------------------------------------------------------------------------------

2-2. 서비스 재시작


[root@localhost html]# service httpd restart
httpd 를 정지 중:                                          [  OK  ]
httpd (을)를 시작 중:                                      [  OK  ]

-----------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------

3. 홈페이지 인증방식 설정


3-1. Apache  환경설정파일 설정

[root@localhost conf]# pwd
/etc/httpd/conf
[root@localhost conf]# vi httpd.conf
329 #   AllowOverride None      - 기본 설정
330      AllowOverride AuthConfig - 사용자 인증을 위한 설정

-----------------------------------------------------------------------------------------------------

3-2. 홈페이지 인증 설정

[root@localhost html]# pwd
/var/www/html
[root@localhost html]# ll
합계 4
-rw-r--r-- 1 root root 57 12월 13 21:48 index.html
[root@localhost html]# vi .htaccess
[root@localhost html]# cat .htaccess
AuthType Basic
AuthName "Here's bban2's Homepage!!"
AuthUserFile /etc/httpd/htpasswd
Require valid-user     - 모든 유저들이 들어갈수 있게 설정

#Require user admin  - Admin 만 들어갈수 있게 설정
[root@localhost html]# ls -al
합계 24
drwxr-xr-x 2 root root 4096 12월 13 23:57 .
drwxr-xr-x 8 root root 4096 11월  8 23:13 ..
-rw-r--r-- 1 root root  125 12월 13 23:57 .htaccess
-rw-r--r-- 1 root root   57 12월 13 21:48 index.html
[root@localhost html]# htpasswd -c /etc/httpd/htpasswd admin - admin으로 패스워드 설정, -c 옵션은 처음 생성할때만 사용
New password:
Re-type new password:
Adding password for user admin
[root@localhost html]# cat /etc/httpd/htpasswd
admin:2nGMXMue2QvAs   - 암호화된 패스워드

-----------------------------------------------------------------------------------------------------

3-3. htpasswd 파일 확인

[root@localhost httpd]# pwd
/etc/httpd
[root@localhost httpd]# ls -la
합계 56
drwxr-xr-x   4 root root  4096 12월 13 23:57 .
drwxr-xr-x 108 root root 12288 12월 13 22:34 ..
drwxr-xr-x   2 root root  4096 12월 13 23:52 conf
drwxr-xr-x   2 root root  4096 11월  8 23:13 conf.d
-rw-r--r--   1 root root    20 12월 13 23:57 htpasswd
lrwxrwxrwx   1 root root    19 11월  8 22:57 logs -> ../../var/log/httpd
lrwxrwxrwx   1 root root    27 11월  8 22:57 modules -> ../../usr/lib/httpd/modules
lrwxrwxrwx   1 root root    13 11월  8 22:57 run -> ../../var/run

-----------------------------------------------------------------------------------------------------

3-4. 서비스 재시작


[root@localhost html]# service httpd restart
httpd 를 정지 중:                                          [  OK  ]
httpd (을)를 시작 중:                                      [  OK  ]

-----------------------------------------------------------------------------------------------------

3-5. 웹브라우저에서 설정확인



-----------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------

4. 개인 사용자들 홈페이지에 패스워드 설정


4-1. Apache  환경설정파일 설정

[root@localhost conf]# pwd
/etc/httpd/conf
[root@localhost conf]# vi httpd.conf
377 <Directory /home/*/public_html>
378     AllowOverride AuthConfig
379 </Directory>

-----------------------------------------------------------------------------------------------------

4-2. test1 사용자로 접속

[root@localhost html]# su - test1  - test1 사용자로 전환한다.
[test1@localhost ~]$ cd /home/test1/public_html/ - 개인 사용자 홈페이지 디렉토리인 public_html 디렉토리로 이동한다.
[test1@localhost public_html]$ ll
합계 4
-rw-rw-r-- 1 test1 test1 54 12월 13 22:35 index.html
[test1@localhost public_html]$ vi .htaccess
[test1@localhost public_html]$ cat .htaccess
AuthType Basic
AuthName "Here's test1's Homepage!!"
AuthUserFile /etc/httpd/htpasswd
Require valid-user

-----------------------------------------------------------------------------------------------------

4-3. 서비스 재시작

[root@localhost html]# service httpd restart
httpd 를 정지 중:                                          [  OK  ]
httpd (을)를 시작 중:                                      [  OK  ]

-----------------------------------------------------------------------------------------------------

4-4. 웹브라우저에서 설정 확인



-----------------------------------------------------------------------------------------------------

- 참고
패스워드에 대한 권한은 관리자만 가지고 있으므로 패스워드를 공유해서 쓴다.
test1에 대한 패스워드를 따로 지정할 수도 있다.

4-5. test1의 패스워드 추가 설정

[root@localhost html]# htpasswd /etc/httpd/htpasswd test1 - test1의 패스워드 설정, -c 옵션은 처음 생성할때만 사용하므로 생략.
New password:
Re-type new password:
Adding password for user test1
[root@localhost html]# cat /etc/httpd/htpasswd
admin:2nGMXMue2QvAs
test1:89CV/ZuiZwxiI  - test1에 대한 passwd가 설정되었다.

-----------------------------------------------------------------------------------------------------

4-6. 서비스 재시작

[root@server3 conf]# service httpd restart
httpd 를 정지 중:                                          [  OK  ]
httpd (을)를 시작 중:                                      [  OK  ]

-----------------------------------------------------------------------------------------------------

4-7. 웹브라우저에서 확인


test1 

admin