Ubuntu에 (LEMP)Nginx, MariaDB, PHP 8.3 최신 버전 설치 방법 – 메모리 절약 설정 포함

Ubuntu에 최신 버전의 Nginx, MariaDB, PHP 8.3을 설치 하는 방법을 찾고 계신가요?
최종적으로 워드프레스를 설치 하시려면 이 단계를 먼저 작업 하셔야 합니다.
이 HowTo 문서에는 사양이 낮은 서버(VM)에서 최대한 메모리 사용량을 줄여서 워드프레스 고유 기능을
최대한 활용 가능하도록 설정하는 내용을 포함시켰습니다. 이제 시작하겠습니다.

LEMP설치하기

1. LEMP 서버에 설치 할 패키지 설명

설치 작업을 시작하기 전에 작업 환경과 최종적으로 설치될 패키지 버전 정보를 소개합니다.

OS :

ubuntu 22.04 minimal

Nginx :

$ nginx -v
nginx version: nginx/1.18.0 (Ubuntu)

MariaDB :

$ mysql -V
mysql  Ver 15.1 Distrib 10.6.16-MariaDB, for debian-linux-gnu (x86_64) using  EditLine wrapper

MariaDB는 MySQL에 비해서 메모리를 적게 사용하는 것으로 알려져 있습니다. 관련해서 자세한 내용은 다른 글에서 소개 하겠습니다.

PHP :

$ php -v
PHP 8.3.8 (cli) (built: Jun  8 2024 21:34:22) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.8, Copyright (c) Zend Technologies
    with Zend OPcache v8.3.8, Copyright (c), by Zend Technologies

2. LEMP 서버(VM) 사양 정보

저는 오라클 클라우드에 VM을 생성했습니다. 아래는 작업하게될 VM의 OS 정보와 메모리, CPU 정보입니다.
특히 메모리가 1GB 라서 워드프레스를 원활하게 사용하기에는 부족할 수 있습니다.
그래서 메모리 사용량을 최대한 줄이는 작업을 추가할 것입니다.

작업할 VM의 OS 정보
작업할 VM의 메모리 정보
작업할 VM의 CPU 정보

현재 이 서버의 CPU성능은 BogoMips 3992.49 수준입니다.

BogoMips 3992.49: 이 정도면 어느 정도일까?

BogoMips 값이 3992.49라면, 이는 보통 중간 정도의 성능을 갖춘 CPU에 해당한다고 볼 수 있습니다. 이 값은 일상적인 작업에는 충분히 적합하며, 다음과 같은 용도로 사용될 수 있습니다.

– 일반적인 오피스 작업: 워드 프로세싱, 스프레드시트, 프레젠테이션 작업 등
– 웹 브라우징: 여러 탭을 열고 사용하는 데 무리가 없음
– 스트리밍 및 멀티미디어: HD 비디오 스트리밍, 음악 감상 등
– 경량 프로그램 실행: 간단한 소프트웨어나 도구 실행

3. LEMP 서버에서 필요 없는 서비스 제거(메모리 확보 작업)

메모리 확보를 위해 필요 없는 서비스를 제거하는 작업을 하겠습니다.
구동중인 서비스 데몬을 확인하고 제거 가능한 서비스를 내리겠습니다.
서버의 메모리가 4GB 이상으로 넉넉하다면 다음 단계로 넘어가도 좋습니다.

$ sudo systemctl list-units --type=service --state=running --no-pager | awk 'NR==1 || /^ / {print $1, $2, $3}'
UNIT LOAD ACTIVE
dbus.service loaded active
getty@tty1.service loaded active
iscsid.service  loaded active
ModemManager.service loaded active
networkd-dispatcher.service loaded active
polkit.service loaded active
rpcbind.service loaded active
serial-getty@ttyS0.service loaded active
snap.oracle-cloud-agent-updater.service loaded active running
snap.oracle-cloud-agent.service loaded active
snapd.service loaded active
ssh.service loaded active
systemd-journald.service loaded active
systemd-logind.service loaded active
systemd-networkd.service loaded active
systemd-resolved.service loaded active
systemd-timesyncd.service loaded active
systemd-udevd.service loaded active
udisks2.service loaded active
unattended-upgrades.service loaded active
user@1001.service loaded active

LOAD = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB = The low-level unit activation state, values depend on unit type.

21 loaded units listed.

Modem Manager는 모뎀 장치를 관리하는 데몬입니다. 일반적인 데스크탑 및 서버 환경에서는 필요하지 않을 수 있습니다.

$ sudo systemctl stop ModemManager.service
$ sudo systemctl disable ModemManager.service

RPC bind portmap service는 원격 프로시저 호출(RPC) 서비스를 위한 포트 매핑을 관리합니다. NFS와 같은 특정 네트워크 서비스를 사용하지 않는다면 비활성화할 수 있습니다.

$ sudo systemctl stop rpcbind.socket
$ sudo systemctl stop rpcbind.service
$ sudo systemctl disable rpcbind.service

원격 접속(SSH)만으로 서버를 관리하기 때문에 필요 없습니다.

$ sudo systemctl stop getty@tty1.service
$ sudo systemctl disable getty@tty1.service

iSCSI initiator daemon은 iSCSI를 통한 네트워크 스토리지 관리를 위한 서비스입니다. iSCSI 스토리지를 사용하지 않는 경우 비활성화할 수 있습니다.

