Giới thiệu
Mình vừa thực hiện upgrade Ubuntu 18 lên Ubuntu 20.04 và bây giờ mình sẽ tiến hành cài đặt LAMP lên Ubuntu 20.04 để trải nghiệm thử, mời bạn đọc cùng theo dõi và thực hiện nhé.
Sử kết hợp của Linux, Apache, MySQL và PHP được gọi là LAMP stack là môi trường lưu trữ web phổ biến cho ứng dụng dựa trên PHP. Ở đây Linux là một hệ điều hành, Apache là máy chủ web phổ biến được phát triển bởi Apache Foundation, MySQL là hệ thống quản lý cơ sở dữ liệu quan hệ được sử dụng để lưu trữ dữ liệu và PHP là ngôn ngữ lập trình được sử dụng rộng rãi.
Bài viết này sẽ giúp bạn Cài đặt Apache 2.4, MySQL 8.0 và PHP 7.4 trên hệ thống Ubuntu 20.04 LTS (Focal Fossa)
Cài đặt
Để cài đặt được điều tất nhiên bạn phải truy cập vào máy chủ thông qua SSH với quyền root. Và sau đó cập nhật hệ thống.
$ apt update $ apt upgrade
Sau khi mọi thứ đã sẳn sàng hãy thực hiện từng bước sau để cài đặt.
Bước 1: Cài đặt PHP
Ở đây mình sẽ cài đặt PHP 7.4 là phiên bản mới nhất hiện tại, tại thời điểm mình viết bài này. Các gói PHP 7.4 có sẵn trong kho lưu trữ mặc định trên Ubuntu 20.04 LTS. Để cài đặt PHP trên hệ thống của bạn nhập lệnh sau
$ apt install -y php7.4
Sau khi cài đặt PHP hoàn tất, bạn cần cài đặt các modules php cần thiết
$ apt install php7.4-curl php7.4-gd php7.4-json php7.4-mbstring php7.4-xml
Bước 2: Cài đặt Apache2
Tiếp theo, bạn cần cài đặt package Apache2 trên hệ thống của bạn. Các package Apache có sẵn trong kho apt mặc định. Bạn cũng cần cài đặt modules libapache2-mod-php
để làm việc PHP với Apache2. Nhập các lệnh bên dưới để cài đặt
$ apt install apache2 libapache2-mod-php7.4
Sau khi hoàn tất bạn truy cập vào trình duyệt nhập vào domain hoặc IP public, nếu hiện như ảnh thông báo là đã thành công
Bước 3: Cài đặt MySQL Server
Các kho lưu trữ apt Ubuntu 20.04 mặc định chứa máy chủ MySQL 8.0. Bạn sẽ cài đặt các gói máy chủ mysql cho cơ sở dữ liệu MySQL. Ngoài ra, cài đặt gói php-mysql để sử dụng hỗ trợ MySQL bằng PHP.
$ apt install mysql-server php7.4-mysql
$ mysql_secure_installation
root@lab:~# mysql_secure_installation Securing the MySQL server deployment. Connecting to MySQL using a blank password. VALIDATE PASSWORD COMPONENT can be used to test passwords and improve security. It checks the strength of password and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD component? Press y|Y for Yes, any other key for No: y There are three levels of password validation policy: LOW Length >= 8 MEDIUM Length >= 8, numeric, mixed case, and special characters STRONG Length >= 8, numeric, mixed case, special characters and dictionary file Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2 Please set the password for root here. New password: <== Đặt mật khẩu root mysql Re-enter new password: <== Nhập lại mật khẩu root mysql Estimated strength of the password: 50 Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL 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? (Press y|Y for Yes, any other key for No) : 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? (Press y|Y for Yes, any other key for No) : y Success. By default, MySQL 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? (Press y|Y for Yes, any other key for No) : 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? (Press y|Y for Yes, any other key for No) : y Success. All done! root@lab:~#
Bước 4: Cài đặt phpMyAdmin
phpMyAdmin cung cấp một giao diện website trực quan giúp bạn quản lý cơ sở dữ liệu MySQL một cách dễ dàng hơn, để cài đặt được bạn thực hiện như sau.
$ apt install phpmyadmin
Quá trình cài đặt sẽ tuỳ chọn hỏi bạn webserver, bạn chọn Apache để chạy phpMyAdmin nhé. Sau khi cài đặt hoàn tất bạn truy cập bằng ip/phpmyadmin sẽ ra kết quả như anh sau.
Bước 5: Kiểm tra quản trị các dịch vụ
Sau khi cài đặt LAMP hoàn tất, bạn cần nắm các lệnh cơ bản sau để quản trị các dịch vụ
- Restart dịch vụ Apache và MySQL
$ systemctl restart apache2 $ systemctl restart mysql
- Start dịch vụ Apache và MySQL
$ systemctl start apache2 $ systemctl start mysql
- Stop dịch vụ Apache và MySQL
$ systemctl stop apache2 $ systemctl stop mysql
- Kiểm tra trạng thái dịch vụ Apache và MySQL
$ systemctl status apache2 $ systemctl status mysql
Bước 6: Kiểm tra firewalld
Nếu máy chủ đang thiết lập tường lửa thì bạn cần mở các port để các dịch vụ có thể hoạt động
$ firewall-cmd --permanent --add-service=http $ firewall-cmd --permanent --add-service=https $ firewall-cmd --reload
Bước 7: Kiểm tra hệ thống
Ở bước này mình sẽ thực hiện kiểm tra sau khi cài đặt, bằng cách mình sẽ tạo một file info.php tại thư mục sau đó truy cập ip/info.php
$ echo "<?php phpinfo(); ?>" > /var/www/html/info.php
Chúc mừng bạn đã vừa thực hiện hoàn tất cài đặt một máy chủ web sử dụng LAMP trên Ubuntu 20.04. Hãy bình luận bên dưới nếu bạn có bất kỳ câu hỏi nào cần để lại nhé.