IT 끄적이기

CentOS 7 nginx+php 설치와 SSL 적용까지

미르아 2019. 8. 28. 15:27
728x90

CentOS 7 nginx+php 설치부터 SSL 적용까지

 

그냥 nginx를 설치하면 구버전이 설치 되기 때문에 repo 등록 후 설치를 진행합니다.

 

# vi /etc/yum.repos.d/nginx.repo

 

입력해야 하는 내용은 아래와 같습니다.

 

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/운영체제/버전/$basearch/
gpgcheck=0
enabled=1

 

운영체제는 CentOS이며 버전은 7이기 때문에 아래와 같이 입력합니다.

 

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

 

이어서 nginx 설치를 진행합니다.

 

# yum install nginx

 

y를 입력하면 설치가 완료됩니다.

 

설치 진행 후 nginx 실행과 부팅시 자동실행 등록을 하도록 하겠습니다.

 

# systemctl start nginx
# systemctl enable nginx

 

포트를 등록해 줍니다.

 

-- CentOS 기본 방화벽 등록

추가 # firewall-cmd --permanent --zone=public --add-service=http 
제거 # firewall-cmd --permanent --zone=public --remove-service=http

추가 # firewall-cmd --permanent --zone=public --add-service=https
제거 # firewall-cmd --permanent --zone=public --remove-service=https

추가 # firewall-cmd -permanent -zone=public -add-port=80/tcp
제거 # firewall-cmd -permanent -zone=public -remove-port=80/tcp

적용 후
# systemctl stop firewalld
# systemctl mask firewalld

 

-- iptables 등록할 경우 아래 2줄 추가

# vi /etc/sysconfig/iptables

-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT

# systemctl restart iptables

 

이 후 해당 아이피로 들어가보면 초기 페이지가 뜨게 됩니다.

nginx 설치가 완료되었습니다.

 

도메인으로 접속할 수 있도록 하기 위한 작업을 하겠습니다.

 

vi /etc/nginx/conf.d/default.conf 파일을 열어 줍니다.

 

맨 밑부분에 해당 내용을 작성합니다.

 

server { 
listen       80; 
server_name  도메인 주소; 
location / { 
root   해당 경로; 
index  index.html index.htm index.php; 

    location ~ \.php$ 
    { 
    fastcgi_pass    127.0.0.1:9000; 
    fastcgi_index   index.php; 
    fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name; 
    include     fastcgi_params; 
    try_files $uri = 404; 
  } 
} 
}

 

 

설정을 하고 저장을 한 후 systemctl restart nginx를 입력해주세요

해당 도메인으로 연결된 화면입니다.

 

다음 단계로 php를 설치합니다.

 

PHP는 CentOS 기본 Repo에 포함되어 있지만, 버전이 낮기 때문에 Remi Repo 기반 설치를 추천합니다.

 

Remi Repo를 추가합니다.

 

# yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

 

버전 설정을 위해서 yum-utils을 설치합니다.

 

# yum install yum-utils

 

사용할 버전을 PHP 7.1로 설정합니다.

 

# yum-config-manager --enable remi-php71

 

PHP 는 Core와 확장 모듈 구조로 되어 있으며, Core와 필요한 확장 모듈을 추가하여 설치합니다.

 

# yum install php php-bcmath php-common php-dba php-fpm php-gd php-mbstring php-mysql php-pear php-xml 

 

PHP 설치가 완료되면 설정을 합니다.

 

PHP 설정 파일은 /etc/php.ini 에 있으며 /etc/php.d/ 를 include 하게 되어 있으므로, 사용자 설정 파일은 /etc/php.d/php.ini 에 작성합니다.

 

# vi /etc/php.d/php.ini

 

[PHP]

date.timezone = Asia/Seoul

 

추가로 보안을 위해서 다음과 같이 추가할 수 있습니다.

 

cgi.fix_pathinfo=0

open_basedir = /home

display_errors = Off

allow_url_fopen = Off

expose_php = Off

 

다음은 PHP의 Runtime user:group을 설정합니다.

www.conf 파일의 user, group 부분을 찾아서 다음과 같이 변경합니다.

 

#vi /etc/php-fpm.d/www.conf

 

user = nginx

group = nginx

 

