How to configure basic FastCGI caching on nginx (Ubuntu 16.04) - php

I'm trying to get basic FastCGI caching to work, but am running into trouble after following this tutorial.
On a fresh installation of Ubuntu 16.04, I ran the following commands:
apt-get update
apt-get install -y nginx
apt-get install -y php-fpm
I then changed /etc/nginx/sites-available/default to:
fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=MYAPP:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
include snippets/fastcgi-php.conf;
# fastcgi_cache MYAPP;
# fastcgi_cache_valid 200 60m;
}
}
After adding a file called time.php in the server document root (/var/www/html) with the following contents:
<?php echo time();
and navigating IP/time.php, the file executes and displays a timestamp. On reloads, fresh timestamps are displayed.
If I uncomment the above lines, only a blank screen with <html><body></body></html> loads.
Why does adding:
fastcgi_cache MYAPP;
fastcgi_cache_valid 200 60m;
result in a blank html page? How can this be fixed to cache time.php and serve the cached version to future requests?
Note: I did see that /etc/nginx/cache was created and does contain data. I changed the directory to 777 permissions to eliminate permission issues.

$ sudo mkdir -p /var/cache/nginxfastcgi
$ chown www-data: /var/cache/nginxfastcgi
THEN IN YOUR CONFIG
fastcgi_cache_path /var/cache/nginxfastcgi levels=1:2 keys_zone=fastcgicache:10m inactive=10m max_size=64m;
fastcgi_cache_key $scheme$request_method$host$request_uri;
fastcgi_cache_lock on;
fastcgi_cache_use_stale error timeout invalid_header updating http_500;
fastcgi_cache_valid 5m;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
server {
listen 80;
root **************;
index index.php index.html index.htm;
server_name *************;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
add_header X-Cache $upstream_cache_status;
fastcgi_cache fastcgicache;
}
}

Related

How to Configure Nginx to Execute PHP

This is config of nginx.conf
http {
include mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root /Users/*name*/PHPSITE/standard;
index index.html index.htm;
try_files $uri $uri/ /index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /Users/*name*/PHPSITE/standard;
}
location ~ \.php$ {
include /usr/local/etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index /index.php;
fastcgi_param SCRIPT_FILENAME /Users/*name*/PHPSITE/standard$fastcgi_script_name;
}
}
But when I'm running on 127.0.0.1:9000 it doesn't work. On localhost it shows 403 Forbidden. As you can see, I deleted commented lines.
You should always use $document_root instead providing root path everywhere
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
And check if your php-fpm process is running, for example if you are using systemctl run
systemctl status php-fpm
systemctl start php-fpm
also check if you are using same fast_cgi path, it should match in www.conf of php-fpm file ( could be here /etc/php-fpm.d/www.conf)
listen = 127.0.0.1:9000
and your site config nginx.conf file
fastcgi_pass 127.0.0.1:9000;
after updating files restart your Nginx/php-fpm services.
and than visit http://localhost (as you set server_name above) in your browser , not 127.0.0.1:9000.

Resource interpreted as Stylesheet but transferred with MIME (localhost vs domain)

I am running my php code in docker with Nginx and fastcgi
I am exposing port 9999 of my docker container
So to invoke my application I do:
http://localhost:9999/index.php
I also have a localdomain: example.com which is pointing to 127.0.0.1 in /etc/hosts.
So I can invoke my application using example.com:9999/index.php
Strange thing I noticed is, when I use localhost url, I am getting an error on my browser
Resource interpreted as Stylesheet but transferred with MIME type application/octet-stream: "http://localhost:9999/css/login.css?v=1".
I don't get the above error if I inovke it using example.com
Can somebody explain why is the strange behavior happening?
The nginx config is as follows:
events {
multi_accept on;
worker_connections 65535;
}
http {
# MIME
default_type application/octet-stream;
include /etc/nginx/mime.types;
# logging
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log warn;
server {
listen 80 default_server;
server_name _;
index index.php;
root /code/Public;
location / {
try_files $uri /index.php$is_args$args;
}
location ~ \.php {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
}
}
}
This is my Dockerfile config:
FROM phpdockerio/php73-fpm
RUN apt-get update
RUN apt-get install -y php7.3-memcache
RUN apt-get install -y php7.3-curl
RUN apt-get install -y nginx
ADD docker/nginx.conf /etc/nginx/
CMD ["sh", "-c", "service nginx restart ; /usr/sbin/php-fpm7.3 -O"]
FYI: I am using slim-framework. here is the reference doc for setting up nginx