$ sudo systemctl stop iscsid.socket
$ sudo systemctl stop iscsid.service
$ sudo systemctl disable iscsid.service

시리얼 포트를 사용할 일이 없기 때문에 필요 없습니다.

$ sudo systemctl stop serial-getty@ttyS0.service
$ sudo systemctl disable serial-getty@ttyS0.service

스냅 패키지를 사용하지 않고 기존의 apt 패키지 관리 시스템만 사용하기 때문에 비활성화할 수 있습니다.

$ sudo systemctl stop snapd.socket
$ sudo systemctl stop snapd.service
$ sudo systemctl disable snapd.service

메모리를 얼마나 확보했는지 확인해 봅시다.

필요없는 서비스 제거 후 메모리 사용량

free 용량이 368Mi에서 544Mi로 늘었군요.

4. LEMP 서버에 최소 필요 패키지 설치

netstat, vi 등을 사용할 수 있도록 패키지를 설치 합니다. OS를 minimal로 선택해서 필요한 패키지가 더 많이 있지만, 이번 작업에서 필요한 패키지만 설치 했습니다.

$ sudo apt update && sudo apt install net-tools neovim curl -y

5. Nginx 설치 및 방화벽 오픈

Nginx를 설치하고 서비스 포트를 오픈하겠습니다. (클라우드 서비스의 방화벽도 별도로 열어주셔야 합니다.)

$ sudo apt install nginx -y
$ sudo systemctl start nginx
$ sudo systemctl enable nginx

`ufw`를 사용하는 경우는 아래 명령으로 서비스 포트를 오픈합니다.

$ sudo ufw allow 'Nginx Full'
$ sudo ufw status

`iptables`를 사용하는 경우는 아래와 같이 rules.v4 파일에 두줄 추가하고 reload 합니다.

$ sudo vi /etc/iptables/rules.v4
...
-A INPUT -p tcp --dport 80 -j ACCEPT
-A INPUT -p tcp --dport 443 -j ACCEPT
...

$ sudo iptables-restore < /etc/iptables/rules.v4
$ sudo iptables -L -v
...
  288 15622 ACCEPT     tcp  --  any    any     anywhere             anywhere             tcp dpt:http
 1287 72183 ACCEPT     tcp  --  any    any     anywhere             anywhere             tcp dpt:https
...

IP 정보를 확인하고 브라우저에서 접속 가능한지 확인한다.

$ curl -4 icanhazip.com
64.*.*.*

브라우저에서 접속 가능여부 확인
http://64.*.*.*

6. MariaDB 설치 및 기본 설정

MariaDB를 설치하고 기본적인 설정을 진행하겠습니다.

$ sudo apt install mariadb-server -y
$ sudo systemctl start mariadb
$ sudo systemctl enable mariadb

아래 명령으로 초기 설정을 진행합니다.

$ sudo mysql_secure_installation

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): 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] n입력
 ... skipping.

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

Change the root password? [Y/n] 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] y입력
 ... 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] y입력
 ... 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] y입력
 - 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] y입력
 ... 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!

이제 MariaDB의 root 계정의 패스워드를 설정하겠습니다.

$ sudo mysql
MariaDB [(none)]> ALTER USER 'root'@'localhost' IDENTIFIED BY '새로운패스워드';
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> exit

설정한 패스워드로 로그인 가능 여부 확인 하세요.

$ mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 39
Server version: 10.6.16-MariaDB-0ubuntu0.22.04.1 Ubuntu 22.04
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> exit

7. 최신 PHP 8.3 설치하기

LEMP 설치 작업의 마지막 단계인 PHP 설치입니다. PHP 까지 설치를 마무리 하셨으면 워드프레스 설치 단계로 넘어가시면 됩니다. 아래 명령으로 설치 하세요.

$ sudo apt install software-properties-common -y
$ sudo add-apt-repository ppa:ondrej/php ==> enter 입력 해줘야 함
$ sudo apt update
$ sudo apt install php8.3-fpm php8.3-mysql -y
$ sudo systemctl start php8.3-fpm
$ sudo systemctl enable php8.3-fpm

PHP가 제대가 잘 동작하는지 한번 확인 해보세요. /etc/nginx/sites-available/default 파일을 열어서 Nginx와 PHP-FPM 연동 설정을 추가합니다. Nginx 데몬을 restart한 후, /var/www/html/info.php 파일에 아래 내용을 작성하세요.

$ ls /var/run/php/
php-fpm.sock  php8.3-fpm.pid  php8.3-fpm.sock

$ sudo vi /etc/nginx/sites-available/default
...
location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
}
...

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

$ sudo systemctl restart nginx

$ sudo vi /var/www/html/info.php

<?php
phpinfo();
?>

브라우저에서 접속 해 봅니다. http://64.*.*.*/info.php 접속해서 PHP 관련 정보를 보여주는지 확인합니다. 정상 동작 했다면 info.php 파일을 삭제 합니다.

8. 마무리

축하합니다! LEMP 스택(Ubunt, Nginx, MariaDB, PHP)을 성공적으로 설치하고 구성하는 방법을 배우셨고, 워드프레스를 설치할 수 있는 환경을 완성하셨습니다. 계속해서 워드프레스 설치 방법 HowTo 글로 이동해서 작업을 진행하세요.

Ubuntu 22.04에서 LEMP 스택으로 워드프레스 설치하기: 단계별 가이드

Leave a Comment