웹사이트 검색

Fedora 31에서 PHP 및 MariaDB(LEMP 스택)로 Nginx를 설치하는 방법


이 튜토리얼은 다음 OS 버전에 대해 존재합니다.

  • 페도라 17

이 페이지에서

  1. 전제 조건
  2. 방화벽 구성\n
  3. PHP 설치
  4. MariaDB 설치
  5. Nginx 설치
  6. Nginx 구성
  7. PHP-FPM 구성
  8. 결론

LEMP 소프트웨어 스택은 서버가 웹사이트와 앱을 호스팅할 수 있도록 함께 설치되는 오픈 소스 소프트웨어 그룹입니다. Linux, ENginx 서버, MySQL(여기서는 MariaDB 사용) 및 PHP의 약어입니다.

이 가이드에서는 Fedora 31 기반 서버에 LEMP 스택을 설치합니다. 또한 PHPMyAdmin, Redis 및 Opcache도 설치합니다.

전제 조건

  • A server running Fedora 31.

  • A non-root sudo user.

  • Make sure everything is updated.

    $ sudo dnf upgrade
    
  • Few packages that your system needs.

    $ sudo dnf install wget curl nano -y
    

    Some of these packages may already be installed on your system.

  • Disable SELinux.

    $ sudo setenforce 0
    

방화벽 구성

첫 번째 단계는 방화벽을 구성하는 것입니다. Fedora 서버에는 Firewalld 방화벽이 미리 설치되어 있습니다.

방화벽이 실행 중인지 확인하십시오.

$ sudo firewall-cmd --state

다음 출력을 얻어야 합니다.

running

방화벽의 기본 영역을 공개로 설정합니다.

$ sudo firewall-cmd --set-default-zone=public

현재 허용된 서비스/포트를 확인하십시오.

$ sudo firewall-cmd --zone=public --permanent --list-services

다음 출력이 표시되어야 합니다.

dhcpv6-client mdns ssh

HTTP 및 HTTPS 포트를 허용합니다.

$ sudo firewall-cmd --zone=public --permanent --add-service=http
$ sudo firewall-cmd --zone=public --permanent --add-service=https

방화벽의 상태를 다시 확인하십시오.

$ sudo firewall-cmd --zone=public --permanent --list-services

비슷한 출력이 표시되어야 합니다.

dhcpv6-client http https mdns ssh

방화벽을 다시 로드합니다.

$ sudo systemctl reload firewalld

PHP 설치

Fedora 31은 기본적으로 PHP 7.3과 함께 제공됩니다. 그러나 REMI 저장소를 추가해야 하는 PHP 7.4를 설치하려고 합니다.

PHP 패키지 설치를 위한 공식 Fedora 리포지토리인 REMI 리포지토리를 설치합니다.

$ sudo dnf -y install https://rpms.remirepo.net/fedora/remi-release-31.rpm

remiremi-php74 리포지토리를 활성화하고 remi-modular 리포지토리를 비활성화합니다. 이렇게 하면 PHP 7.4 패키지를 설치하는 데 필요한 리포지토리가 활성화됩니다.

$ sudo dnf config-manager --set-enabled remi
$ sudo dnf config-manager --set-enabled remi-php74
$ sudo dnf config-manager --set-disabled remi-modular

일부 추가 패키지와 함께 PHP 7.4를 설치합니다.

$ sudo dnf install -y php-cli php-fpm php-mysqlnd

PHP가 올바르게 작동하는지 확인하십시오.

$ php --version

비슷한 출력이 표시되어야 합니다.

PHP 7.4.3 (cli) (built: Feb 18 2020 11:53:05) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

MariaDB 설치

MariaDB는 MySQL의 드롭인 대체품으로 MariaDB를 실행하고 작동하는 명령이 MySQL과 동일합니다.