Nginx not running php files after pear install

I installed Nginx & PHP using this guide here:
Nginx install guide
yum install php php-mysql php-fpm
edited /etc/php-fpm.d/www.conf
listen = /var/run/php-fpm/php-fpm.sock
listen.owner = nobody
listen.group = nobody
listen.owner = nobody
listen.group = nobody
ran:
systemctl start php-fpm
/etc/nginx/conf.d/default.conf
server {
listen 80;
server_name <my servers IP here - removed>;
# 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;
}
}
I then tested
http://<my server ip>/phpinfo.php
and it worked!
I then installed pear
yum install php-pear
but now php files don't work, the browser tries to download them instead of running them.
I've tried:
rebooting the server
restarting nginx
restarting php-fpm
checking all the config files to make sure they as the same as above.
I'm completely stuck. I don't know what to check to get php work again. This is the first time I've installed Nginx. I've looked around on the net for answers and on here.
I'm running Centos 7
help :)
UPDATE:
I've tried a much more shortened config file:
server {
listen 80;
server_name <my servers IP here - removed>;
# note that these lines are originally from the "location /" block
root /usr/share/nginx/html;
index index.php index.html index.htm;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
but that didn't work either.
I also tried:
fastcgi_pass 127.0.0.1:9000;
instead of:
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
but that didn't work too.
UPDATE 2
i found out that if I go to:
http://my ip/phpinfo.php - it works!
but if I go to:
http://domainname/phpinfo.php - it tries to download the PHP file instead of running it.
How do I make php files run while using the domain name instead of the IP?
According your second update, you have problem with Listen directive. nginx listen only IP-address or Domain name that mentioned in Listen directive. Maximillian gives you right answer. It you put listen 80 in your configuration file, you'll solve your problem, but there would be wotk only one site, with this listen. If you would like to configure PHP on your domain you could configure listen yourdomain.com:80.
Have you tried to edit
/etc/nginx/sites-enabled/default ?
Then you need to edit it as follows.
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.php index.html index.htm;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
afterward, you have to restart php-fpm and nginx
service nginx restart && service php-fpm restart
Try with below config for location,
location ~ .php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html/$fastcgi_script_name;
include fastcgi_params;
}
And do clear cache beforehand.
For your 2nd update try below in server block,
listen 127.0.0.1:8000; #your ip with port
server_name example.com; #domain name
Recently I also need to setup linux and php environment, so I strictly follow your step to setup it.
I encounter this two issue when I setup the LEMP environment,
a. nginx issue, post and answer
b. php-fpm issue, post and answer
I don't encounter the issue you face, so I show the environment for you, you may compare with yours.
Centos Version
vi /etc/centos-release
CentOS Linux release 7.3.1611 (Core)
Mysql version
mysql -u root -p
//enter password
Server version: 5.5.52-MariaDB MariaDB Server
Php version
php -v
PHP 5.4.16 (cli) (built: Nov 6 2016 00:29:02)
Pear info
pear list // refer to this post
Installed packages, channel pear.php.net:
=========================================
Package Version State
Archive_Tar 1.3.11 stable
Console_Getopt 1.3.1 stable
PEAR 1.9.4 stable
Structures_Graph 1.0.4 stable
XML_Util 1.2.1 stable
Nginx config file
vi /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name 192.168.236.129;
# 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;
}
}
info.php vi /usr/share/nginx/html/info.php
<?php phpinfo(); ?>

Creating new Lumen app on nginx throwing 404 when hiting IP Adress through browser

