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.
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 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
I installed PHP 7 Nightly and Nginx 1.9.7 (Mainline) on my development Debian Stable:
$ curl http://nginx.org/keys/nginx_signing.key | apt-key add -
$ echo -e 'deb http://nginx.org/packages/mainline/debian/ jessie nginx\ndeb-src http://nginx.org/packages/mainline/debian/ jessie nginx' > /etc/apt/sources.list.d/nginx.list
$ echo -e 'deb http://repos.zend.com/zend-server/early-access/php7/repos ubuntu/' > /etc/apt/sources.list.d/php.list
$ apt-get -y update && time apt-get -y dist-upgrade
$ apt-get -y --force-yes install --fix-missing nginx php7-nightly
$ service nginx restart
I have this configuration file in /etc/nginx/conf.d/php.conf (default.conf uncommented). It's the original default.conf, I just uncommented the PHP fastCGI lines:
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
I didn't found any tutorial on the net how to set up Nginx Mainline with PHP 7 yet. :(
Thank You!
After taking a quick look at the packages offered by zend (the packages you are installing), you'll have to write a script for your init system (systemd) to start and manage the php-fpm process.
The binaries are installed in /usr/local/php7/sbin/php-fpm. You'll also have to modify the configuration files in /usr/local/php7/etc. The main change will be to ensure that fpm has a pool listening on address 127.0.0.1 at port 9000.
I bought a server with Digital Ocean and have been trying to setup Laravel for 2 days now. The main tutorial How to install laravel with an nginx web server and the result gives me a Apache2 Ubuntu Default Page. When I go to the IP address, it gives me a Welcome to NGINX page. I need to get this server up and running in the next few hours for a Pitch my partner and I are doing but it is not working.
Anyone know of a fix?
Also, NGINX will not restart. It says * Restarting nginx nginx [fail] and when I do nginx -t it says:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
and it gives the same result when I say sudo service nginx restart
Remove apache2 from your server and start nginx as #Sw0ut said then start nginx by typing service nginx start open your nginx.conf file located at
/etc/nginx/sites-available/default
and make suitable changes, possibly working conf would be
server {
listen 80;
server_name localhost;
root /location/to/your/www/public/folder;
index index.php index.html index.htm;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
error_log /var/log/nginx/nginx_error.log warn;
location / {
#try_files $uri $uri/ /index.php;
try_files $uri $uri/ /index.php$is_args$args;
}
#error_page 404 /404.html;
#redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/location/to/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
}
unix:/location/to/php5-fpm.sock; the location of php5-fpm in most cases will be /var/run/php5-fpm.sock;
also chmod 755 /app/storage folder if that doesn't works try 777
and restart nginx if that doesn't works stop nginx and start it
Make sure you uninstall all apache2 stuff before trying to run nginx.
sudo apt-get remove apache2 apache2-utils
sudo service nginx start
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.