nginx, php and localhost Ubuntu 13.04

The place to post if you need help or advice

Moderators: ChrisThornett, LXF moderators

nginx, php and localhost Ubuntu 13.04

Postby duff » Wed Sep 11, 2013 3:34 pm

Hi Everyone

Long time since I posted here.

I'm having a problem with nginx and php on ubuntu 13.04.

I'm very new to web development and php. I want to be able to read a text file (tab or comma sep) on the server using php and pass the resulting array to js for further client side processing.

In order to get nginx to play with php Google tells me I have to install php5-fpm and set this up. So I have followed a couple of web tutorials (tell you more or less the same thing) but the setup isn't working.

Relevant configurations are (note I've left out a load of stuff that's commented out in these files):

Nginx default file
Code: Select all
server {
   listen 80;
   listen [::]:80 default_server ipv6only=on;

   # Make site accessible from http://localhost/
   server_name localhost;

      root /usr/share/nginx/html;
      index index.php index.html index.htm;

   location / {
      # First attempt to serve request as file, then
      # as directory, then fall back to displaying a 404.
      try_files $uri $uri/ /index.html;
      # Uncomment to enable naxsi on this location
      # include /etc/nginx/naxsi.rules
   }

   location /doc/ {
      alias /usr/share/doc/;
      autoindex on;
      allow 127.0.0.1;
      allow ::1;
      deny all;
   }

   # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
   #
   location ~ \.php$ {
      try_files $uri=404;
      fastcgi_split_path_info ^(.+\.php)(/.+)$;
   #   # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
   #
   #   # With php5-cgi alone:
   #   fastcgi_pass 127.0.0.1:9000;
   #   # With php5-fpm:
      fastcgi_pass unix:/var/run/php5-fpm.sock;
      fastcgi_index index.php;
      include fastcgi_params;
   }

}



In the
Code: Select all
/etc/php5/fpm/php.ini
file I uncommented and changed the line
Code: Select all
cgi.fix_pathinfo=1
to
Code: Select all
cgi.fix_pathinfo=0;
(as per above)

I created a test file:

Code: Select all
cd /usr/share/nginx/html
sudo su
echo '<?php phpinfo(); ?>' > info.php

and this is fine

Both nginx and php5-fpm were restarted.

Browsing to
Code: Select all
http://localhost
let's me see the nginx default page (so I know nginx is working) but
Code: Select all
http://localhost/info.php
does not display the page but only asks if I want to download the info.php file.

Can anyone help with this? I'd like to get nginx and php to play nice on my local machine so I can get the hang of server side programming.

Best

duff
duff
 
Posts: 47
Joined: Tue Oct 11, 2005 11:50 am

Postby leke » Wed Sep 11, 2013 5:22 pm

I would be interested to see the output of...
ls -l /usr/share/nginx/html
User avatar
leke
LXF regular
 
Posts: 506
Joined: Mon Oct 22, 2007 5:45 pm
Location: Oulu, Finland

Postby duff » Wed Sep 11, 2013 9:43 pm

Hi leke
Code: Select all
total 12
-rw-r--r-- 1 root root 537 Sep 24  2012 50x.html
-rw-r--r-- 1 root root 612 Sep 24  2012 index.html
-rw-r--r-- 1 root root  20 Sep 11 17:59 info.php


Following another post from someone using Arch Linux I removed nginx, nginx-common, nginx-full, php5-common, php5-fmp and php5-cli. Reinstalled nginx, nginx-common, nginx-full, php5-common and php5-fmp. Now I can display the info.php page as it should be.

But if I try my own code in html it doesn't display properly. For example:
Code: Select all
<!DOCTYPE html>
<html>
   <head>
      <title>Example.com test</title>
   </head>

   <body>
      <p>Success with the virtual server</p>
   <?php
          echo "<p>php says hi</p>";
   ?>
   </body>
</html>


displays as:
Code: Select all
Success with the virtual server
php says hi

"; ?>


i.e. the trailing "; ?> are shown on the page.

So that's strange and annoying enough. If I save the index page as index.php instead of index.html and point my browser at the localhost site (http://locahost:8080) it tries to download the file rather than displaying it.

Thanks for any help/advice.

Best

duff
duff
 
Posts: 47
Joined: Tue Oct 11, 2005 11:50 am

Postby leke » Thu Sep 12, 2013 4:01 am

Interesting, I've never seen anything like that before, but then I've never used Nginx :P
Does the error log report anything?
User avatar
leke
LXF regular
 
Posts: 506
Joined: Mon Oct 22, 2007 5:45 pm
Location: Oulu, Finland

Postby duff » Thu Sep 12, 2013 10:14 am

Hi leke

Tracked the problem down to a misconfigured virtual hosts file for the example site. I'd accidentally included a closing curly bracket where there shouldn't have been one.

i.e.

Code: Select all
server {
   listen 8080;
   listen [::]:8080 default_server ipv6only=on;
   root /var/www/example.com/public_html;
   index index.php index.html index.htm;

   # Make site accessible from http://localhost/
   server_name localhost; # since I'm only testing here

} # THIS SHOULD NOT BE HERE

   # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
   #
   location ~ \.php$ {
      fastcgi_split_path_info ^(.+\.php)(/.+)$;
   #   # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
   #
   #   # With php5-cgi alone:
   #   fastcgi_pass 127.0.0.1:9000;
   #   # With php5-fpm:
      fastcgi_pass unix:/var/run/php5-fpm.sock;
      fastcgi_index index.php;
      include fastcgi_params;
   }

} # THIS IS THE REAL CLOSING BRACKET


Thanks for your input.

Best

Iain
duff
 
Posts: 47
Joined: Tue Oct 11, 2005 11:50 am


Return to Help!

Who is online

Users browsing this forum: No registered users and 1 guest