I, Cài đặt nginx
1, Thêm Nginx Repository
|
1 |
sudo dnf install epel-release -y |
2, Cài đặt nginx
|
1 |
sudo dnf install nginx -y |
3, Khởi động nginx
|
1 |
sudo systemctl start nginx |
4, Mở firewall cho HTTP/HTTPS:
|
1 2 3 |
sudo firewall-cmd --permanent --zone=public --add-service=http sudo firewall-cmd --permanent --zone=public --add-service=https sudo firewall-cmd --reload |
Nếu chưa cài đặt firewall thì cần thực hiện các bước sau rồi mới quay lại mở port cho http/https
|
1 2 3 4 5 6 7 |
## Cài firewalld sudo dnf install firewalld -y ## Bật và khởi động firewalld sudo systemctl enable firewalld sudo systemctl start firewalld sudo systemctl status firewalld |
5, Bật nginx
|
1 |
sudo systemctl enable nginx |
II, Cài đặt PHP
1, Thêm repository PHP 7.4
|
1 2 3 4 5 |
sudo dnf install dnf-utils -y sudo dnf install https://rpms.remirepo.net/enterprise/remi-release-9.rpm -y sudo dnf module reset php -y sudo dnf module list php sudo dnf module enable php:remi-7.4 -y |
2, Cài PHP 7.4 và các extension cần thiết
|
1 |
sudo dnf install php php-cli php-fpm php-mysqlnd php-pdo php-mbstring php-xml php-gd php-curl php-json -y |
3, Kiểm tra phiên bản php
|
1 |
php -v |
4, Cấu hình PHP-FPM cho Nginx
a, Chỉnh php-fpm chạy dưới user nginx:
|
1 2 |
sudo dnf install nano -y sudo nano /etc/php-fpm.d/www.conf |
Thay user = apache và group = apache thành:
|
1 2 |
user = nginx group = nginx |
Thay
|
1 |
listen = /var/run/php-fpm/php-fpm.sock |
Bỏ comment và sửa thành
|
1 2 3 |
listen.owner = nginx listen.group = nginx listen.mode = 0660 |
b, Bật và khởi động PHP-FPM:
|
1 2 3 |
sudo systemctl enable php-fpm sudo systemctl start php-fpm sudo systemctl status php-fpm |
III, Cài đặt Mysql 8
1, Tắt module MySQL mặc định (nếu cần)
|
1 |
sudo dnf module disable mysql -y |
2, Thêm MySQL Community Repo cho EL9:
|
1 |
sudo dnf install https://dev.mysql.com/get/mysql80-community-release-el9-1.noarch.rpm -y |
3, Kiểm tra repo MySQL EL9 đã có:
|
1 |
dnf repolist enabled | grep mysql |
4, Thiết lập key
|
1 |
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2023 |
5, Cài MySQL server:
|
1 |
sudo dnf install mysql-community-server -y |
6, Khởi động mysql
|
1 |
sudo systemctl start mysqld |
7, Lấy mật khẩu mysql root tạm thời
|
1 |
grep 'A temporary password is generated' /var/log/mysqld.log | tail -1 |
8, Thay đổi password root và thiết lập secure
|
1 |
sudo mysql_secure_installation |
- Enter password for user root: [Enter temporary password]
- New password: [Enter a new password]
- Re-enter new password: [Re-enter new password]
- Change the password for root ? ((Press y|Y for Yes, any other key for No) : n
- Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
- Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
- Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
- Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
IV, Cấu hình nginx domain
1, Tạo folder chứa source code
|
1 2 3 |
sudo mkdir /var/www/your-domain.com sudo mkdir /var/www/your-domain.com/{html,logs} sudo chown -R nginx:nginx /var/www/your-domain.com |
2, Kiểm tra setenforce đã được tắt chưa
|
1 2 3 4 5 |
setenforce status # Nếu kết quả khác : setenforce: SELinux is disabled thì chạy lệnh dưới để tắt setenforce 0 nginx -s reload |
3, Tạo file cấu hình virtural host
|
1 |
sudo nano /etc/nginx/conf.d/yourdomain.com.conf |
Nội dung như sau:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
server { listen 80; server_name your-domain.com; root /var/www/your-domain.com/html; index index.php index.html index.htm; location / { try_files $uri $uri/ /index.php?$args; } error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ \.php$ { try_files $uri =404; fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } |
4, Kiểm tra cấu hình và restart lại nginx
|
1 2 3 4 |
nginx -t # => nếu thông báo: nginx: configuration file /etc/nginx/nginx.conf test is successful sudo systemctl restart nginx |
V, Tạo mới database
1, Đăng nhập vào mysql root
|
1 |
mysql -u root -p |
2, Tạo database, user và thiết lập quyền
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
-- Tạo database CREATE DATABASE `yourdatabase`; -- Tạo user và mật khẩu CREATE USER 'youruserdatabase'@'localhost' IDENTIFIED BY 'h5Mi3qH2nR#W'; -- Cấp quyền GRANT ALL PRIVILEGES ON yourdatabase.* TO 'youruserdatabase'@'localhost' WITH GRANT OPTION; -- Cập nhật quyền FLUSH PRIVILEGES; -- Kiểm tra quyền user SHOW GRANTS FOR 'youruserdatabase'@'localhost'; |