Fedora 31은 기본적으로 MariaDB 10.3과 함께 제공되지만 최신 10.4(이 자습서 작성 시점에 사용 가능)도 설치할 수 있습니다. 이를 위해 공식 MariaDB 리포지토리를 추가해야 합니다.

/etc/yum.repos.d/ 디렉터리에 MariaDB.repo를 만듭니다.

$ sudo nano /etc/yum.repos.d/MariaDB.repo

그 안에 다음 코드를 추가합니다.

# MariaDB 10.4 Fedora repository list
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.4/fedora31-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

Ctrl + X를 누르고 메시지가 표시되면 Y를 입력하여 파일을 저장합니다.

MariaDB를 설치하려면 다음 명령을 실행하십시오.

$ sudo dnf install MariaDB-server -y

명령에 주목하십시오. Fedora 저장소에 사본(10.3)을 설치하려면 sudo dnf install mariadb-server를 사용해야 하지만 10.4 버전의 경우 sudo dnf install MariaDB-server<를 사용합니다. /코드>.

MariaDB가 올바르게 설치되었는지 확인하십시오.

$ mysql --version

다음 출력이 표시되어야 합니다.

mysql  Ver 15.1 Distrib 10.4.12-MariaDB, for Linux (x86_64) using  EditLine wrapper

MariaDB 서비스를 활성화하고 시작합니다.

$ sudo systemctl enable mariadb
$ sudo systemctl start mariadb

다음 명령을 실행하여 루트 암호 제공, 익명 사용자 제거, 원격 루트 로그인 금지 및 테스트 테이블 삭제와 같은 기본 구성을 수행합니다.

$ sudo mysql_secure_installation

MariaDB 10.4에서는 이제 루트 암호 또는 unix_socket 플러그인 사용 사이에 묻는 메시지가 표시됩니다. unix_socket 플러그인을 사용하면 Linux 사용자 자격 증명으로 MariaDB에 로그인할 수 있습니다. PhpMyAdmin과 같은 타사 앱을 사용하려면 기존 사용자 이름/암호가 필요하지만 더 안전한 것으로 간주됩니다. 이 튜토리얼에서는 unix_socket 플러그인을 계속 사용할 것입니다. 데이터베이스에 대해 생성한 특정 사용자를 통해 PhpMyAdmin을 계속 사용할 수 있습니다.

Enter 키를 누르면 기본 옵션(대문자로 표시된 옵션, 이 경우 Y)이 선택됩니다.

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none): [PRESS ENTER]
OK, successfully used password, moving on...

Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] [PRESS ENTER]
Enabled successfully!
Reloading privilege tables..
 ... Success!

You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n] [ANSWER n]

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] [PRESS ENTER]
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] [PRESS ENTER]
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] [PRESS ENTER]
 \- Dropping test database...
 ... Success!
 \- Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] [PRESS ENTER]
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

그게 다야. 다음에 MySQL에 로그인하려면 다음 명령을 사용하십시오.

$ sudo mysql

프롬프트가 표시되면 루트 비밀번호를 입력하십시오.

Nginx 설치

Fedora 31은 기본적으로 Nginxs 안정 버전(1.16.1)과 함께 제공됩니다. Nginx의 메인라인 버전을 설치하려면 소스에서 Nginx 빌드 가이드를 따르세요. 최신 버전의 Nginx와 튜토리얼에 언급된 종속성을 확보했는지 확인하세요. 나머지 지침은 동일하게 유지됩니다(./configure 명령 중에 그에 따라 경로 이름 변경). 이 튜토리얼에서는 안정적인 Nginx 버전을 사용합니다.

Nginx 서버를 설치합니다.

$ sudo dnf install nginx -y

제대로 작동하는지 확인하십시오.

$ nginx -v

다음 출력이 표시되어야 합니다.

nginx version: nginx/1.16.1

Nginx를 시작하고 활성화합니다.

$ sudo systemctl start nginx
$ sudo systemctl enable nginx

