Run php script with PHP-FPM - php

I have just installed nginx and php-fpm according to this
http://blog.frd.mn/install-nginx-php-fpm-mysql-and-phpmyadmin-on-os-x-mavericks-using-homebrew/
I am now visiting a php webpage and the php code is not been rendered
<?php echo 'test' ?>

Your problem is related with nginx configuration not php-fpm itself.
Nginx needs to be configured to let him now what to do with each file type. By default it is throwing files as static (the result is the raw content of the file as you report).
An basic example of nginx routing to set php-fpm as fast-cgi service for *.php files would be:
nginx.conf
server {
....
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm/DOMAINNAME.socket;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
}
}
Check this basic example: https://support.rackspace.com/how-to/install-nginx-and-php-fpm-running-on-unix-file-sockets/
If you want more advanced examples:
This nginx.conf for Symfony (app.php is the Symfony2 bootstrap file).
https://gist.github.com/leon/3019026
Lumen use case. https://gist.github.com/psgganesh/8d1790dd0c16ab5a4cde
And if you are interested in a deeper learning:
Nginx documentation for Location: http://nginx.org/en/docs/http/ngx_http_core_module.html#location

Related

Nginx fastcgi reverse proxy for a PHP API, without need to serve static files

I'm trying to setup an nginx reverse proxy, deployed as a networked image in ECS. Behind it is a PHP API, running in a separate php-fpm container listening on port 9000. The nginx docs and examples I can find seem to suggest that it has to be configured with a root path within the server block in the config.
The following is an example (from here) of the nginx config that seems to be commonly used in this scenario:
server {
listen 80;
root /var/www/html/public; # is this really needed if proxying all requests to php?
server_name _;
index index.php;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php-fpm:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
Looking at it from a performance perspective, when a request comes into an API endpoint such as /api/foobar, in this particular scenario there is no point in nginx trying to check the filesystem for an index file inside this folder, since we know for sure there is no such folder. Ideally it should bypass this unnecessary step and simply proxy the request straight to the other container that's running php-fpm, letting it deal with route matching and 404's if an unexpected route is requested.
So, my question is - given a requirement to only serve API requests via a PHP application with "virtual routes" served up via a single index.php entry point (such as Slim) with no need to serve up any static files from a "root" path - is there a way to configure nginx to just proxy all incoming requests using fastcgi_pass?

nginx location block only in conf file

I was able to put a location block inside the default.conf file and have it work as expected. It passed the correct files from the correct directory over to the php-fpm server and display the expected results.
Now, I want to move that location block to its own .conf file for maintenance and ease of leaving the main/default .conf file alone.
However, this is not working as I had hoped or expected. When I do this, nginx restarts just fine, but when I try to browse, I get a 404 error.
This is the conf file for just the location:
server {
location /myapp/ {
root /opt/myapp/htdocs;
index index.php;
location ~ \.php$ {
#root html;
fastcgi_pass 127.0.0.1:9071;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
As I stated, if I copy this exact code into the main default.conf file, then it works. But in its own conf file, it doesnt.
In the end, I want to be able to have a different conf file for each location, where each location represents a different version of my app, running on different version of php-fpm (which are already configured on their own ports).
In Apache I was able to do this using Alias and blocks, each in its own conf file. I am trying to do something similar with nginx.

Unable to get nginx/php-fpm consistently working with docker-compose and a custom wordpress install

I'm honestly just getting quite frustrated. I feel like I keep fiddling with my nginx conf, and I'll get it working, until I restart my PC or docker. All of the docker functionality seems to be working fine, I can build and docker-compose up without problem, but it seems like every time I restart my computer or docker I have to fiddle with my nginx conf again until it works. I'm using wordpress, and currently have wordpress installed to a subdirectory wp and my wp-content in a different subdirectory at content.
My nginx configuration looks like this:
server {
listen 80;
root /var/www;
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
try_files $uri $uri/index.php /index.php;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
and it was working yesterday while I was working, but after restarting the PC overnight, and trying to work on the site today, I'm getting the standard php-fpm issue FastCGI sent in stderr: "Primary script unknown". I'm just curious if anybody has a solution, or knows why I'm having this issue. I understand that the "Primary script unknown" error usually means that my SCRIPT_FILENAME param is bad, but it was working yesterday, and as far as I can tell should always point to /var/www/index.php, which is where my wordpress index file is.
Should I basically just hardcode $document_root/index.php ?
EDIT: I literally just rebuilt my docker containers with docker-compose build and everything is working as intended. So I guess this question is more associated with docker than nginx/php, but I would still love some guidance. Would there be some reason my docker containers are ephemeral and need rebuilding on boot everytime?

Errors while setting up nginx and php fpm

I'm trying to install 3 websites on a VPS running centos 6, NGINX, PHP-FPM and WordPress. I followed the instructions shared in this article :https://deliciousbrains.com/hosting-wordpress-yourself-setting-up-sites/ and i created the below configuration file in sites-available directory
server {
server_name 7symptoms.com;
access_log /var/www/html/7symptoms/logs/access.log;
error_log /var/www/html/7symptoms/logs/error.log;
root /var/www/html/7symptoms/public/;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
I tested the files using nginx test command and php-fpm is configured correctly. But when i try to access a simple hello world (hi.php) file on my website 7symptoms.com, i get 502 bad gateway or 404 file not found error. What's the problem with the above code?
You created config in "sites-available", now you should link this fie to "sites-enabled":
cd /etc/nginx/sites-enabled/ #please check that in centos this is correct path
ln -s ../sites-available/website_config_file .
Next look into log files and check, does your request are entering the correct website.
Next, if you are using nginx + php-fpm the error "502" means that php-fpm is not running or you wrote wrong path to socket. Please check that file exists: /var/run/php-fpm.sock and (using htop, top or ps) does php-fpm process is working.
Next - if you want to have 3 different websites, it is more secure to use 3 different user, and 3 different php-fpm config (for each website), it means there will be also 3 different unix sockets created.

nginx + php with drupal + codeigniter in separate folders

I have a Slicehost slice for a dev server, with nginx and PHP.
I'm trying to get drupal running on localhost/drupal and a codeigniter app running on localhost/codeigniter.
I can get one or the other to work, but not both -- the rewrite and fastcgi seem to be interfering with one another.
Does anyone know how to have /drupal and /codeigniter both working, with rewrite rules (for SEF URLs), in separate folders in my /var/www?
Cheers.
Ok, you have to create a file (no extension needed) in /etc/nginx/sites-available that represents the name of your folder/domain (ex: drupal, yoursite.com).
Here's a sample file:
server {
server_name yourdomain.com;
root /var/www/yourdomain;
index index.php;
location / {
autoindex on;
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
}
In the sample above it will actually send url rewrites to $_SERVER['REQUEST_URI']. For more nginx rewrites, you can take a look at http://wiki.nginx.org/HttpRewriteModule for more reference.
Then you want to enable it by creating a symlink of this file in your /etc/nginx/sites-enabled folder
Example: # ln -s /etc/nginx/sites-available/yoursite /etc/nginx/sites-enabled/yoursite
Then restart/reload nginx
# services nginx reload

Categories