웹사이트 검색

Nginx로 Drupal 8 설치 및 구성 및 CentOS 8에서 암호화 허용


이 페이지에서

  1. 요구 사항\n
  2. Nginx, MariaDB 및 PHP 설치
  3. 데이터베이스 구성
  4. 드루팔 다운로드
  5. Drupal용 Nginx 구성
  6. SELinux 및 방화벽 구성\n
  7. Lets Encrypt SSL로 Drupal 보안 유지
  8. Drupal 웹사이트 액세스

Drupal은 개인이 모든 유형의 웹 사이트를 만들고 관리하는 데 사용할 수 있는 확장 가능한 무료 오픈 소스 콘텐츠 관리 시스템입니다. PHP로 작성되었으며 MySQL/MariaDB를 사용하여 데이터를 저장합니다. Drupal은 수천 개의 추가 기능으로 확장할 수 있는 풍부한 기능 세트를 제공합니다. Drupal은 Apache, Nginx, IIS, Lighttpd 및 데이터베이스 MySQL, MariaDB, MongoDB, SQLite, PostgreSQL 및 MS SQL 서버를 포함한 많은 웹 서버를 지원합니다. Drupal은 코딩 지식 없이도 웹 사이트를 만들 수 있는 간단하고 사용자 친화적인 웹 UI와 함께 제공됩니다.

이 튜토리얼에서는 CentOS 8 서버에 Drupal 8을 설치하고 무료 SSL을 Lets Encrypt로 보호하는 방법을 보여줍니다.

요구 사항

  • CentOS 8을 실행하는 서버.\n
  • 서버 IP를 가리키는 유효한 도메인 이름\n
  • 루트 암호는 서버에서 구성됩니다.\n

Nginx, MariaDB 및 PHP 설치

시작하기 전에 서버에 LEMP 서버를 설치해야 합니다. 다음 명령을 실행하여 설치할 수 있습니다.

dnf install nginx mariadb-server php php-fpm php-cli php-mbstring php-gd php-xml php-curl php-mysqlnd php-pdo php-json php-opcache -y

설치가 완료되면 Nginx, MariaDB 및 php-fpm 서비스를 시작하고 다음 명령을 사용하여 시스템 재부팅 후 시작할 수 있도록 활성화합니다.

systemctl start nginx
systemctl start php-fpm
systemctl start mariadb
systemctl enable nginx
systemctl enable php-fpm
systemctl enable mariadb

데이터베이스 구성

기본적으로 MariaDB는 보안되지 않으므로 보안을 설정해야 합니다. 다음 명령을 실행하여 보안을 설정할 수 있습니다.

mysql_secure_installation

아래와 같이 모든 질문에 답하십시오.

Enter current password for root (enter for none):
Set root password? [Y/n] Y
New password:
Re-enter new password:
Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] Y
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y

완료되면 다음 명령을 사용하여 MariaDB 셸에 로그인합니다.

mysql -u root -p

프롬프트가 표시되면 루트 암호를 제공한 후 다음 명령을 사용하여 Drupal용 데이터베이스 및 사용자를 생성합니다.

MariaDB [(none)]> CREATE DATABASE drupaldb CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
MariaDB [(none)]> CREATE USER IDENTIFIED BY "password";

그런 다음 다음 명령을 사용하여 drupaldb에 모든 권한을 부여합니다.

MariaDB [(none)]> GRANT ALL ON drupaldb.* TO  IDENTIFIED BY "password";

그런 다음 권한을 플러시하고 다음 명령을 사용하여 MariaDB 셸을 종료합니다.

MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> EXIT;

드루팔 다운로드

먼저 공식 웹 사이트에서 최신 버전의 Drupal을 다운로드해야 합니다. 다음 명령으로 다운로드할 수 있습니다.

wget https://ftp.drupal.org/files/projects/drupal-8.7.10.tar.gz

다운로드가 완료되면 다음 명령을 사용하여 다운로드한 파일의 압축을 풉니다.

tar -xvzf drupal-8.7.10.tar.gz

다음으로 다음 명령을 사용하여 추출된 디렉터리를 Nginx 웹 루트 디렉터리로 이동합니다.

mv drupal-8.7.10 /var/www/html/drupal

다음으로 웹 사이트 파일을 저장할 디렉토리를 만들고 아래와 같이 default.settings.php 파일의 이름을 바꿉니다.

mkdir /var/www/html/drupal/sites/default/files
cp /var/www/html/drupal/sites/default/default.settings.php /var/www/html/drupal/sites/default/settings.php

다음으로 아래와 같이 Drupal 디렉토리의 소유권을 nginx로 변경합니다.

chown -R nginx:nginx /var/www/html/drupal/

Drupal용 Nginx 구성

먼저 다음 명령을 사용하여 Drupal용 php-fpm 구성 파일을 만듭니다.

nano /etc/php-fpm.d/drupal.conf

다음 줄을 추가합니다.

[drupal]
user = nginx
group = nginx
listen.owner = nginx
listen.group = nginx
listen = /run/php-fpm/drupal.sock
pm = ondemand
pm.max_children =  50
pm.process_idle_timeout = 10s
pm.max_requests = 500
chdir = /

완료되면 파일을 저장하고 닫습니다. 그런 다음 Drupal용 Nginx 가상 호스트 구성 파일을 만듭니다.

