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
Related
I am trying to add worker_processes into my nginx.conf file on my docker-environment, but whenever I try, it fails to restart or load. I believe I'm missing some key concept here- anyway:
This is my nginx.conf file (everything works here):
server {
listen 80;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/src;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~ ^/wp-json/ {
rewrite ^/wp-json/(.*?)$ /?rest_route=/$1 last;
}
}
I have tried to add the following below the server brackets, but that results in nginx not loading. The error from enginx is is: "worker_processes" directive is not allowed here in /etc/nginx/conf.d/nginx.conf:1
:
worker_processes: 8;
server {
listen 80;
index index.php index.html;
etc. etc. etc.
This is my folder structure:
And this is currently my Dockerfile:
FROM php:7.4.15-fpm
# Arguments defined in docker-compose.yml
ARG user
ARG uid
# Install system dependencies
RUN apt-get update && apt-get install -y \
git \
curl \
libpng-dev \
libonig-dev \
libxml2-dev \
zip \
unzip
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install PHP extensions
RUN docker-php-ext-install mysqli pdo_mysql mbstring exif pcntl bcmath g
# Set working directory
WORKDIR /var/www
USER $user
Your config located at /etc/nginx/conf.d/nginx.conf is likely being imported by the default configuration file /etc/nginx/nginx.conf with the statement include /etc/nginx/conf.d/*.conf;.
As such your config lives within the http context, which does not allow the worker_processes directive. You need to edit the existing statement that should already be defined in /etc/nginx/nginx.conf, by default it should be set to worker_processes auto;.
Note: There is no colon required so just change this to worker_processes 8;
An answer for the same root problem is https://stackoverflow.com/a/41681414/11296166
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(); ?>
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;
}
}
Trying to setup docker with nginx and php5. Here's my Dockerfile
FROM nginx:1.9.9
RUN apt-get update
RUN apt-get -y install php5-fpm php5-mysql php-apc php5-imagick\
php5-imap php5-mcrypt php5-curl php5-cli php5-gd php5-pgsql\
php5-common php-pear curl php5-json
ADD index.html /usr/share/nginx/html/index.html
ADD index.php /usr/share/nginx/html/index.php
ADD default /etc/nginx/sites-available/default
RUN /etc/init.d/php5-fpm restart
I am able to build this just fine with the following command:
sudo docker build -t myuser/nginx-php5:0.1 .
And then I launch it as follows:
sudo docker run -d -P myuser/nginx-php5:0.1
The nginx deamon is running, I can indeed see my index.html, yet, index.php gets 'downloaded' rather than served. This indicates that PHP is not properly set...
My default file:
server {
listen 80;
root /var/www;
index index.php index.html;
server_name localhost;
access_log /var/log/nginx/localhost.com-access.log;
error_log /var/log/nginx/localhost.com-error.log error;
charset utf-8;
location / {
try_files $uri $uri/ /index.html /index.php?$query_string;
}
location = /favicon.ico { log_not_found off; access_log off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
include fastcgi.conf;
fastcgi_param HTTPS off;
}
location ~ /\.ht {
deny all;
}
}
Any ideas what am I doing wrong? I guess the problem is that I am not doing the proper things to keep php5-fpm running.. but how to fix this?
I think php-fpm didn't run, so nginx just returns php file instead of passing to php-fpm to process.
You should create an "entrypoint.sh" file to launch nginx and php-fpm when run the container. Besides you must correctly set user and group for php-fpm worker processes.
I install a LEMP server in ubuntu 12.04 LTS 64
whit HHVM Fastcgi Service
and i install laravel via laravel.phar ( and test via composer too )
when in get my site in brwoser do not display any error but in chrome developer console get error 500
i can't see any error in error.log file ( laravel - hhvm , nginx )
the storage directory Permissions is 777
and my nginx.conf and vhosts file have basic configuration
when i use PHP CLI or hhvm command it's worked good
thanks for help me :)
my location block
location ~ \.(hh|php)$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_keep_conn on;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
The problem with HHVM is it doesn't show much error, You have to keep watching the HHVM or Laravel error logs.
You'll want to pay close attention to your error logs. HHVM doesn't
report errors to the browser by default.
Check the HHVM logs!
$ tail -n 50 -f /var/log/hhvm/error.log
Check your Laravel logs!
$ tail -n 50 -f /path/to/laravel/app/storage/logs/laravel.log
config reference
Create a file /etc/nginx/hhvm.conf if it doesn't exist yet. Insert the ff:
location ~ \.(hh|php)$ {
fastcgi_keep_conn on;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
Then include it on your nginx virtual host config.
eg. /etc/nginx/sites-available/laravel
Now add this for Laravel, edit as needed:
server {
listen 80 default_server;
root /vagrant/laravel/public;
index index.html index.htm index.php;
server_name localhost;
access_log /var/log/nginx/localhost.laravel-access.log;
error_log /var/log/nginx/locahost.laravel-error.log error;
charset utf-8;
location / {
try_files \$uri \$uri/ /index.php?\$query_string;
}
location = /favicon.ico { log_not_found off; access_log off; }
location = /robots.txt { log_not_found off; access_log off; }
error_page 404 /index.php;
include hhvm.conf; # INCLUDE HHVM HERE
# Deny .htaccess file access
location ~ /\.ht {
deny all;
}
}
Then reload Nginx:
$ sudo service nginx reload
Since the X-Powered-By header is set by HHVM I assume your NGINX is configured correct. A 500 error mostly comes from a syntax error or an exception thrown in your application. Maybe your fastcgi settings in NGINX are still wrong. What's inside the location *\.php block?
Try for a less error-prone setup and run php artisan serve to locally host your project.
You can modify Laravel's handle exception class to display the errors while HHVM is being used.
Full details here: https://github.com/laravel/framework/issues/8744#issue-76454458
I have tested this and It works well on Laravel 5.2/5.3 on Homestead with HHVM.