NGINX의 Runtime user:group을 설정합니다.

nginx.conf 파일의 user 부분을 찾아서 다음과 같이 변경합니다.

 

# vi /etc/nginx/nginx.conf

 

user    nginx nginx;

 

PHP와 연동할 수 있도록 기본 설정을 작성합니다.

 

새로 conf 파일을 만든 경우는 밑 내용을 입력해주시면 됩니다.

 

# vi /etc/nginx/conf.d/USER.conf 

 

server { 
listen       80; 
server_name  도메인 주소; 
location / { 
root   해당 경로; 
index  index.html index.htm index.php; 

    location ~ \.php$ 
    { 
    fastcgi_pass    127.0.0.1:9000; 
    fastcgi_index   index.php; 
    fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name; 
    include     fastcgi_params; 
    try_files $uri = 404; 
  } 
} 
}

 

PHP 설치가 완료되고 PHP가 연결되어있는 도메인으로 들어갔는데 페이지가 나오지 않고 만약 다운이 된다!

그러한 현상이 나타나면

 

# vi /etc/nginx/nginx.conf

 

nginx.conf 파일에서 default_type 부분의 값을

 

default_type  application/octet-stream; -> default_type  text/html; 로 바꿔주시기 바랍니다.

 

이제 https 연결을 위한 SSL Let's Encrypt 설정을 진행합니다.

Let's Encrypt : cerbot-nginx 설치

 

# yum install certbot-nginx

 

y를 입력해 주면 설치 완료

 

# certbot --nginx -d [해당 도메인주소]

 

다음은 certbot --nginx -d [해당 도메인주소]를 입력합니다.

여러개의 web은 -d를 추가하여 추가하도록 합니다.

 

위에는 제가 인증서를 발급받을 도메인을 입력한 내용 입니다.

자신의 도메인 주소를 입력해 주시면 됩니다.

 

중간에 e-mail 주소를 물어보면 e-mail주소를 입력해주시고

A를 입력해주신 후 숫자를 입력하는 창이 나오면 2번을 입력해주세요.

 

인증서 발급이 완료 되었습니다.

 

# cd /etc/letsencrypt/live/해당 도메인/ 을 입력하셔서 폴더 안에 pem파일이 4개가 있는지 확인해주시기 바랍니다.

 

그 후 # openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048 을 입력합니다.

 

DH Param은 일부 암호화 알고리즘에 사용되는 커다란 난수 하나를 미리 생성해 두어서 암호화 성능을 향상 시킵니다.
Web Server 사양에 따라 시간이 오래 거릴수 있습니다.

 

nginx.conf default.conf설정

 

# vi /etc/nginx/nginx.conf 를 입력하여 nginx.conf 파일을 수정합니다.

 

ssl_dhparam /etc/ssl/certs/dhparam.pem; 를 적당한 곳에 넣어줍니다.

저는 빨간색으로 표시한 곳에 넣어놓았습니다.

 

 

이 작업이 완료되면 이제는 default.conf 파일에 해당 내용이 입력되어 있을 겁니다.

conf 파일을 새로 만들어서 하신 분들은 밑에 내용을 적당한 곳에 복사해서 붙여넣으세요.

 

# vi /etc/nginx/conf.d/default.conf 입력 후

밑에 적혀 있는 내용을 확인해주세요

 

listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/인증서 경로/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/인증서 경로/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;

 

if ($scheme != "https") {
return 301 https://$host#request_uri;
}

 

 

 

위 부분에서 빨간색이 쳐진 example.com을 위에서 확인한 pem파일이 있는 경로가 맞는지 확인해주세요

 

위에 4줄은 ssl 설정에 관한 내용이고 밑에 3줄은 http로 접속시 https로 리다이렉트 시켜주는 내용입니다.

 

ssl 설정이 끝났기 때문에 nginx를 재시작해 줍니다.

 

# systemctl restart nginx

 

Let's Encrypt 인증서는 무료이기 대문에 90일 마다 인증서 갱신을 해주어야 합니다.

 

갱신 인증방법은 메일이 오게 되면 cerbot renew를 입력해주면 된다고 합니다.

 

이상으로 nginx+php 설치와 SSL 적용까지하는 방법이었습니다.

728x90