• Show More Brands
Home / Hướng dẫn cấu hình Linux / Linux: Hướng dẫn cài đặt LAMP stack trên Ubuntu Server

Linux: Hướng dẫn cài đặt LAMP stack trên Ubuntu Server

Overview

Bài viết hướng dẫn cài đặt LAMP trên Ubuntu Server

LAMP (Linux – Apache – MySQL – PHP) là một hệ thống các ứng dụng được cài đặt để chạy môi trường Web Server

Quyền cài đặt hiện tại đang là quyền root

Hướng dẫn cài đặt

Cài Apache

  • Update Server bằng câu lệnh
sudo apt -y update
  • Cài đặt Apache2
sudo apt -y install apache2
  • Kiểm tra phiên bản Apache2 đã cài
apache2 -v
  • Khởi động dịch vụ Apache2
systemctl reload apache2
  • Bật dịch vụ Apache2 chạy cùng hệ thống
systemctl enable apache2 
  • Enable cho phép dịch vụ Apache chạy trên port 80 và 443
sudo ufw allow in "Apache Full"

Cài MySQL Database

  • Update Server
sudo apt -y update
  • Cài đặt gói MySQL
sudo apt install mysql-server mysql-client -y
  • Kiểm tra dịch vụ MySQL
sudo systemctl status mysql
  • Cấu hình bảo mật MySQL
sudo mysql_secure_installation
sudo mysql_secure_installation
[sudo] password for toor: 

Securing the MySQL server deployment.

Enter password for user root: 

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
Using existing password for root.

Estimated strength of the password: 0 
Change the password for root ? ((Press y|Y for Yes, any other key for No) : Y

New password: 

Re-enter new password: 

Estimated strength of the password: 100 
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! 
  • Ở dòng Enter password for user root: yêu cầu nhập mật khẩu tài khoản root quản trị database khi cài đặt MySQL, nếu bạn không đặt mật khẩu ở bước đó thì ấn Enter bỏ qua
  • Sau khi nhập mật khẩu, có thông báo hiện ra cho ta lựa chọn sử dụng VALIDATE PASSWORD PLUGIN, để chọn mức độ phức tạp của mật khẩu
VALIDATE PASSWORD PLUGIN 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 plugin?

   Press y|Y for Yes, any other key for No: Y
  • Chọn 1 số mà bạn muốn
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
  • Thông báo tiếp theo cho ta biết độ an toàn của mật khẩu hiện có và cho phép ta thay đổi mật khẩu, để thay đổi mật khẩu nhấn Y. Nếu nhấn Y thì ta nhập mật khẩu mới và xác nhận lại mật khẩu
  • Thông báo xác nhận thay đổi mật khẩu -> Nhấn Y
Estimated strength of the password: 100 
   Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y
  • Tại dòng Remove anonymous users? (Press y|Y for Yes, any other key for No) : cho phép ta xóa tài khoản anonymousanonymous là tài khoản mặc định ngay từ khi cài đặt MySQL. Để xóa anonymous user ta ấn Y
  • Tại dòng Disallow root login remotely? (Press y|Y for Yes, any other key for No) : cho phép ta vô hiệu hóa tính năng đăng nhập từ xa đối với tài khoản root. Để vô hiệu hóa ta ấn Y
  • Tại dòng Remove test database and access to it? (Press y|Y for Yes, any other key for No) : cho phép ta xóa cơ sở dữ liệu test, một cơ sở dữ liệu được tạo mặc định ngay từ khi cài đặt MySQL. Để xóa ta ấn Y
  • Tại dòng Reload privilege tables now? (Press y|Y for Yes, any other key for No) : ta ấn Y để Reload privilege nhằm áp dụng các cấu hình ta vừa đặt ở trên
  • Đăng nhập bằng tài khoản root và thực hiện thử truy vấn SQL đơn giản để kiểm tra

mysql -u root -p

mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: ... MySQL Community Server - GPL

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
  • Kiểm tra thử bằng câu truy vấn
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.02 sec)

mysql> select host, user from mysql.user;
+-----------+------------------+
| host      | user             |
+-----------+------------------+
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | root             |
+-----------+------------------+
4 rows in set (0.00 sec)

mysql> 
  • Trong các hệ thống Ubuntu chạy MySQL 5.7 và các phiên bản mới hơn, người dùng MySQL root được đặt để xác thực bằng cách sử dụng plugin auth_socket theo mặc định thay vì bằng mật khẩu. Điều này làm phức tạp mọi thứ khi bạn cần sử dụng một chương trình bên ngoài (VD: phpMyAdmin)
  • Để sử dụng mật khẩu để kết nối với MySQL với quyền root, bạn cần chuyển phương thức xác thực từ auth_socket sang mysql_native_password

sudo mysql

  • Kiểm tra phương thức xác thực của người dùng MySQL

SELECT user,authentication_string,plugin,host FROM mysql.user;

  • Thay đổi phương thức bằng câu lệnh ALTER USER

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

  • Lưu các thay đổi mới

FLUSH PRIVILEGES;

  • Để thoát MySQL

Gõ exit

Cài đặt PHP

  • Cài đặt PHP và các gói hỗ trợ
apt -y install php libapache2-mod-php php-cli php-fpm php-json php-pdo php-mysql php-zip php-gd  php-mbstring php-curl php-xml php-pear php-bcmath
  • Kiểm tra phiên bản PHP
php -v
  • Kích hoạt module Apache cho PHP 7.*
a2enmod php7.*
  • Khởi động lại Apache
systemctl reload apache2 
3 2 đánh giá
Đánh giá bài viết
Theo dõi
Thông báo của
guest
0 Góp ý
Phản hồi nội tuyến
Xem tất cả bình luận
0
Rất thích suy nghĩ của bạn, hãy bình luận.x