Cài đặt Nginx và PHP-FPM on Centos
Cài đặt PHP
Bước 1: Cài đặt REMI Repository
sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

Bước 2: Tiến hành tìm kiếm các repo chứa bản PHP mà chúng ta cần
yum --disablerepo="*" --enablerepo="remi-safe" list php[7-9][0-9].x86_64
Chúng ta có thể thấy có rất nhiều bản PHP từ 7.0 tới 8.0 trong hình

Bước 3: Chẳng hạn ở đây tôi muốn cài PHP7.4 nên chúng ta sẽ kích hoạt repo tương ứng
sudo yum install yum-utils - y sudo yum-config-manager --enable remi-php74

Bước 4: Tiến hành cài đặt PHP
sudo yum install php php-mysqlnd php-fpm

Bước 5: Kiểm tra phiên bản PHP xem đã cài đặt đúng chưa
php -v

Bước 6: Tiến hành chỉnh sửa file cấu hình của PHP
yum install nano -y sudo nano /etc/php-fpm.d/www.conf
Tìm tới đoạn sau


Sửa Apache thành Nginx
Tìm tới dòng listen =

Và sửa thành:
/var/run/php-fpm/php-fpm.sock;

Tìm tới đoạn này

Sửa lại thành
listen.owner = nginx listen.group = nginx listen.mode = 0660
Cuối cùng lưu files lại
Bước 5: Tiến hành kích hoạt PHP-FPM
sudo systemctl start php-fpm
4. Cấu hình NGINX để xử lí file PHP
Bước 1: Mở file cấu hình NGINX
sudo nano /etc/nginx/conf.d/default.conf
Dán vào đoạn cấu hình sau
server { listen 80; server_name domain hoặc IP của bạn; root /usr/share/nginx/html; index index.php index.html index.htm; location / { try_files $uri $uri/ =404; } 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; } }

Bước 2: Khởi chạy lại NGINX
sudo systemctl restart nginx
5. Kiểm tra PHP đã hoạt động hay chưa
Bước 1: Tạo ra một file info.php
nano /usr/share/nginx/html/info.php
Gõ nội dung sau vào
<?php phpinfo();
Bước 2: Truy cập domain/info.php thấy như hình là thành công