다음 페이지를 보려면 브라우저에서 서버 IP 주소를 여십시오. Nginx가 제대로 작동하고 있음을 의미합니다.

Nginx 구성

서버 블록이 위치할 디렉토리를 설정합니다.

$ sudo mkdir /etc/nginx/sites-available
$ sudo mkdir /etc/nginx/sites-enabled

사이트가 위치할 디렉토리를 생성합니다.

$ sudo mkdir /var/www/example.com/html -p

-p 지시문을 사용하면 이전에 존재하지 않았던 상위 디렉토리가 생성됩니다.

다음 명령을 실행하여 사이트에 대한 구성 파일을 추가하십시오.

$ sudo nano /etc/nginx/sites-available/example.com.conf

편집기에 다음 코드를 붙여넣습니다.

server {
  listen          *:80;
  server_name     example.com;
  root            /var/www/example.com/html;
  index           index.php index.html;

  location / {
    try_files   $uri $uri/ =404;
  }
    
  access_log /var/log/nginx/example.com.access.log;
  error_log /var/log/nginx/example.com.error.log;

  location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass  unix:/run/php-fpm/www.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_index index.php;
    include  fastcgi_params;
  }
}

이 파일은 /var/www/html 디렉토리에서 example.com을 호스팅한다고 가정합니다. 도메인을 사용하지 않고 IP 주소/로컬 호스트를 통해서만 서버에 액세스할 수 있도록 구성하려는 경우 nginx.conf 파일에서 해당 서버 블록 설정을 제거해야 합니다. 그렇지 않으면 엉망이 됩니다. 생성할 서버 블록으로.

sites-enabled 디렉터리에 연결하여 이 구성 파일을 활성화합니다.

$ sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/

편집을 위해 /etc/nginx/nginx.conf 파일을 엽니다.

$ sudo nano /etc/nginx/nginx.conf	

include /etc/nginx/conf.d/*.conf 줄 뒤에 다음 줄을 붙여넣습니다.

include /etc/nginx/sites-enabled/*.conf;
server_names_hash_bucket_size 64;

types_hash_max_size 값을 2048에서 4096으로 변경합니다.

types_hash_max_size 4096;

Ctrl + X를 눌러 편집기를 닫고 파일을 저장하라는 메시지가 표시되면 Y를 누릅니다. Nginx 구성을 테스트합니다.

$ sudo nginx -t

구성이 올바르다는 것을 나타내는 다음 출력이 표시되어야 합니다.

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Nginx 서비스를 다시 로드합니다.

$ sudo systemctl reload nginx

PHP-FPM 구성

/etc/php-fpm.d/www.conf 파일을 엽니다.

$ sudo nano /etc/php-fpm.d/www.conf

PHP 프로세스의 Unix 사용자/그룹을 nginx로 설정해야 합니다. 파일에서 user=apachegroup=apache 줄을 찾아 nginx로 변경합니다.

...
; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
; RPM: apache user chosen to provide access to the same directories as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx
...

또한 유닉스 소켓에 대한 소유권 권한을 nginx로 설정하고 그 앞에 있는 ;를 제거합니다.

listen.owner = nginx
listen.group = nginx

Ctrl + X를 누르고 메시지가 표시되면 Y를 입력하여 파일을 저장합니다.

PHP-fpm 프로세스를 다시 시작하십시오.

$ sudo systemctl restart php-fpm

PHP 설정을 테스트하려면 html 폴더에 test.php 파일을 만듭니다.

$ sudo nano /var/www/example.com/html/test.php

다음 콘텐츠를 추가하고 Ctrl + X를 누르고 메시지가 표시되면 Y를 입력하여 파일을 저장합니다.

<?php phpinfo();

웹 브라우저에서 http:///test.php를 실행하면 다음이 표시되어야 합니다.

결론

이것이 이 튜토리얼의 전부입니다. LEMP 설정이 완료되었으며 웹사이트와 애플리케이션을 만들고 호스팅할 수 있습니다.