Can't figure out Nginx PHP

February 22, 2018 540 views
PHP Nginx Ubuntu 16.04

I've been scouring through all the nginx, Wordpress, PHP threads but I can't seem to get what I need. I have a Ghost blog installed (working fine) and am trying to add a Wordpress along side it. The Wordpress block looks like:

server {                                                     
        listen 80;                                           
        listen [::]:80;                                      

        root /var/www/html;                                  
        index index.php index.html index.htm;                

        server_name wpdomain.com;                        

        location / {                                         
                try_files $uri $uri/ /index.php?q=$uri&$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_split_path_info ^(.+\.php)(/.+)$;    
                fastcgi_pass 127.0.0.1:9000;                 
                fastcgi_index index.php;                   
                include fastcgi_params;                      
        }                                                    

}                                                            

I've tried the alternative:

fastcgi_pass unix:/run/php/php7.2-fpm.sock;

with the appropriate listen adjustment to no avail.

The status looks fine:

php7.2-fpm.service - The PHP 7.2 FastCGI Process Manager
   Loaded: loaded (/lib/systemd/system/php7.2-fpm.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2018-02-22 00:22:56 UTC; 6min ago
     Docs: man:php-fpm7.2(8)
 Main PID: 22063 (php-fpm7.2)
   Status: "Processes active: 0, idle: 2, Requests: 6, slow: 0, Traffic: 0req/sec"
    Tasks: 3
   Memory: 11.4M
      CPU: 68ms
   CGroup: /system.slice/php7.2-fpm.service
           ├─22063 php-fpm: master process (/etc/php/7.2/fpm/php-fpm.conf)
           ├─22066 php-fpm: pool www
           └─22067 php-fpm: pool www

and I can access the non-php files just fine, but I can't setup Wordpress until I can actually access the PHP. I'm not seeing errors in /var/log/nginx/error.log either, I'm not sure what else to check at this point...

1 Answer
server {                                                     
        listen 80;                                           
        # listen [::]:80; ## only need this directive if you are using ipv6

        root /var/www/html;                                  
        index index.php index.html index.htm;                

        server_name wpdomain.com;                        

        location / {                                         
                try_files $uri $uri/ /index.html /index.php$args; #removed unneeded args 
        }                                                    

        # remove these for now until WP is up and running
        # 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_split_path_info ^(.+\.php)(/.+)$;    
                fastcgi_pass unix:/run/php/php7.2-fpm.sock;                 
                fastcgi_index index.php;                   
                include fastcgi_params;                      
        }                                                    
}

ensure the WP directory has the right permissions:

find /var/www/html -type d -exec chmod 755 {} \;

find /var/www/html -type f -exec chmod 644 {} \;


You may want to look into a configuration similar to this : https://www.digitalocean.com/community/tutorials/how-to-host-multiple-websites-securely-with-nginx-and-php-fpm-on-ubuntu-14-04

It's well known that the LEMP stack (Linux, nginx, MySQL, PHP) provides unmatched speed and reliability for running PHP sites. Other benefits of this popular stack such as security and isolation are less popular, though. In this article we'll show you the security and isolation benefits of running sites on LEMP with different Linux users. This will be done by creating different php-fpm pools for each nginx server block (site or virtual host).
  • Thanks! In the end, nothing seemed to work, but I did use the guide you sent to restart the whole thing and I've now got Ghost/WP working properly.

Have another answer? Share your knowledge.