nano /etc/nginx/conf.d/drupal.conf

다음 줄을 추가합니다.

server {
    listen 80;
    server_name example.com;

    root /var/www/html/drupal;

    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    location ~ \..*/.*\.php$ {
        return 403;
    }

    location ~ ^/sites/.*/private/ {
        return 403;
    }

    # Block access to scripts in site files directory
    location ~ ^/sites/[^/]+/files/.*\.php$ {
        deny all;
    }
    location ~ (^|/)\. {
        return 403;
    }

    location / {
        try_files $uri /index.php?$query_string;
    }

    location @rewrite {
        rewrite ^/(.*)$ /index.php?q=$1;
    }
    location ~ /vendor/.*\.php$ {
        deny all;
        return 404;
    }


    location ~ '\.php$|^/update.php' {
        fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
        include fastcgi_params;
       	# Block httpoxy attacks. See https://httpoxy.org/.
        fastcgi_param HTTP_PROXY "";
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_intercept_errors on;
        fastcgi_pass unix:/run/php-fpm/drupal.sock;
    }
    location ~ ^/sites/.*/files/styles/ { # For Drupal >= 7
        try_files $uri @rewrite;
    }

    # Handle private files through Drupal. Private file's path can come
    # with a language prefix.
    location ~ ^(/[a-z\-]+)?/system/files/ { # For Drupal >= 7
        try_files $uri /index.php?$query_string;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
        try_files $uri @rewrite;
        expires max;
        log_not_found off;
    }
}

파일을 저장하고 닫습니다. 그런 다음 php-fpm 및 Nginx 서비스를 다시 시작하여 변경 사항을 적용합니다.

systemctl restart php-fpm
systemctl restart nginx

SELinux 및 방화벽 구성

기본적으로 SELinux는 CentOS 8에서 활성화되어 있습니다. 따라서 Drupal이 제대로 작동하려면 SELinux를 구성해야 합니다.

먼저 다음 명령을 사용하여 Drupal이 공용 및 개인 파일 디렉토리에 쓸 수 있도록 허용합니다.

semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/html/drupal(/.*)?"
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/html/drupal/sites/default/settings.php'
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/html/drupal/sites/default/files'
restorecon -Rv /var/www/html/drupal
restorecon -v /var/www/html/drupal/sites/default/settings.php
restorecon -Rv /var/www/html/drupal/sites/default/files

다음으로 Drupal이 다음 명령을 사용하여 아웃바운드 이메일을 보낼 수 있도록 허용합니다.

setsebool -P httpd_can_sendmail on

다음으로 외부 네트워크에서 HTTP 및 HTTPS 서비스를 허용하는 방화벽 규칙을 생성해야 합니다. 다음 명령으로 허용할 수 있습니다.

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

Lets Encrypt SSL로 보안 Drupal

이제 Drupal이 설치 및 구성되었습니다. 무료 SSL을 Lets Encrypt로 보호할 때입니다.

이렇게 하려면 서버에서 certbot 클라이언트를 다운로드해야 합니다. 다음 명령을 실행하여 올바른 권한을 다운로드하고 설정할 수 있습니다.

wget https://dl.eff.org/certbot-auto
mv certbot-auto /usr/local/bin/certbot-auto
chown root /usr/local/bin/certbot-auto
chmod 0755 /usr/local/bin/certbot-auto

이제 다음 명령을 실행하여 Drupal 웹 사이트에 대한 SSL 인증서를 얻고 설치하십시오.

certbot-auto --nginx -d example.com

위의 명령은 먼저 서버에 필요한 모든 종속성을 설치합니다. 설치가 완료되면 아래와 같이 이메일 주소를 제공하고 서비스 약관에 동의하라는 메시지가 표시됩니다.

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator apache, Installer apache
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): 

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y 


Obtaining a new certificate
Performing the following challenges:
http-01 challenge for example.com
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/conf.d/drupal.conf

다음으로 아래와 같이 HTTP 트래픽을 HTTPS로 리디렉션할지 여부를 선택해야 합니다.

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2

2를 입력하고 Enter를 눌러 계속하십시오. 설치가 완료되면 다음 출력이 표시됩니다.

Redirecting all traffic on port 80 to ssl in /etc/nginx/conf.d/drupal.conf

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://example.com

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=example.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/example.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/example.com/privkey.pem
   Your cert will expire on 2020-03-23. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot-auto
   again with the "certonly" option. To non-interactively renew *all*
   of your certificates, run "certbot-auto renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

Drupal 웹사이트에 액세스

이제 웹 브라우저를 열고 URL https://example.com을 입력하십시오. 다음 페이지로 리디렉션됩니다.

원하는 언어를 선택하고 저장하고 계속하기 버튼을 클릭합니다. 다음 페이지가 표시됩니다.

설치 프로필을 선택하고 저장하고 계속하기 버튼을 클릭합니다. 다음 페이지가 표시됩니다.

데이터베이스 세부 정보를 제공하고 저장하고 계속하기 버튼을 클릭합니다. 다음 페이지가 표시됩니다.

사이트 이름, 관리자 사용자 이름, 비밀번호를 입력하고 저장하고 계속하기 버튼을 클릭합니다. 다음 페이지에서 Drupal 대시보드를 볼 수 있습니다.

축하합니다! CentOS 8 서버에 Drupal을 성공적으로 설치하고 보호했습니다.