'궁금한 옵션만 검색하세요'에 해당되는 글 1건

  1. 2019.03.17 Apache 설정법 간단정리

Apache 서버 사용법에 대해 정리합니다.

인터넷 강의를 정리한거라 범위가 제한적일수도 있습니다.

초보 입장에서 보시고 빠진건 의견 주시면 감사하겠습니다!


1. 아파치 서버 배경지식


웹서버 히스토리

- 연구원 Tim burners Lee가 hypertext 기반 정보를 연구원들 사이어서 공유 & 업데이트 하기 위한 수단을 연구하는 프로젝트를 시작으로 네트워크와 웹 환경에 대한 연구를 진행함

- hypertext idea with TCP and DNS creates the WWW (1989)

- HTML, URI, HTTP : 웹 서버를 이해하기 위한 개념 3대장


Software for Web Server (다음의 요구사항을 만족시켜야 함)

- Web Server: HTTP Server -> Application Server

  - using HTTP Protocol

  - Static content, Dynamic content

  - Must implement the HTTP Protocol

  - example) Apache, Nginx, IIS and others


HTTP는 제일 단순하게 이렇게 작동한다.

Client ---- (HTTP Request)  ---> Web Server

            /index.html 보여줘

       <--- (HTTP Response) ----

            여기 있음, HTML 형식이고 길이는 ...


2. 아파치 구조 파악하기


(1) 아파치 프로세스 제어


Apache enable automatically (시스템 설정)

$ systemctl enable httpd

$ systemctl status httpd


Firewall http and https on (CentOS 7의 방법이므로 OS마다 다시 찾아서 적용해야 함)

$ firewall-cmd --permanent --add-service=http

$ firewall-cmd --permanent --add-service=https

$ firewall-cmd --reload


Find Apache location

$ whereis httpd


Help Apache execution

$ httpd -h


Show Loaded Apache Modules

$ httpd -M


Show Compiled Apache Modules

$ httpd -l


Check if config file location is vaild

$ httpd -t -f /etc/httpd/conf/httpd.conf


All Apache Module's location

$ cd /usr/lib64/httpd/modules


(2) Apache's main location (아파치 프로그램 구성)

$ cd /etc/httpd

- conf    : configuration file's directory

- conf.d  : custom configuration file's directory

- logs    : Apache log directory

- modules : Apache module's location (linked to /usr/lib64/httpd/modules)


3. Apache Configuration

(1) 설정 파일 작성 기초

한줄로 선언하는 설정 명령

+ ServerRoot                 : Apache's root directory

+ Listen                     : Port(+IP Addr) that Apache listen

+ Include *.conf             : 추가 설절파일 포함하여 작동, 설정파일이 존재하지 않으면 에러

+ User/Group                 : OS에서 아파치가 실행되는 계정명

+ ServerAdmin                : 관리자 이메일

+ ServerName                 : 웹 서버의 도메인 이름

+ DocumentRoot               : 웹 서버에서 사용할 resource 의 root 경로

+ ErrorLog                   : 아파치 에러로그 파일의 위치

+ LogLevel                   : 로그 출력 수준 (debug, info, notice, warn, error, crit, alert, emerg)

+ IncludeOptional            : 명시한 추가 설정파일이 실제로 있을 경우에만 포함시켜 작동함


Tag로 선언하는 설정 명령

+ <Directory addr>            : 디렉토리에 대한 접근 설정   *root에 대한 접근은 기본적으로 막혀있다.

  - AllowOverride

  - Require

  - Options

+ <IfModule mod>              : 지정한 모듈이 존재할 경우 추가 설정 정의

+ <File file>                 : 파일에 대한 접근 설정   *보통은 .htaccess와 .htpasswd 파일이 웹에 공유되는 것을 막기 위해 기본 설정 되어 있음


(2) Virtual Host Setting

name based

<VirtualHost *:80>
  DocumentRoot  "/var/www/akademia"
  ServerName  www.akademia.com
</VirtualHost>

port based

* 방화벽 포트 오픈 설정
* conf 생성
Listen 8080

<VirtualHost *:8080>
  DocumentRoot  "/var/www/akademia"
  ServerName  web.akademia.com
</VirtualHost>

Alias Setting (한줄 추가)