I am trying to create a plain Lumen application on Nginx server on Digital Ocean. Following the official documentation for Laravel, I followed everything as it is said until the Laravel Installation bit.
Just instead of this
composer create-project laravel/laravel /var/www/lumen/ 4.1
I used
composer create-project --prefer-dist laravel/lumen blog` instead.
The Lumen document seems on the directory now when I use ls.
However when I hit the IP address, or IPAdress/index.php, it shows
404 Not Found - nginx/1.4.6 (Ubuntu)
Also, this is my configuration for Virtual Host file (nano /etc/nginx/sites-available/default)`:
server {
listen 80 default_server;
root /var/www/laravel/public/;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
# pass the PHP scripts to FastCGI server listening on /var/run/php5-fpm.sock
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
What am I doing wrong or missing?
Also, if I try connect to the SSH directory with PhpStorm, I received an error:
ssh://root#46.101.172.134:22null /usr/bin/php
env: /var/www/Lumen: Permission denied
Process finished with exit code 126
Please also check this question for further info on this error..
Edit 2:
I tried changing my server to..
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
listen 443 ssl;
root /var/www/lumen/public;
index index.php index.html index.htm;
server_name IPAddress;
ssl_certificate /etc/nginx/ssl/nginx.crt; // I didn't set this
ssl_certificate_key /etc/nginx/ssl/nginx.key; // and this
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I used this guide to create an SSL Certificate on nginx. Then, hitting http://ipaddress gives me the Lumen/Laravel stuff.

Can't get Baikal running in a subdirectory

I try to install Baïkal on a dedicated host with the "regular package". I am using Nginx as webserver but I can't get it running. The official docs are only dedicated to run Baikal on a subdomain (http://baikal.mydomain.com) instead in a subdirectory (http://mydomain.com/baikal). When I open http://mydomain.com/baikal/card.php/addressbooks/IstMe/default/ I only get a "File not found". Any help would be appreciated.
My nginx.conf looks like this one:
location /baikal {
alias /usr/share/webapps/baikal/html;
index index.php;
rewrite ^/.well-known/caldav /cal.php redirect;
rewrite ^/.well-known/carddav /card.php redirect;
location ~ ^/baikal/(.+\.php)$ {
alias /usr/share/webapps/baikal/html/$1;
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi.conf;
}
}
location ~* /baikal/(\.ht|Core|Specific) {
deny all;
return 404;
}
I'd got the same problem. The folowing very simple instance configuration from this article worked great for me:
server {
listen [::]:443 ssl;
server_name yourdomain.tld;
root /usr/share/nginx/baikal/html;
index index.php;
ssl_certificate server.crt;
ssl_certificate_key server.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
rewrite ^/.well-known/caldav /cal.php redirect;
rewrite ^/.well-known/carddav /card.php redirect;
charset utf-8;
location ~ /(\.ht|Core|Specific) {
deny all;
return 404;
}
location ~ ^(.+\.php)(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
quite an old post but i've been redirected here searching for a solution to the very same problem ^^
the're's a post about this issue and a possible solution.
here is the configuration for NGINX (this is cut&paste, it is not my work):
location ^~ /baikal { # triggers location of baikal installation, and stop looking for other matches
index index.php;
charset utf-8;
# curiosity killed the cat
location ~ ^/baikal/(?:\.ht|Core|Specific) {
deny all;
}
# this corresponds to the recommended regex for matching php files
# and piping it to php-fpm
location ~ ^(.+\.php)(.*) {
try_files $fastcgi_script_name =404;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
}
# case insensitive matching of static files for maximum caching time
location ~* \.(?:jpg|gif|ico|png|css|js|svg)$ {
expires max; add_header Cache-Control public;
}
}
i use apache so i had no way to test it but this is the starting point i'm using to solve the problem on my web server.
Have you tried to create 2 symlinks from root to html directory like that :
cd /var/www/baikal
sudo ln -s html/card.php card.php
sudo ln -s html/cal.php cal.php
Which should gave that result :
ls -lah /var/www/baikal
total 72K
drwxrwxr-x 6 www-data www-data 4,0K nov. 19 12:40 .
drwxr-xr-x 25 www-data www-data 4,0K nov. 19 12:54 ..
lrwxrwxrwx 1 root root 12 nov. 19 12:40 cal.php -> html/cal.php
lrwxrwxrwx 1 root root 13 nov. 19 12:40 card.php -> html/card.php
This seems to work for my installation.

Categories