![](https://static.wixstatic.com/media/68165d_eae9090699b24257b1429ee8b218f1ff~mv2.png/v1/fill/w_800,h_351,al_c,q_85,enc_auto/68165d_eae9090699b24257b1429ee8b218f1ff~mv2.png)
Khác với Apache, Nginx không chạy được file PHP nên chúng ta cần phải cài một ứng dụng khác để hỗ trợ chạy được PHP. Trong hướng dẫn này chúng ta sẽ cài PHP-FPM.
Trước tiên chúng ta cần cài Nginx và PHP 7.x.
Cài php-fpm
# yum install php-fpm
Tạo file config
# vi /etc/nginx/conf.d/yourdomainname.conf
server {
listen 80;
server_name your_server_ip_or_domain;
# note that these lines are originally from the "location /" block
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;
}
}
Mặc định php-fpm chạy với user apache trên port 9000, chúng ta sẽ đổi apache thành nginx và chuyển TCP socket (listen = 127.0.0.1:9000) thành Unix socket.
# vi /etc/php-fpm.d/www.conf
Kết quả sau khi thay đổi như sau:
...
user = nginx
...
group = nginx
...
listen.owner = nginx
listen.group = nginx
...
listen = /run/php-fpm/www.sock
...
Start và enable php-fpm
# systemclt start php-fpm
# systemctl enable php-fpm
Tạo file php để test thử.
# vi /usr/share/nginx/html/info.php
<?php
phpinfo();
?>
Khởi động lại Nginx.
# systemctl restart nginx
Vào trình duyệt web gõ: ip_server_or_domain/info.php
![](https://static.wixstatic.com/media/68165d_e385740283f148c7af36342ea9e9ef5b~mv2.png/v1/fill/w_980,h_275,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/68165d_e385740283f148c7af36342ea9e9ef5b~mv2.png)
Vậy là Nginx đã chạy được code php.
Comentários