Home Máy chủ LinuxWeb Server Hướng dẫn cài đặt LEMP Stack trên Ubuntu 22.04

Hướng dẫn cài đặt LEMP Stack trên Ubuntu 22.04

by Thạch Phạm
Published: Last Updated on
A+A-
Reset

Ở bài viết này mình sẽ Hướng dẫn cài đặt LEMP Stack trên Ubuntu 22.04, với các phần thường sử dụng nhất trên LEMP Stack.

I. LEMP Stack là gì?

LEMP là từ viết tắt của Linux, Nginx, MySQL, PHP(hay Perl hoặc Python), để tạo nên một môi trường máy chủ Web có khả năng chứa và phân phối các trang Web động. Bốn phần mềm nói trên tạo thành một gói phần mềm LEMP.

Các thành phần cài đặt LEMP.

  • NGINX làm webserver
  • MariaDB
  • PHP (sử dụng phiên bản 8.1 trong bài viết này)
  • phpMyAdmin

II. Cài đặt LEMP Stack.

1. Login SSH vào server/VPS.

Để cài đặt LEMP, việc đầu tiên bạn cần làm là SSH hoặc truy cập VPS hoặc máy chủ của bạn với quyền root trước. Nếu bạn chưa biết cách SSH vào VPS hoặc Server của bạn thì bạn có thể tham khảo bài viết hướng dẫn sau:

2. Cập nhật hệ thống.

Bạn nên cập nhật các gói trên hệ thống lên bản mới nhất trước khi cài đặt. Và để cập nhật bạn hãy sử dụng lệnh sau

AZDIGI Tutorial
root@ubuntu-2204:~# apt-get update && apt-get upgrade -y
    

3. Thao tác cài đặt LEMP

3.1. Cài đặt NGINX

Có nhiều cách để cài đặt NGINX. Và ở bài viết này mình sẽ cài đặt trực tiếp bằng lệnh apt-get install

AZDIGI Tutorial
root@ubuntu-2204:~# apt-get install nginx -y
    

Tiếp theo bạn sử dụng 3 lệnh sau để kích hoạt và khởi động nginx.

AZDIGI Tutorial
root@ubuntu-2204:~# systemctl enable nginx     #Bật khởi động cùng hệ thống
root@ubuntu-2204:~# systemctl start nginx      #Khởi động nginx
root@ubuntu-2204:~# systemctl status nginx      #Kiểm tra trạng thái
    
● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2022-05-13 14:44:01 +07; 4min 22s ago
       Docs: man:nginx(8)
   Main PID: 2300 (nginx)
      Tasks: 2 (limit: 1034)
     Memory: 4.5M
        CPU: 33ms
     CGroup: /system.slice/nginx.service

Để kiểm tra phiên bản nginx được cài vào là phiên bản bao nhiêu bạn có thể sử dụng lệnh nginx -v để kiểm tra.

AZDIGI Tutorial
root@ubuntu-2204:~# nginx -v
nginx version: nginx/1.18.0 (Ubuntu)
    

Bây giờ bạn hãy truy cập IP trên trình duyệt. Nếu hiện như ảnh bên dưới là đã cài NGINX thành công.

CleanShot 2022 05 13 at 14.55.09@2x

Trường hợp nếu hệ thống bạn có cài firewall. Bạn hãy mở port 80 trên firewall để truy cập.

  • Đối với iptables
AZDIGI Tutorial
root@ubuntu-2204:~# iptables -I INPUT -p tcp --dport 80 -j ACCEPT
    
  • Đối với UFW firewall
AZDIGI Tutorial
root@ubuntu-2204:~# ufw allow http
Hoặc
ufw allow 80/tcp
    

3.2. Cài đặt máy chủ cơ sở dữ liệu

Ở đây mình sử dụng MariaDB làm máy chủ cơ sở dữ liệu. Để cài đặt bạn sử dụng lệnh sau:

AZDIGI Tutorial
root@ubuntu-2204:~# apt-get install mariadb-server mariadb-client -y
    

Cài xong bạn kiểm tra dịch vụ cũng như bật khởi động cùng hệ thống.

AZDIGI Tutorial
root@ubuntu-2204:~# systemctl enable mariadb    #Bật khởi động
root@ubuntu-2204:~# systemctl start mariadb     #Khởi động dịch vụ
root@ubuntu-2204:~# systemctl status mariadb    #Kiểm tra dịch vụ
    
● mariadb.service - MariaDB 10.6.7 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2022-05-13 15:09:50 +07; 4min 28s ago
       Docs: man:mariadbd(8)
             https://mariadb.com/kb/en/library/systemd/
   Main PID: 3220 (mariadbd)
     Status: "Taking your SQL requests now..."
      Tasks: 8 (limit: 1034)
     Memory: 60.1M
        CPU: 575ms
     CGroup: /system.slice/mariadb.service
             └─3220 /usr/sbin/mariadbd
  • Cấu hình MariaDB
AZDIGI Tutorial
root@ubuntu-2204:~# 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):
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] y       #Thay đổi mật khẩu root mariaDB
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!


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!
  • Kiểm tra phiên bản MariaDB
AZDIGI Tutorial
root@ubuntu-2204:~# mariadb --version
mariadb  Ver 15.1 Distrib 10.6.7-MariaDB, for debian-linux-gnu (x86_64) using  EditLine wrapper
    

3.3. Cài đặt PHP

Như trong mô tả đầu bài viết này mình sẽ cài phiên bản PHP 8.1 cũng là phiên bản mới nhất hiện tại ngoài ra mình sẽ cài một số extension quan trọng đi kèm.

AZDIGI Tutorial
root@ubuntu-2204:~# apt-get install php8.1 php8.1-fpm php8.1-mysql php-common php8.1-cli php8.1-common php8.1-opcache php8.1-readline php8.1-mbstring php8.1-xml php8.1-gd php8.1-curl php8.1-soap php8.1-mbstring -y
    
AZDIGI Tutorial
root@ubuntu-2204:~# systemctl enable php8.1-fpm    #Bật khởi động cùng hệ thống
root@ubuntu-2204:~# systemctl start php8.1-fpm     #Khởi động PHP
root@ubuntu-2204:~# systemctl status php8.1-fpm    #Kiểm tra PHP
    
● php8.1-fpm.service - The PHP 8.1 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php8.1-fpm.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2022-05-13 15:42:31 +07; 4min 22s ago
       Docs: man:php-fpm8.1(8)
   Main PID: 13862 (php-fpm8.1)
     Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
      Tasks: 3 (limit: 1034)
     Memory: 9.5M
        CPU: 92ms
     CGroup: /system.slice/php8.1-fpm.service
             ├─13862 "php-fpm: master process (/etc/php/8.1/fpm/php-fpm.conf)

3.4. Cài đặt phpMyAdmin

phpMyAdmin là ứng dụng dùng để quản lý cơ sở dữ liệu dưới dạng giao diện, thông qua phpMyAdmin sẽ giúp bạn quản lý các batabase trực quan hơn. Để cài đặt bạn hãy thực hiện như sau:

AZDIGI Tutorial
root@ubuntu-2204:~# apt-get install phpmyadmin -y
    
CleanShot 2022 05 13 at 16.06.30@2x

Sau khi bạn chạy lệnh sẽ xuất hiện một hộp thoại hỏi bạn chọn webserver để cài. Nếu webserver bạn không có ở đây, bạn không tick và nhấn Tab để di chuyển xuống chữ OK để cài.

Sau đó việc cài đặt sẽ tiếp tục và xuất hiện thêm một hộp thoại với thông tin người dùng với tên phpmyadmin bạn chọn Yes.

CleanShot 2022 05 13 at 16.10.54@2x

