서버 설치 · 12 min read · Oct 13, 2025
Nginx, PHP 및 MariaDB (LEMP 스택) 설치 방법: Opcache, Redis 및 Let's Encrypt를 Ubuntu에 설치하기

“LEMP”라는 약어는 서버가 동적 웹사이트 및 웹 애플리케이션을 호스팅할 수 있도록 함께 설치되는 소프트웨어 그룹을 나타냅니다. 이 용어는 실제로 네 가지 주요 구성 요소를 나타내는 약어입니다:
- Linux: 운영 체제. Linux는 서버의 기초가 되는 인기 있는 오픈 소스 운영 체제입니다.
- Engine-X (발음: “nginx”): 웹 서버. Nginx는 안정성, 풍부한 기능 세트, 간단한 구성 및 낮은 자원 소비로 알려진 고성능 웹 서버입니다.
- MySQL 또는 MariaDB: 데이터베이스 시스템. MySQL은 웹사이트나 애플리케이션의 데이터를 저장하고 관리하는 널리 사용되는 관계형 데이터베이스 관리 시스템입니다. MySQL은 때때로 MariaDB로 대체되며, 이는 MySQL의 향상된 완전 오픈 소스 커뮤니티 개발 포크입니다.
- PHP: 프로그래밍 언어. PHP는 웹 개발을 위해 설계된 서버 측 스크립팅 언어이지만 일반 목적의 프로그래밍 언어로도 사용됩니다.
LEMP 스택은 데이터베이스 및 서버 측 처리가 필요한 웹사이트를 호스팅하는 데 인기 있는 선택입니다. 예를 들어 WordPress 웹사이트, 전자상거래 플랫폼 및 기타 동적 웹 애플리케이션이 있습니다. 이는 Apache(LEMP의 “A”로 나타남)가 Nginx 대신 사용되는 LAMP 스택과 비교할 수 있습니다.
이 가이드에서는 Ubuntu 20.04 기반 서버에 LEMP 스택을 설치합니다. 또한 phpMyAdmin, Redis, Opcache 및 Let’s Encrypt SSL을 설치합니다.
전제 조건
- Ubuntu 20.04를 실행하는 서버.
- 비 루트 sudo 사용자.
- 모든 것이 업데이트되었는지 확인하십시오.
shell $ sudo apt update $ sudo apt upgrade - 시스템에 필요한 몇 가지 패키지.
shell $ sudo apt install wget curl nano -y이러한 패키지 중 일부는 이미 시스템에 설치되어 있을 수 있습니다.
방화벽 구성
첫 번째 단계는 방화벽을 구성하는 것입니다. Ubuntu는 기본적으로 ufw(간단한 방화벽)를 제공합니다.
방화벽이 실행 중인지 확인하십시오.
$ sudo ufw status
다음과 같은 출력을 받아야 합니다.
Status: inactive
SSH 포트를 허용하여 방화벽이 활성화될 때 현재 연결이 끊어지지 않도록 합니다.
$ sudo ufw allow OpenSSH
HTTP 및 HTTPS 포트도 허용합니다.
$ sudo ufw allow 80
$ sudo ufw allow 443
방화벽을 활성화합니다.
$ sudo ufw enable
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup
방화벽 상태를 다시 확인하십시오.
$ sudo ufw status
유사한 출력을 볼 수 있어야 합니다.
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
80 ALLOW Anywhere
443 ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
80 (v6) ALLOW Anywhere (v6)
443 (v6) ALLOW Anywhere (v6)
PHP 설치
Ubuntu 20.04는 기본적으로 PHP 7.4를 제공하지만 업데이트된 PHP 저장소를 사용하기 위해 Ondrej의 PHP 저장소를 추가합니다.
Ondrej의 PHP 저장소를 설치합니다.
$ sudo add-apt-repository ppa:ondrej/php
PHP 7.4와 몇 가지 추가 패키지를 설치합니다.
$ sudo apt install php-cli php-fpm php-mysql -y
PHP가 제대로 작동하는지 확인합니다.
$ php --version
다음과 유사한 출력을 볼 수 있어야 합니다.
PHP 7.4.5 (cli) (built: Apr 28 2020 14:49:23) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.5, Copyright (c), by Zend Technologies
MariaDB 설치
MariaDB는 MySQL의 드롭인 대체품으로, MariaDB를 실행하고 운영하는 명령은 MySQL과 동일합니다.
Ubuntu 20.04는 기본적으로 약간 구식인 MariaDB 10.3을 제공합니다. 최신 안정 버전의 MariaDB를 얻기 위해 공식 저장소를 설치합니다.
MariaDB의 공식 저장소를 추가합니다.
$ sudo apt-key adv --fetch-keys 'https://mariadb.org/mariadb_release_signing_key.asc'
$ sudo add-apt-repository 'deb [arch=amd64] http://mirror.lstn.net/mariadb/repo/10.4/ubuntu focal main'
MariaDB를 설치하려면 다음 명령을 실행합니다.
$ sudo apt install mariadb-server -y
MariaDB가 올바르게 설치되었는지 확인합니다.
$ mysql --version
다음과 같은 출력을 볼 수 있어야 합니다.
mysql Ver 15.1 Distrib 10.4.13-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2
MariaDB 서비스를 활성화합니다.
$ sudo systemctl enable mariadb
루트 비밀번호 설정, 익명 사용자 제거, 원격 루트 로그인 금지 및 테스트 테이블 삭제와 같은 기본 구성을 수행하려면 다음 명령을 실행합니다.
$ sudo mysql_secure_installation
MariaDB 10.4에서는 루트 비밀번호 사용 또는 unix_socket 플러그인 사용 중에서 선택하라는 메시지가 표시됩니다. unix_socket 플러그인은 Linux 사용자 자격 증명을 사용하여 MariaDB에 로그인할 수 있도록 합니다. 이는 더 안전하다고 여겨지지만 phpMyAdmin과 같은 3자 앱을 사용하려면 전통적인 사용자 이름/비밀번호가 필요합니다. 이 튜토리얼에서는 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]
... skipping.
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
프롬프트가 표시되면 루트 비밀번호를 입력하십시오.
Redis 설치
다음 명령을 사용하여 Redis 및 해당 PHP Redis 확장을 설치합니다.
$ sudo apt install redis php-redis
Redis 서버 구성
Redis 서버에 대한 기본 구성을 수행해 보겠습니다.
Nano 편집기로 /etc/redis/redis.conf 파일을 엽니다.
$ sudo nano /etc/redis/redis.conf
파일 내에서 supervised 지시어를 찾습니다. 이 지시어는 Redis를 서비스로 관리할 init 시스템을 선언할 수 있도록 합니다. 기본값은 no로 설정되어 있습니다. systemd init 시스템을 사용하는 Ubuntu를 사용하고 있으므로 값을 no에서 systemd로 변경합니다.
supervised systemd
원격 클라이언트가 Redis 인스턴스에 연결할 수 있도록 하려면 bind 127.0.0.1 줄을 찾아 다음과 같이 변경합니다.
bind 0.0.0.0
Redis가 수신 대기하는 기본 포트를 6379에서 원하는 값으로 변경할 수도 있습니다.
port 3458
Redis를 캐시 서버로 구성하려면 다음 값을 설정합니다.
maxmemory 256mb
maxmemory-policy allkeys-lru
이는 Redis가 최대 메모리 256MB에 도달했을 때 LRU 알고리즘을 사용하여 키를 제거하도록 지시합니다. 메모리 값은 요구 사항 및 사용하는 서버에 따라 설정할 수 있습니다.
Redis를 사용해야 하는 클라이언트가 먼저 인증을 요구하도록 비밀번호를 설정할 수 있습니다. 그렇게 하려면 다음 지시어를 사용하여 비밀번호를 설정합니다.
requirepass
구성 파일에서 변경할 수 있는 더 많은 지시어를 찾을 수 있습니다. 완료되면 Ctrl + X를 누르고 파일을 저장하라는 메시지가 표시되면 Y를 입력합니다.
변경 사항을 적용하기 위해 Redis 서버를 재시작합니다.
$ sudo systemctl restart redis
원격 클라이언트가 연결하려면 방화벽에 규칙을 추가해야 합니다. 그렇지 않으면 이 단계를 건너뛸 수 있습니다.
$ sudo ufw allow 6379/tcp
위 명령의 값을 구성 파일에서 선택한 포트와 일치하도록 변경해야 합니다.
Nginx 설치
Ubuntu 20.04는 기본적으로 Nginx의 최신 메인라인 버전(1.17.10)을 제공합니다. 그러나 Nginx의 공식 안정 저장소를 사용하도록 전환하겠습니다.
먼저 몇 가지 전제 조건을 설치합니다.
$ sudo apt install curl gnupg2 ca-certificates lsb-release
이 중 일부는 이미 시스템에 설치되어 있습니다.
Nginx의 저장소를 추가합니다.
$ echo "deb [arch=amd64] http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" | sudo tee /etc/apt/sources.list.d/nginx.list
$ curl -fsSL https://nginx.org/keys/nginx_signing.key | sudo apt-key add -
Nginx를 설치합니다.
$ sudo apt update
$ sudo apt install nginx -y
정상적으로 작동하는지 확인합니다.
$ nginx -v
설치한 Nginx 버전에 따라 다음과 같은 출력을 볼 수 있어야 합니다.
nginx version: nginx/1.18.0
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/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
include fastcgi_params;
}
}
Ctrl + X를 눌러 편집기를 닫고 파일을 저장하라는 메시지가 표시되면 Y를 누릅니다.
이 파일은 /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 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/7.4/fpm/pool.d/www.conf
PHP 프로세스의 Unix 사용자/그룹을 nginx로 설정해야 합니다. 파일 내에서 user=www-data 및 group=www-data 줄을 찾아 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
...
또한 파일 내에서 listen.owner=www-data 및 listen.group=www-data 줄을 찾아 nginx로 변경합니다.
listen.owner = nginx
listen.group = nginx
Ctrl + X를 눌러 파일을 저장하고 저장하라는 메시지가 표시되면 Y를 입력합니다.
PHP-FPM 프로세스를 재시작합니다.
$ sudo systemctl restart php7.4-fpm
PHP 설정을 테스트하려면 html 폴더에 test.php 파일을 생성합니다.
$ sudo nano /var/www/example.com/html/test.php
다음 내용을 추가하고 Ctrl + X를 눌러 파일을 저장하라는 메시지가 표시되면 Y를 입력합니다.
웹 브라우저에서 http://를 열면 다음과 같은 내용을 볼 수 있습니다.

