top of page

HƯỚNG DẪN CẤU HÌNH NGINX CHẠY ĐƯỢC FILE PHP

Writer's picture: Tuan NguyenTuan Nguyen

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


Vậy là Nginx đã chạy được code php.

23 views0 comments

Comentários


 

© 2018 by Tuấn Nguyễn

 Liên hệ tôi
  • Facebook - Black Circle
  • Google+ - Black Circle
bottom of page