I have a new fresh CentOS7 installed and running as a VM. I am playing with Nginx since I have being using Apache my whole life and now just for fun and learn I decide to switch to Nginx. I am following this two guides:
How to install LEMP on CentOS7 by Digital Ocean
How to install LEMP on CentOS7 by a blog named IfNotTrueThenFalse
And as part of my previous research before I get out of ideas I did read this which is not helpful at all.
Before continue I should said that I took what I need for each of them because I want to use PHP 7.0.x instead of the default that comes with CentOS 7 repos (5.4 I think).
So, this is how my config files looks like:
/etc/nginx/conf.d/default.conf
server {
listen 80;
server_name centos7.localdomain;
root /var/www/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;
}
}
/etc/php-fpm.d/www.conf
[www]
; RPM: apache Choosed to be able to access some dir as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx
...
; Set permissions for unix socket, if one is used. In Linux, read/write
; permissions must be set in order to allow connections from a web server.
; Default Values: user and group are set as the running user
; mode is set to 0660
listen.owner = nobody
listen.group = nobody
For www.conf other than those values you see in here are the default ones. The full file is shared here
I have created the file /var/www/html/index.php with nothing else than:
<?php
phpinfo();
As soon as I try the URL http://centos7.localdomain/index.php (or without the index.php) the file is downloaded instead of display it content.
Of course after all those changes I have restarted nginx and php-fpm services and check them by runing systemctl status nginx.service and systemctl status php-fpm.service
The permissions for /var/www/html are as follow:
$ ls -la /var/www/html/
total 4
drwxr-xr-x. 2 root root 22 Oct 9 20:53 .
drwxr-xr-x. 3 root root 17 Oct 9 20:24 ..
-rw-r--r--. 1 root root 18 Oct 9 20:53 index.php
This is the PHP version I am running:
$ php -v
PHP 7.0.11 (cli) (built: Sep 14 2016 08:28:52) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.11, Copyright (c) 1999-2016, by Zend Technologies
with Xdebug v2.4.1, Copyright (c) 2002-2016, by Derick Rethans
I am missing something here? If so what is it? Or what is wrong on this setup that I am playing with?
Remove "try_files $uri =404" from the last segment. This may solve your problem.
location ~ \.php$ {
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;
}
Related
I'm trying to setup Nginx as a reverse proxy for Apache, from what I've read, it allows nginx to serve static content and Apache handles the backend PHP stuff, but i cant seem to get Apache to render.
I'm on CentOS7, i installed nginx just using yum install nginx, then i installed PHP7.2 by doing the following;
yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum-config-manager --enable remi-php72
yum install php72 php72-php-fpm php72-php-mysqlnd php72-php-opcache php72-php-xml php72-php-xmlrpc php72-php-gd php72-php-mbstring php72-php-json
running php72 -v gives me
PHP 7.2.13 (cli) (built: Dec 8 2018 10:59:58) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.13, Copyright (c) 1999-2018, by Zend
Technologies
I then ran
ln -s /usr/bin/php72 /usr/bin/php
As yum installs the command as php72
I edited nginx.conf and changed the user from nginx to apache and changed the server block to;
server {
listen 80 default;
server_name 108.xxx.xxx.xxx;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
root /var/www/html;
proxy_pass http://127.0.0.1:8080/;
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
I also added /etc/nginx/conf.d/proxy.conf with the following;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
I then installed Apache2 via yum install httpd.
I have then edited the Apache2 httpd.conf file;
- Listen 80
+ Listen 127.0.0.1:8080
I also edited /etc/opt/remi/php72/php-fpm.d/www.conf and changed user and group to apache and also
listen = /var/run/php-fpm.sock
listen.owner = apache
listen.group = apache
listen.mode = 0660
These are the only changes I have made.
I added 2 files to /var/www/html, index.html and index.php ... The index.html works perfect, and when i check with browserspy, it says that it is being served by Nginx, excellent. But when I run the index.php file it displays the actual php code and doesn't render it.
I have never really worked with Apache2 before, so i am unsure of how to look for the error. When i loook in Apache2 modules directory i can't find any PHP modules
ls -lah /etc/httpd/modules/ | grep php
returns nothing at all.
Any help would be really greatful, I have been looking for a solution for days.
Thanks
Nginx can definitely execute PHP scripts without the need of proxying back to Apache.
The reason why you're seeing just the PHP code rather than the website, is because your Apache configuration likely does not have the PHP module enabled.
You can do this by running yum --enablerepo=remi install php and running service apache2 restart to restart the server with the new configuration.
Installing the base PHP packages also adds the required modules for PHP files to be executed by Apache.
This should allow your server to start executing the PHP scripts as you expect.
If you'd like to instead run your PHP website via Nginx instead, you'll need to make some slight modifications to your Nginx configuration.
Firstly, you'll need to replace your location block to use the files on your local filesystem and then point any .php file to run using PHP-FPM.
location / {
root /var/www/html;
try_files $uri $uri/ =404;
}
location ~ \.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;
}
I recently installed nginx and php7.0 on my raspberry pi 3 (Raspbian Stretch) and I couldn't configure the server configuration files. Here is information of nginx and php:
$ sudo nginx -v
nginx version: nginx/1.10.3
$ sudo php -v
PHP 7.0.30-0+deb9u1 (cli) (built: Jun 14 2018 13:50:25) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
with Zend OPcache v7.0.30-0+deb9u1, Copyright (c) 1999-2017, by Zend Technologies
Here is my server configuration file:
/etc/nginx/sites-available/example
server {
listen 80;
server_name example.com www.example.com;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
include global/restrictions.conf;
# Root
location / {
root /var/www/root/html;
index index.php index.htm index.html index.nginx-debian.html;
try_files $uri $uri/ =404;
# pass the PHP scripts to FastCGI server
include global/root.conf;
}
# WordPress
location /blog/ {
root /var/www/blog/html;
# pass the PHP scripts to FastCGI server
include global/wordpress.conf;
}
# DokuWiKi
location /wiki/ {
root /var/www/wiki/html;
index index.php index.htm index.html;
try_files $uri $uri/ =404;
# pass the PHP scripts to FastCGI server
include global/wiki.conf;
}
# NextCloud
# location /drive/ {
# root /var/www/drive/html;
# pass the PHP scripts to FastCGI server
# include global/.conf;
# }
}
(nextcloud is not configured yet.)
/etc/nginx/global/root.conf & /etc/nginx/global/wiki.conf
(Those two files have same contents)
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
/etc/nginx/global/wordpress.conf
# WordPress single site rules. Designed to be included in any
# server {} block.
# This order might seem weird - this is attempted to match last if
# rules below fail. http://wiki.nginx.org/HttpCoreModule
location /blog/ {
try_files $uri $uri/ /index.php?$args;
}
# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
# Directives to send expires headers and turn off 404 error
# logging.
location ~*
^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$
{
access_log off; log_not_found off; expires max;
}
# Uncomment one of the lines below for the appropriate caching
#plugin (if used). include global/wordpress-wp-super-cache.conf;
#include global/wordpress-w3-total-cache.conf;
# Pass all .php files onto a php-fpm/php-fcgi server.
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
# This is a robust solution for path info security issue and
# works with "cgi.fix_pathinfo = 1" in /etc/php.ini (default)
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
# fastcgi_intercept_errors on;
fastcgi_pass unix:/var/run/php7.0-fpm.sock;
}
# Deny all attempts to access hidden files such as .htaccess,
# .htpasswd, .DS_Store (Mac). Keep logging the requests to parse
# later (or to pass to firewall utilities such as fail2ban)
location ~ /\. {
deny all;
}
# Deny access to any files with a .php extension in the uploads
# directory Works in sub-directory installs and also in multisite
# network Keep logging the requests to parse later (or to pass to
# firewall utilities such as fail2ban)
location ~* /(?:uploads|files)/.*\.php$ {
deny all;
}
I configured this way because:
A. I can apply different configuration on each subdirectory. (I don't want to show wordpress' 404 page on nextcloud)
B. I can just include overlapping configuration file by including global configuration.conf
The problem is:
A. The server won't even show files in each subdirectory. It's showing nginx welcome page on root (which I removed), and won't show files under subdirectory (e.g. "example.com/wiki" which is in "/var/www/wiki/html" would just show me 404 page)
B. I don't think php is configured correctly.
Thanks in advance.
You need to install php-fpm and it will resolve your issue
Command to install php-fpm version 7.0
sudo apt-get install php7.0-fpm
I've looked through every question like this here and tried to apply the stated fixes with no success.
I'm using the wordpress:4.7.3-php7.0-fpm-alpine docker image with a separate nginx container in front of it.
When I curl wordpress I get:
File not found.
When I check the wordpress container logs, I get:
127.0.0.1 - 16/Mar/2017:06:26:24 +0000 "GET /index.php" 404
127.0.0.1 - 16/Mar/2017:06:31:27 +0000 "GET /index.php" 404
127.0.0.1 - 16/Mar/2017:06:32:16 +0000 "GET /index.php" 404
127.0.0.1 - 16/Mar/2017:06:37:17 +0000 "GET /index.php" 404
127.0.0.1 - 16/Mar/2017:06:39:09 +0000 "GET /index.php" 404
The actual nginx error is:
2017/03/16 06:26:24 [error] 17#17: *1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 10.128.0.7, server: k8wp, request
: "GET / HTTP/1.0", upstream: "fastcgi://127.0.0.1:9000"
I'm using php 7
/var/www/html # php-fpm -v
PHP 7.0.16 (fpm-fcgi) (built: Mar 3 2017 23:07:56)
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
with Zend OPcache v7.0.16, Copyright (c) 1999-2017, by Zend Technologies
My nginx config is
server {
root /app;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name _localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I'm running nginx as the user www-data:
user www-data;
According to /usr/local/etc/php-fpm.d/www.conf the user and group are uncommented and set to www-data
The error indicates that your SCRIPT_FILENAME is incorrect. Your comment:
in the wordpress container it's at /var/www/html/index.php in the
nginx container it's at /app
suggests that nginx and php-fpm are seeing a different document root.
In which case, use:
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
You can try to include include snippets/fastcgi-php.conf;. It should fix your problem.
The issue I had with this was that my wordpress install was in a subdirectory. So the /blog/ main index of wordpress would load, but none of the published blog entries or pages in deeper directories such as /blog/wp-admin/ would load.
To fix this, I added this block:
location /blog {
try_files $uri $uri/ /blog/index.php?$args;
}
This is actually explained in "Location Strategies" of the nginx wordpress recipes page.
My issue was due to serving the website's folder from within user's home folder.
Once I moved it from /home/ubuntu/my-website to /srv/my-website all worked fine.
I have a vagrant box with a LEMP (linux, nginx, mariadb, php) stack for developing a webapp and I am currently having some issues configuring nginx and php-fpm. I am using puppet and puPHPet to provision the VM.
It seems that the combo php-fpm and nginx are unable to write files (ie. log files generated, assets published by the framework (yii2) etc).
This project worked just fine using apache instead of nginx, so I would discard a programming issue and I have been following the guidelines for nginx installation and still getting messages such as "The directory is not writable by the Web process" or "Permission denied" like errors.
As recommended I set /etc/php5/fpm/php.ini the option cgi.fix_pathinfo = 0.
nginx/sites-available/site.conf
server {
listen *:80;
server_name site.dev;
client_max_body_size 128M;
root /var/www/frontend/web;
index index.php;
access_log /var/log/nginx/test.access.log;
error_log /var/log/nginx/test.error.log;
location / {
root /var/www/frontend/web;
try_files $uri $uri/ index.php /index.php$is_args$args;
}
location ~ \.php$ {
root /var/www/frontend/web;
index index.html index.htm index.php;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param APP_ENV dev;
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
}
sendfile off;
}
php-fpm/www.conf
user = www-data
group = www-data
listen-owner = www-data
listen-group = www-data
listen-mode = 0660
I've seen other posts where listen-mode is commented out, I tried both methods and setting mode to 0666 but still no luck.
I tried also setting permissions from the out and the inside of the vagrant to 0777 over the folders that I know that the framework uses to write data but still no luck.
I have a very similar project at work configured just the same that it works but this one doesn't and I can't find the difference (in fact I took config files from this project to set up the new one after the first failed attempt).
ps aux | grep php
root php-fpm: master process (/etc/php5/fpm/php-fpm.conf)
www-data php-fpm: pool www
www-data php-fpm: pool www
Directories in the different projects seem to have the same permissions number yet onw eorks and the other doesn't.
I'm completely clueless! Ideas are very welcomed!
I have this nginx vhost file
server { # php/fastcgi
listen 80;
server_name trinityplex.com www.trinity.com;
access_log /home/web/trinity_web/log/access.log;
root /home/web/trinity_web/public;
location / {
index index.html index.htm index.php;
}
}
(for domain trinityplex.com), but if I go to trinityplex.com nginx display me 502 Bad gateway and throws the index file - chrome download index.php like a normal download.
It's ridiculous I have never seen that. If I ask PHP for version it dumps
PHP 5.3.5-0.dotdeb.0 with Suhosin-Patch (cli) (built: Jan 7 2011 00:30:52)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
with Suhosin v0.9.32.1, Copyright (c) 2007-2010, by SektionEins GmbH
Have you any ideas how to fix that?
Here is an nginx cofig file
user root;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server_names_hash_bucket_size 128;
gzip on;
include /usr/local/nginx/sites-enabled/*;
}
You haven't configured PHP in the server section so PHP files will obviously be sent as plain text. How are you planning to run PHP? As FastCGI?
Update: The configuration you have shown here does still not include anything at all about PHP or FastCGI. Try something like this:
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
include fastcgi_params;
}