Skip to contentSkip to main navigation Skip to footer

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

CleanShot 2022 05 13 at 14.55.09@2x

Ở 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ì?

Thông tin về LEMP Stack.

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.

  • Máy chủ Ubuntu 22.04.
  • NGINX làm webserver
  • MariaDB
  • phpMyAdmin

II. Cài đặt LEMP Stack.

I. 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:

II. cập nhật hệ thống.

Trước khi cài đặt bạn cần phải cập nhật lại hệ thống và các dịch vụ chạy trên server/VPS để quá trình cài đặt được tốt hơn.

root@ubuntu-2204:~# apt-get update
root@ubuntu-2204:~# apt-get upgrade
    

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

1. Cài đặt NGINX

Ở đây mình sẽ cài nginx với lệnh bên dưới.

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

Tiếp theo bạn sử dụng 3 lệnh sau:

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
             ├─2300 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
             └─2303 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ">

Kiểm tra phiên bản Nginx.

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 firewall để dịch vụ lắng nghe.

– iptables

root@ubuntu-2204:~# iptables -I INPUT -p tcp --dport 80 -j ACCEPT
    

UFW firewall

root@ubuntu-2204:~# ufw allow http
    

2. Cài đặt MariaDB(Database server)

Ở đâ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:

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.

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

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

root@ubuntu-2204:~# mariadb --version
mariadb  Ver 15.1 Distrib 10.6.7-MariaDB, for debian-linux-gnu (x86_64) using  EditLine wrapper
    

3. Cài đặt PHP

Bài viết này mình sẽ cài phiên bản PHP 8.1 và các extension đi kèm.

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 -y
    
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)" "" "" "" "" "" "" "" "" "" "" "" "">
             ├─13864 "php-fpm: pool www" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ">
             └─13865 "php-fpm: pool www" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ">

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. Giúp bạn quản lý Database trực quan hơn. và dễ dàng kiểm soát quản lý. Để cài đặt bạn hãy thực hiện như sau:

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.

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.

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:

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ủ.

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

root@ubuntu-2204:~# systemctl restart nginx
    

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

Phpmyadmin

Chúc bạn thực hiện thành công và thuận lợi.

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:

Was This Article Helpful?

0
0 Comments

There are no comments yet

Leave a comment

Your email address will not be published. Required fields are marked *