IT 기술 문서

2025.02.01 17:25

CentOS 7 유용한 명령어 시리즈 - ⑮ Apache / Nginx 웹서버 관리

  • it2033 4일 전 2025.02.01 17:25
  • 10
    0

alt text


웹 서버를 운영할 때 가장 널리 사용되는 ApacheNginx는 CentOS 7에서도 많이 활용됩니다. 이번 글에서는 Apache와 Nginx의 설치, 설정, 로그 관리, 문제 해결 명령어들을 정리하겠습니다.


1. Apache (httpd) 웹 서버 관리

1.1. Apache 설치

yum install httpd -y
  • Apache 웹 서버(httpd)를 설치합니다.

1.2. Apache 서비스 시작

systemctl start httpd
  • Apache 서비스를 실행합니다.

1.3. 부팅 시 Apache 자동 실행 설정

systemctl enable httpd
  • 서버 재부팅 시 Apache가 자동으로 실행되도록 설정합니다.

1.4. Apache 상태 확인

systemctl status httpd
  • Apache 서비스가 정상적으로 실행 중인지 확인합니다.

1.5. Apache 서비스 재시작

systemctl restart httpd
  • Apache 서비스를 다시 시작합니다.

1.6. Apache 포트 열기 (방화벽 설정)

firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
  • HTTP(80번 포트) 및 HTTPS(443번 포트)를 방화벽에서 허용합니다.


2. Nginx 웹 서버 관리

2.1. Nginx 설치

yum install epel-release -y
yum install nginx -y
  • EPEL 리포지토리를 추가한 후 Nginx를 설치합니다.

2.2. Nginx 서비스 시작

systemctl start nginx
  • Nginx 서비스를 실행합니다.

2.3. 부팅 시 Nginx 자동 실행 설정

systemctl enable nginx
  • 서버 부팅 시 Nginx가 자동으로 실행되도록 설정합니다.

2.4. Nginx 상태 확인

systemctl status nginx
  • Nginx 서비스가 정상적으로 실행 중인지 확인합니다.

2.5. Nginx 서비스 재시작

systemctl restart nginx
  • Nginx 서비스를 다시 시작합니다.

2.6. Nginx 포트 열기 (방화벽 설정)

firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
  • HTTP(80번 포트) 및 HTTPS(443번 포트)를 방화벽에서 허용합니다.


3. 웹 서버 포트 및 프로세스 확인

3.1. 현재 웹 서버가 사용하는 포트 확인

netstat -tulnp | grep -E 'httpd|nginx'

또는

ss -tulnp | grep -E 'httpd|nginx'
  • 현재 실행 중인 웹 서버 프로세스와 포트를 확인합니다.

출력 예시:

tcp   LISTEN   0   128   0.0.0.0:80   0.0.0.0:*   users:(("httpd",pid=1234,fd=3))
tcp   LISTEN   0   128   0.0.0.0:443  0.0.0.0:*   users:(("nginx",pid=5678,fd=6))
  • Apache가 80번 포트를 사용하고, Nginx가 443번 포트를 사용하고 있습니다.

3.2. 웹 서버 프로세스 확인

ps aux | grep -E 'httpd|nginx'
  • 실행 중인 Apache(httpd) 및 Nginx 프로세스를 확인합니다.


4. 웹 서버 로그 관리

4.1. Apache 로그 확인

tail -f /var/log/httpd/access_log
tail -f /var/log/httpd/error_log
  • Apache의 접속 로그(access_log)에러 로그(error_log) 를 실시간으로 확인합니다.

4.2. Nginx 로그 확인

tail -f /var/log/nginx/access.log
tail -f /var/log/nginx/error.log
  • Nginx의 접속 로그(access.log)에러 로그(error.log) 를 실시간으로 확인합니다.

4.3. 특정 IP의 접속 로그 필터링

grep "192.168.1.100" /var/log/httpd/access_log
  • 특정 IP(예: 192.168.1.100)의 접속 기록을 필터링합니다.


5. Apache 및 Nginx 설정 파일 관리

5.1. Apache 주요 설정 파일

nano /etc/httpd/conf/httpd.conf
  • Apache의 메인 설정 파일을 편집합니다.

5.2. Nginx 주요 설정 파일

nano /etc/nginx/nginx.conf
  • Nginx의 메인 설정 파일을 편집합니다.

5.3. Apache 가상 호스트 설정

nano /etc/httpd/conf.d/mysite.conf

예제 설정:

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/example
    ErrorLog /var/log/httpd/example_error.log
    CustomLog /var/log/httpd/example_access.log combined
</VirtualHost>
  • example.com에 대한 가상 호스트를 설정합니다.

5.4. Nginx 가상 호스트 설정

nano /etc/nginx/conf.d/mysite.conf

예제 설정:

server {
    listen 80;
    server_name example.com;
    root /var/www/example;
    index index.html;
    access_log /var/log/nginx/example_access.log;
    error_log /var/log/nginx/example_error.log;
}
  • example.com에 대한 Nginx 가상 호스트를 설정합니다.

5.5. 설정 변경 후 Apache/Nginx 재시작

systemctl restart httpd   # Apache
systemctl restart nginx   # Nginx

5.6. 설정 파일 문법 오류 검사

apachectl configtest  # Apache 설정 검사
nginx -t             # Nginx 설정 검사
  • 설정 변경 후 문법 오류가 없는지 검사합니다.


6. 웹 서버 문제 해결

6.1. 서비스가 실행되지 않을 때 로그 확인

journalctl -xe | grep httpd
journalctl -xe | grep nginx
  • Apache 또는 Nginx의 실행 오류 로그를 확인합니다.

6.2. 특정 포트가 이미 사용 중인지 확인

netstat -tulnp | grep :80
  • 80번 포트를 사용하는 프로세스를 확인하고 충돌 여부를 파악합니다.

6.3. 웹 서버 프로세스 강제 종료

killall -9 httpd
killall -9 nginx
  • Apache(httpd) 또는 Nginx 프로세스를 강제로 종료한 후 다시 시작합니다.


정리

명령어

설명

yum install httpd -y

Apache 설치

yum install nginx -y

Nginx 설치

systemctl start httpd

Apache 시작

systemctl start nginx

Nginx 시작

systemctl enable httpd

Apache 자동 실행 설정

systemctl enable nginx

Nginx 자동 실행 설정

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

방화벽에서 HTTP 포트 열기

apachectl configtest

Apache 설정 문법 검사

nginx -t

Nginx 설정 문법 검사

tail -f /var/log/httpd/access_log

Apache 접속 로그 확인

tail -f /var/log/nginx/access.log

Nginx 접속 로그 확인

이번에는 Apache / Nginx 웹 서버 관리 명령어를 정리했습니다. 다음 시리즈에서는 파일 압축 및 백업 관리 명령어를 다루겠습니다.

  • 공유링크 복사