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

RELATED POST

How to Install SFTPGo on Ubuntu 22.04

Setup UFW Firewall For security reasons, it is recommended to install and configure the UFW firewall in your system. First,…

Executing Bash Scripts at Startup in Ubuntu Linux

Creating a Bash script in Ubuntu To create a Bash script in Ubuntu, you can use any text editor of…

How To Clear PHP’s Opcache

PHP can be configured to store precompiled bytecode in shared memory, called Opcache. It prevents the loading and parsing of PHP…

iLO port configuration on HP servers + license

HP Integrated Lights-Out (iLO) Advanced License 34RQQ-6D5R4-G8NW3-LBMG6-MLJXR325WC-J9QJ7-495NG-CP7WZ-7GJMM iLO 2 Advanced features have been activated License Key: License Key: 35DPH-SVSXJ-HGBJN-C7N5R-2SS4W32Q8Y-XZVGQ-4SGJB-4KY3R-M9ZBN –…