Alias /img /var/www/html/images

Redirect Setting (한줄 추가)

Redirect [permanent|temp] /program.html /download.html

Custom Log

https://httpd.apache.org/docs/2.2/ko/mod/mod_log_config.html 참고, 그만큼 옵션이 다양함


(3) Security Basic Settings

httpd.conf

ServerTokens OS->Prod 
ServerSignature On->Off
FileETag None
TraceEnable off

custom conf

Options -Indexes

apache 서비스 계정에 권한

$ sudo usermod -g apache apache
$ sudo chown -R apache.apache /etc/httpd/
$ ls -ld /etc/httpd/
$ sudo systemctl restart httpd

root 외에 설정 파일 접근 차단

$ ps -ef | grep httpd
$ sudo chmod 750 /etc/httpd/conf
$ sudo chmod 750 /etc/httpd/conf.d

Set up TLS/SSL for a free (도메인이 있어야 가능)

$ sudo yum install epel-release
$ sudo yum install mod_ssl python-certbot-apache
$ firewall-cmd --permanent --add-service=https
$ firewall-cmd --reload
$ certbot --apache -d <도메인 명>
$ certbot --renew   # scheduler 설정

Basic Authentication Setting

# custom 설정에 추가
<Directory "/var/www/akademia/admin">
  AuthType Basic
  AuthName "For Authenticated Users"
  AuthUserFile /etc/httpd/password/passwords_file
  Require valid-user
</Directory>

$ httpd -t
$ sudo systemctl restart httpd

(2) passwords_file 생성
$ sudo mkdir /etc/httpd/password
$ sudo htpasswd -c /etc/httpd/password/passwords_file user1

Digest Authentication Setting

# custom 설정에 추가
<Directory "/var/www/akademia/mod">
  AuthType Digest
  AuthName "Private"
  AuthUserFile /etc/httpd/password/digest
  Require valid-user
</Directory>

(2) passwords_file 생성
$ sudo htdigest -c /etc/httpd/password/digest "Private" user3

Access Control

(참고: https://httpd.apache.org/docs/2.4/mod/mod_authz_host.html)
(1) Directory 태그 안에 추가
<RequireAll>
  Require all granted
  Require [not] ip XX.XX.XX.XX
</RequireAll>

.htaccess 설정

(1) Directory 태그 안에 추가
AllowOverride AuthConfig #추가

(2) 지정 위치에 .htaccess 파일 작성
AuthType Digest
AuthName "Private"
AuthUserFile /etc/httpd/password/digest
Require valid-user

웹 서버 anti-virus 설치

$ yum -y install epel-release
$ yum -y install clamav-server clamav-data clamav-update clamav-filesystem clamav clamav-scanner-systemd clamav-devel clamav-lib clamav-server-systemd
(selinux 작동 되어있어야 함)
$ setsebool -P antivirus_can_scan_system 1
$ setsebool -P clamd_use_jit 1
$ vi /etc/clamd.d/scan.conf
$ vi /etc/freshclam.conf
$ freshclam
$ clamscan -r [--(remove|move)] /var/www/akademia/

DoS Attack 방지

$ yum -y install mod_evasive
$ yum -y install mailx
$ vi /etc/httpd/conf.d/mod_evasive.conf
$ tail -f /var/log/messages

4. 기타

(1) Apache MPM (Multi-Processing Modules)

- The worker MPM : multiple child processes * many threads each, Each thread handles one connection at a time
(generally good for high-traffic servers)
- The event MPM : worker with main threads, designed to allow more requests to be served simultaneously by passing off some processing work to supporting threads
- The prefork MPM : multiple child processes * 1 thread each, Each process handles one connection at a time.
(it can be used with non-thread-safe third-party modules, and it is easier to debug on platforms with poor thread debugging support.)

(2) Adjusting httpd.conf (커넥션 관련 옵션)

- Timeout               : sets the number of seconds before data sends to or receives from the client timeout
- KeepAlive             : enables persistent connections on the web server
- KeepAliveTimeout      : the number of seconds Apache wait for a subsequent request before closing the connection
- MaxKeepAliveRequests  : limits the number of requests allowed per connection when KeepAlive is on.


Posted by kevin.jeong.
,