Sau đó đặt mật khẩu cho phpmyadmin. Bạn nhập mật khâu xong bấm tab để di chuyển xuống nút OK

CleanShot 2022 05 13 at 16.12.13@2x

Nhập lại mật khẩu một lần nữa để xác nhận.

CleanShot 2022 05 13 at 16.15.16@2x

Quá trình cài đặt hoàn tất, một cơ sở dữ liệu mới có tên là phpmyadmin sẽ được tạo và người dùng cơ sở dữ liệu phpmyadmin có các đặc quyền cần thiết để quản lý cơ sở dữ liệu này. Bạn có thể đăng nhập vào MariaDB và kiểm tra những đặc quyền mà người dùng phpmyadmin đã được cấp.

AZDIGI Tutorial
root@ubuntu-2204:~# mysql -u root
    
MariaDB [(none)]> show grants for phpmyadmin@localhost;
+-------------------------------------------------------------------------------------------------------+
| Grants for phpmyadmin@localhost                                                                                   |
+-------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO `phpmyadmin`@`localhost` IDENTIFIED BY PASSWORD '*C2D9A6C499D*************517E31E6FA1ED006' |
| GRANT ALL PRIVILEGES ON `phpmyadmin`.* TO `phpmyadmin`@`localhost`                                                |
+-------------------------------------------------------------------------------------------------------+
  • Cấu hình để login phpMyAdmin

Bạn tạo một file cấu hình như sau:

AZDIGI Tutorial
root@ubuntu-2204:~# vi /etc/nginx/conf.d/phpmyadmin.conf
    

Sau đó bạn thêm nội dung cấu hình bên dưới vào, và thay đổi example.com bằng tên miền của bạn.

server {
  listen 80;
  listen [::]:80;
  server_name example.com;
  root /usr/share/phpmyadmin/;
  index index.php index.html index.htm index.nginx-debian.html;

  access_log /var/log/nginx/phpmyadmin_access.log;
  error_log /var/log/nginx/phpmyadmin_error.log;

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

  location ~ ^/(doc|sql|setup)/ {
    deny all;
  }

  location ~ \.php$ {
    fastcgi_pass unix:/run/php/php8.1-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    include snippets/fastcgi-php.conf;
  }

  location ~ /\.ht {
    deny all;
  }
}

Lưu ý: Để truy cập được từ tên miền thì bạn cần phải trỏ tên miền về IP máy chủ.

AZDIGI Tutorial
root@ubuntu-2204:~# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
    

Sau đó khởi động lại nginx

AZDIGI Tutorial
root@ubuntu-2204:~# systemctl restart nginx
    

Truy cập vào trình duyệt để kiểm tra phpMyAdmin.

Phpmyadmin

Cảm ơn các bạn đã theo dõi bài viết này, tiếp tục về LEMP Stack trên Ubuntu 22.04. Ở các bài viết sau mình sẽ hướng dẫn các bạn tạo vhost, cài đặt WordPress, SSL và các ứng dụng cache đi kèm.

Nếu các bạn cần hỗ trợ các bạn có thể liên hệ bộ phận hỗ trợ theo các cách bên dưới:

Đánh giá

Tham gia nhóm hỗ trợ Server - Hosting

Tham gia nhóm Hỗ trợ Server - Hosting & WordPress để cùng nhau hỏi đáp và hỗ trợ các vấn đề về WordPress, tối ưu máy chủ/server.

Tham gia ngay

Bài viết cùng chuyên mục

AZDIGI – Không chỉ là đơn vị hàng đầu trong lĩnh vực Web Hosting và Máy chủ, chúng tôi mong muốn mang lại những kiến thức bổ ích nhất và luôn cập nhật thường xuyên cho cộng đồng người đam mê thiết kế website, công nghệ,…

Vui lòng không sao chép nội dung nếu chưa xin phép. Designed and Developed by PenciDesign