phpMyAdmin 설치
이 튜토리얼을 위해 phpMyAdmin을 수동으로 설치하겠습니다.
먼저 phpMyAdmin이 작동하기 위해 몇 가지 추가 PHP 패키지를 설치해야 합니다.
$ sudo apt install php-mbstring php-zip php-gd php-json php-curl php-bz2 php-xml
/usr/share 디렉토리로 이동하여 phpMyAdmin을 다운로드하고 설치합니다.
$ cd /usr/share
phpMyAdmin 파일을 다운로드합니다. (최신 버전을 사이트에서 확인하십시오) 영어 버전만 설치합니다.
$ sudo wget https://files.phpmyadmin.net/phpMyAdmin/5.0.2/phpMyAdmin-5.0.2-english.tar.gz
$ tar xvzf phpMyAdmin-5.0.2-english.tar.gz
$ sudo mv phpMyAdmin-5.0.2-english /usr/share/phpmyadmin
$ sudo rm phpMyAdmin*.tar.gz
phpMyAdmin 폴더에 적절한 권한을 부여합니다.
$ sudo chown -R nginx:nginx phpmyadmin
$ sudo chmod -R 744 phpmyadmin
phpMyAdmin 구성
패키지와 함께 제공된 샘플 파일에서 구성 파일을 생성합니다.
$ sudo cp /usr/share/phpmyadmin/config.sample.inc.php /usr/share/phpmyadmin/config.inc.php
쿠키 및 보안 목적으로 구성 파일에 비밀 값을 추가해야 합니다. 다음 명령을 사용하여 코드를 생성하고 추가합니다.
$ randomBlowfishSecret=$(openssl rand -base64 32)
$ sed -i "s|cfg\['blowfish_secret'\] = ''|cfg['blowfish_secret'] = '$randomBlowfishSecret'|" /usr/share/phpmyadmin/config.inc.php
Nginx 웹 서버가 phpMyAdmin 파일을 올바르게 찾고 제공할 수 있도록 실제 위치에서 Nginx의 루트 문서 디렉토리로 심볼릭 링크를 만들어야 합니다.
이를 위해 다음 명령을 실행합니다.
$ sudo ln -s /usr/share/phpmyadmin /var/www/example.com/html/phpmyadmin
phpMyAdmin 설치가 완료되었습니다. 접근하려면 http://example.com/phpmyadmin를 열면 됩니다.
기본적으로 이 위치는 변경해야 합니다. 이는 해커가 phpMyAdmin 설치를 찾을 수 있는 가장 일반적인 위치입니다. 이를 위해 다음 명령을 실행합니다.
$ sudo mv /var/www/example.com/html/phpmyadmin /var/www/example.com/html/sm123
기본적으로 phpMyAdmin 위치를 sm123 폴더로 이동했습니다. 이제 접근하려면 브라우저에서 http://example.com/sm123를 열어야 합니다.
여기서 MySQL과 함께 unix_authentication을 사용하고 있으므로 phpMyAdmin을 통해 로그인할 루트 사용자가 없습니다. 먼저 MySQL 사용자를 생성하고 데이터베이스에 대한 권한을 부여해야 phpMyAdmin을 사용할 수 있습니다.
이를 위해 MySQL 셸에 로그인합니다.
$ sudo mysql
이제 다음 명령을 붙여넣어 새 사용자를 생성하고 모든 데이터베이스 권한을 부여합니다.
CREATE USER 'user'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'user'@'localhost';
FLUSH PRIVILEGES;
EXIT
이제 http://example.com/phpmyadmin에서 이 사용자로 로그인할 수 있습니다.
Opcache 구성
이 튜토리얼을 따라왔다면 Opcache는 이미 PHP와 함께 설치되어 있어야 합니다. 만약 설치되지 않았다면 다음 명령을 사용하여 Opcache를 설치할 수 있습니다.
$ sudo apt install php7.4-opcache
설치되었는지 확인합니다.
$ php -v
PHP 7.4.5 (cli) (built: Apr 28 2020 14:49:23) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.5, Copyright (c), by Zend Technologies
Opcache 설정을 변경하려면 /etc/php/7.4/fpm/conf.d/10-opcache.ini 파일을 엽니다.
$ sudo nano /etc/php/7.4/fpm/conf.d/10-opcache.ini
파일 끝에 다음 코드를 붙여넣습니다. 다음 설정은 Opcache 사용을 시작하는 데 도움이 되며 일반적으로 좋은 성능으로 권장됩니다.
opcache.enable_cli=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
Ctrl + X를 눌러 파일을 저장하라는 메시지가 표시되면 Y를 입력합니다.
설정을 적용하려면 서버를 재시작합니다.
$ sudo systemctl restart php7.4-fpm
Let’s Encrypt를 통한 SSL 설치
SSL은 모든 웹사이트의 필수 요소가 되었습니다. 여기서는 Let’s Encrypt 서비스를 사용하여 SSL을 설치합니다.
먼저 Certbot 도구를 설치합니다.
$ sudo apt install certbot python3-certbot-nginx
인증서를 생성합니다.
$ sudo certbot --nginx -d example.com
시스템에서 certbot을 처음 실행하는 경우 이메일 주소와 서비스 약관에 동의하라는 메시지가 표시됩니다. EFF 재단과 데이터 공유에 동의하는지 여부도 질문받게 되며, 이에 대해 “아니오”라고 대답할 수 있습니다. 그렇게 하면 certbot이 Let’s Encrypt 서버와 통신하고 도메인을 확인하기 위한 챌린지를 실행합니다.
성공하면 HTTPS 리디렉션을 처리하는 방법에 대한 질문을 받게 됩니다.
Please choose whether HTTPS access is required or optional.
-------------------------------------------------------------------------------
1: Easy - Allow both HTTP and HTTPS access to these sites
2: Secure - Make all requests redirect to secure HTTPS access
-------------------------------------------------------------------------------
Select the appropriate number [1-2] then [enter] (press 'c' to cancel):
선택하고 Enter를 누릅니다. 그러면 인증서가 생성되고 Nginx 구성 파일이 SSL 설정으로 업데이트됩니다.
인증서가 준비되었으며 이제 https://example.com로 사이트를 열 수 있습니다.
SSL 자동 갱신 확인
이것은 이 튜토리얼을 마치기 전 마지막 단계입니다.
갱신 프로세스를 확인하기 위해 갱신 프로세스의 드라이런을 수행합니다.
$ sudo certbot renew --dry-run
오류가 없으면 설정이 완료된 것입니다. Certbot은 인증서를 자동으로 갱신합니다. 인증서 만료에 대한 경고 이메일이 발송됩니다.
결론
이 튜토리얼은 여기까지입니다. LEMP 설정이 완료되었으며 웹사이트 및 애플리케이션을 만들고 호스팅할 수 있습니다.
새 게시물을 받은 편지함에서 받기
스팸은 없습니다. 언제든지 구독 해지 가능합니다.