Để đủ điều kiện một website chạy bằng ngôn ngữ mã nguồn mở php hoạt động được ngoài hạ tầng máy chủ (cloud server) thì cần có ít nhất các services sau: Php, apache (nginx), mysql (maria db).
Sau đây chúng tôi sẽ hướng dẫn bạn cách cài đặt các service trên một cách đơn giản nhất:
1. Cài đặt php 7.4 và apache 2 trên Ubuntu 18.04 và 20.04
# sudo apt-get update

Tiếp theo, cài đặt software-properties-common, bổ sung quản lý cho các nguồn phần mềm bổ sung:
# sudo apt -y install software-properties-common

Tiếp theo, cài đặt kho lưu trữ ppa:ondrej/php, kho lưu trữ này sẽ cung cấp cho bạn tất cả các phiên bản PHP của bạn:
# sudo add-apt-repository ppa:ondrej/php
Bắt đầu cài đặt php 7.4:
# sudo apt -y install php7.4
Kiểm version:
# php -v
PHP 7.4.21 (cli) (built: Jul 1 2021 16:09:41) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.21, Copyright (c), by Zend Technologies

Cài đặt các module cần thiết theo php 7.4
# sudo apt-get install -y php7.4-cli php7.4-json php7.4-common php7.4-mysql php7.4-zip php7.4-gd php7.4-mbstring php7.4-curl php7.4-xml php7.4-bcmath
2. Cài đặt maria db trên Ubuntu 18.04 và Ubuntu 20.04
2.1 Cài đặt maria db
# sudo apt-get update
# sudo apt-get install software-properties-common
# sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8
# sudo add-apt-repository 'deb [arch=amd64,arm64,ppc64el] http://sfo1.mirrors.digitalocean.com/mariadb/repo/10.4/ubuntu bionic main'
# sudo apt-get update
sudo systemctl stop mariadb.service
sudo systemctl start mariadb.service
sudo systemctl enable mariadb.service
# sudo mysql_secure_installation
Làm theo tuần tự các bước như sau:
- Enter current password for root (enter for none): Enter
- Set root password? [Y/n]: Y
- New password: Enter password
- Re-enter new password: Repeat password
- Remove anonymous users? [Y/n]: Y
- Disallow root login remotely? [Y/n]: Y
- Remove test database and access to it? [Y/n]: Y
- Reload privilege tables now? [Y/n]: Y
2.2. Tạo database name
# sudo mysql -u root -p
# GRANT ALL ON database_name.* TO 'database_user@localhost' IDENTIFIED BY 'database_user_password';
2.3. Bật chế độ remote access cho maria db
Chỉnh file 50-server.cnf trong mariadb.conf.d cho phép ip ngoài kết nối đến database, mặc định ip localhost: 127.0.0.1
- Nếu mở cho tất cả ip được kết nối tới database thì thay bằng IP 0.0.0.0
- Nếu chỉ cho phép một ip kết nối tới thì đặt IP server đó, ở đây server web của mình ip: 10.0.100.10
# sudo vi /etc/mysql/mariadb.conf.d/50-server.cnf
# this is read by the standalone daemon and embedded servers
# this is only for the mysqld standalone daemon
[mysqld]
#
# * Basic Settings
#
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 10.0.100.11
#
# * Fine Tuning
# sudo systemctl restart mariadb.service
# sudo apt install net-tools
# sudo netstat -anp | grep 3306
tcp 0 10.0.100.10:3306 0.0.0.0:* LISTEN 1938/mysqld

Tiếp theo tạo user cho phép remote access
# GRANT ALL ON database_name.* TO 'database_user@10.0.100.10' IDENTIFIED BY 'database_user_password';
Kiểm tra kết nối database:
# mysql -u database_user -p database_user_password -h database_server
Cuối cùng, Server mở port cho phép ip server web kết nối vào database
Xem mục 3 ufw.
3. Cài đặt firewall ufw trên Ubuntu 18.04 và Ubuntu 20.04
Cài đặt firewall ufw:
# sudo apt install ufw

Kiểm tra tình trạng ufw:
# sudo ufw status verbose

Bật ufw trên máy chủ ubuntu 20.04:
# sudo ufw enable
Kiểm tra danh sách port:
# sudo ufw app list
Available applications:
Apache
Apache Full
Apache Secure
OpenSSH
Kiểm tra services apache dùng những port nào:
# sudo ufw app info 'Apache Full'
Profile: Apache Full
Title: Web Server (HTTP,HTTPS)
Description: Apache v2 is the next generation of the omnipresent Apache web
server.
Ports:
80,443/tcp
Mở port SSH:
# sudo ufw allow ssh
hoặc
# sudo ufw allow 22/tcp
Mở port http, https:
# sudo ufw allow ssh
hoặc
# sudo ufw allow 80/tcp
# sudo ufw allow 443/tcp

Mở port cho phép ip truy cập vào service: cụ thể ở đây cho phép IP 10.0.0.10 vào MariaDB:
# sudo ufw allow from 10.0.100.10 to any port 3306
4. Kiểm tra
4.1. Kiểm tra kết nối Web
Mở browser lên và nhập địa chỉ ip server: https://yourip/
4.2. Kiểm tra kết nối remote access Maria DB
Trên server ip 10.0.100.11 kiểm tra như sau:
# telnet 10.0.100.11 3306
5. Remove Maria DB và upgrade từ version mariadb 10.3 lên 10.4 hoặc cao hơn
Remove maria DB cũ ra:
# sudo apt-get purge mariadb-server
hoặc
# sudo apt-get remove mariadb-server
Tiến hành cài lại mariadb mới, làm theo các lệnh dưới đây
# sudo apt-get update
# sudo apt-get install software-properties-common
# sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8
# sudo add-apt-repository 'deb [arch=amd64,arm64,ppc64el] http://sfo1.mirrors.digitalocean.com/mariadb/repo/10.4/ubuntu bionic main'
# sudo apt-get install mariadb-server galera-4 mariadb-client libmariadb3 mariadb-backup mariadb-common
# sudo systemctl start mysql
# sudo mysql_upgrade --verbose --verbose other-options --force