How to use nginx with PHP? - php

What is a good way of using PHP with nginx? From the finding I got, maybe using PHP-FPM might be a good way of handing PHP behind nginx.
The problem we have is that the free web based API we serve gets a lot of request (about 500K a day), the requests are mostly very short and small in size but Apache is consuming a lot of memory. I want to try nginx to see if it can handle it better.
Thanks.

Ubuntu Lucid 64-bit
apt-get install nginx
apt-get update
apt-get install php5-cli php5-common php5-suhosin
apt-get install python-software-properties
add-apt-repository ppa:brianmercer/php
apt-get update && apt-get install php5-fpm php5-cgi
/etc/init.d/nginx restart
/etc/init.d/php5-fpm restart
Edit (might need this in your site conf) :
location ~ \.php$ {
fastcgi_read_timeout 60000;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/site$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}

Related

Why does assembly of Vagrant and Xdebug not works on latest versions of PHP?

I have the working assembly of vagrant + nginx + PHP + xdebug. Everything is ok while the PHP version is 7.0, but when I upgrade PHP to 7.2 or 7.4 Xdebug stop works. Xdebug installed the latest version (3.0.1).
Vagrantfile (part):
sudo add-apt-repository -y ppa:ondrej/php
sudo add-apt-repository -y ppa:ondrej/nginx
sudo apt-get update
sudo apt-get install -y nginx
sudo apt-get install -y php7.0-fpm php7.0-xdebug
sudo service php7.0-fpm stop
sudo cp /vagrant/.provision/xdebug.ini /etc/php/7.0/mods-available/xdebug.ini
sudo service php7.0-fpm start
sudo service nginx start
sudo cp /vagrant/.provision/project.local /etc/nginx/sites-available/project.local
sudo chmod 644 /etc/nginx/sites-available/project.local
sudo ln -s /etc/nginx/sites-available/project.local /etc/nginx/sites-enabled/project.local
sudo service nginx restart
Nginx config:
server {
listen 80;
index index.php;
server_name project.local www.project.local;
root /var/www/project.local;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location / {
index index.php;
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
}
Xdebug config:
zend_extension=xdebug.so
xdebug.remote_enable=1
xdebug.remote_autostart=1
xdebug.remote_connect_back=1
xdebug.remote_host=192.168.33.11
xdebug.remote_port=9001
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_cookie_expire_time = 36000
xdebug.remote_log=/var/www/project.local/.xdebug/xdebug.log
xdebug.idekey=vagrant
These configs working on php7.0-fpm, but not working on php7.2-fpm or php7.4-fpm. Where can be the problem?
Xdebug installed the latest version (3.0.1).
You are using Xdebug v3 but keep using Xdebug v2 config parameters. You need to go through Upgrading from Xdebug 2 to 3 Guide and adjust your settings (mostly just change the parameter names).
Xdebug v3 uses different config params than Xdebug v2. From what I see 9 out of 10 "xdebug." params from your current php.ini do nothing in Xdebug v3 (if you would check Xdebug section of the phpinfo() output you would see that).
For Xdebug 3 it should be something like this (based on your original config):
zend_extension=xdebug.so
xdebug.mode = debug
xdebug.start_with_request = yes
xdebug.discover_client_host = true
xdebug.client_host = 192.168.33.11
xdebug.client_port = 9001
xdebug.log = /var/www/project.local/.xdebug/xdebug.log
xdebug.idekey = vagrant

Cannot find php-fpm www.sock file on Docker with nginx and php-fpm

I am working to start the laravel app on Docker
I am having connect() to unix:/run/php-fpm/www.sock failed (2: No such file or directory) error and there is no php-fpm folder in run folder.
Why is not there a www.sock file created?
server.conf
server {
listen 8080;
root /home/app/public_html/public;
index index.php;
charset utf-8;
gzip on;
gzip_types text/css application/javascript text/javascript application/x-javascript image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php {
include fastcgi.conf;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php-fpm/www.sock; //this crashes
}
location ~ /\.ht {
deny all;
}
}
Dockerfile
FROM centos:8
RUN dnf install nginx -y
RUN yum -y install yum-utils
RUN dnf install -y dnf-utils http://rpms.remirepo.net/enterprise/remi-release-8.rpm
RUN dnf module reset php
RUN dnf -y module enable php:remi-7.4
RUN dnf install -y php php-cli php-mysqlnd php-zip php-devel php-pdo php-gd php-mbstring php-curl php-xml php-pear php-bcmath php-json php-fileinfo php-intl
RUN yum -y install make
RUN yum -y install libmcrypt-devel
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN rpm -Uvh http://www.city-fan.org/ftp/contrib/yum-repo/rhel7/x86_64/city-fan.org-release-2-1.rhel7.noarch.rpm
RUN mkdir -p /usr/local/lib/php/pecl
RUN pecl install mongodb
RUN pecl install mcrypt-1.0.4
RUN rm -v /etc/nginx/nginx.conf
ADD nginx.conf /etc/nginx/
ADD server.conf /etc/nginx/conf.d/
COPY . /home/app/public_html/
COPY mongodb.ini /etc/php.d/
RUN cd /home/app/public_html && composer update
EXPOSE 8080
CMD ["nginx", "-g", "daemon off;"]
In your DockerFile you need add php-fpm:
RUN dnf install -y php php-fpm php-cli php-mysqlnd php-zip php-devel php-pdo php-gd php-mbstring php-curl php-xml php-pear php-bcmath php-json php-fileinfo php-intl
Hope help you.

Configuration PHP Dockerfile from ubuntu image

I tried to execute docker-compose up with php7-fpm image from docker hub and it wor'ks, so it's my docker which is bad configured, can somehone help me ?
I got a problem using docker-compose with Dockerfile image. I explain you what I need, I need my nginx server which comunicates with php. php should communicates with Mysql database.
I just use a Dockerfile to add modules to php.
Here is my php dockerfile :
FROM ubuntu:18.04
# BLOCK : Make PHP works
RUN apt-get update
RUN apt-get -y install software-properties-common
RUN add-apt-repository ppa:ondrej/php
# FIN BLOCK
RUN apt-get -y update && apt-get install -y \
php7.0 \
php7.0-fpm \
php7.0-mysql \
php7.0-json \
php7.0-curl \
php7.0-sqlite3 \
php7.0-xml \
php7.0-bcmath \
php7.0-zip \
php7.0-mbstring \
php-xdebug \
php-ast
WORKDIR /var/www/html
CMD ["php-fpm"]
EXPOSE 9000
NGINX config
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name localhost;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/html/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
When I execute docker-compose --verbose up I got an exit code
Here is --verbose and architecture file
I am using NGINX Unit as an app server for PHP since a little bit more then 2 years. It makes it easier to use PHP applications running on PHP with NGINX without installing / creating a complex docker container image by yourself.
Here is an example of an UNIT build for Wordpress. But it will work with any other PHP Application as well.
https://github.com/tippexs/unitwp
I would give it a try. The official documentation:
https://unit.nginx.org/configuration/#php
But back to your problem:
I would not using Ubuntu 14.04 anymore if not absolutley needed. Use a newer image as your base image.
What package are you using to install PHP7.0? I just used a fresh ubuntu:14.04 docker image and tried to install php7.0 without adding any special repo or ppa.
root#31bb99d337d9:/# apt search "php7"
Sorting... Done
Full Text Search... Done
Can't find any PHP7 package. I know there are some ppa's for PHP7 on Ubuntu14.04.
To make it work with my fresh Ubunut 14.04:
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php
Your PHP-Image does not include an FCGI-proccesor.
Your NGINX configuration needs an fcgi-processor listen on port 9000 in the php container. You are not installing it in your Dockerfile.
root#31bb99d337d9:/# apt search php5-fpm
Sorting... Done
Full Text Search... Done
php5-fpm/trusty-updates,trusty-security 5.5.9+dfsg-1ubuntu4.29 amd64
server-side, HTML-embedded scripting language (FPM-CGI binary)
You should try to find the fpm for php7 in your container.
The fpm needs some additional configuration in your php container as well as it need to be started on container startup.
If have the chance go into your nginx and / or php container instance and check if the fpm is running and listening on port 9000 to accept connections from the nginx container.
I did that many times in the past and migrated anything to NGINX Unit because its much easier to use it. Give it a try.

Dockerfile - ERROR: for php Cannot start service php: OCI runtime create failed: container_linux.go:348: starting container process caused "exec

I'm quite newbie with using Docker and I'm trying to create a local development environment for running either my custom php or laravel projects.
This is my folder structure
root-dir
- src // this is where my php / laravel code lives
-- info.php // file having phpinfo(); just to ensure that everything has set properly
- docker // this is where all containers and config settings live
-- php etc..
- docker-compose.yml
This is my docker/php/Dockerfile
FROM ubuntu:18.04
ENV TERM=linux
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update
RUN apt-get install -y curl zip unzip --no-install-recommends apt-utils ca-certificates
RUN apt-get install -y --no-install-recommends php7.2-fpm \
php7.2-cli \
php7.2-mysql \
php7.2-xml \
php7.2-curl \
php7.2-bcmath \
php7.2-bz2 \
php7.2-curl \
php7.2-zip \
php7.2-gd \
php7.2-gettext \
php7.2-zip \
php7.2-soap \
php7.2-odbc \
php7.2-json \
php7.2-geoip \
php7.2-igbinary \
php7.2-imagick \
php7.2-mbstring \
php7.2-msgpack \
php7.2-ssh2 \
php7.2-memcached \
php7.2-xdebug \
php7.2-intl \
php7.2-opcache \
php7.2-readline
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN apt-get clean
RUN rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/* ~/.composer
COPY php.ini /etc/php/$PHPVER/cli/php.ini
COPY php.ini /etc/php/$PHPVER/fpm/php.ini
COPY 20-xdebug.ini /etc/php/$PHPVER/cli/conf.d/20-xdebug.ini
COPY 20-xdebug.ini /etc/php/$PHPVER/fpm/conf.d/20-xdebug.ini
COPY php-fpm-startup /usr/bin/php
CMD /usr/bin/php
EXPOSE 9000
ENTRYPOINT ["/usr/bin/php"]
CMD ["--version"]
When I'm running docker-compose up -d I get this error
ERROR: for php Cannot start service php: OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \"/usr/bin/php\": permission denied": unknown
This is my nginx/default.conf file
server {
listen 80 default_server;
root /var/www/html;
index index.html index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/error.log error;
sendfile off;
client_max_body_size 100m;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
location ~ /\.ht {
deny all;
}
}
add user to your dockerfile which will execute the php binary for you something like:
....
RUN ["chmod", "+x", "/usr/bin/php"]
RUN ["chmod", "+x", "-R", "/var/log"]
EXPOSE 9000
RUN useradd -ms /bin/bash admin
USER admin
ENTRYPOINT ["/usr/bin/php"]
CMD ["--version"]
RE:chat convo
However in your concern as to why the container stopped running.
If your command finishes executing the container will terminate
If your command fails and the tty dies, the container will terminate.
In other words if your command never runs or runs and completes, as in your example
php -verson
is the code you are trying to run
when that completes the container will terminate
If you are not catching stdout you will never see this execute
and the container will terminate as if nothing happened.

Docker impossible to link nginx container with php-fpm container

Hello for my work I am doing a nginx server and php fpm server with docker, but
I do not know how to link nginx and php with fast cgi
Nginx - Docker file
FROM debian:jessie
MAINTAINER Thomas Vidal <thomas-vidal#hotmail.com>
RUN apt-get update && apt-get upgrade
RUN apt-get install -y wget
RUN wget http://nginx.org/keys/nginx_signing.key && apt-key add nginx_signing.key
RUN apt-get update && apt-get install -y nginx
RUN echo "\ndaemon off;" >> /etc/nginx/nginx.conf
RUN ln -sf /etc/nginx/conf.d /site-conf
RUN ln -sf /var/www/html /www
VOLUME ["/site-conf", "/www"]
EXPOSE 80 443
CMD nginx
Nginx - default.conf
server {
listen 80;
index index.php index.html;
server_name 192.168.99.100;
root /www;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 192.168.99.100:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Nginx - index.php
<?php phpinfo(); ?>
Php-fpm - Dockerfile
FROM debian:jessie
MAINTAINER Thomas Vidal <thomas-vidal#hotmail.com>
RUN apt-get update && apt-get upgrade
RUN apt-get install -y php5-fpm php5-cli php5-mysql php5-curl php5-mcrypt php5-gd php5-redis
RUN sed -e 's#;daemonize = yes#daemonize = no#' -i /etc/php5/fpm/php-fpm.conf
RUN sed -e 's#listen = /var/run/php5-fpm.sock#listen = [::]:9000#g' -i /etc/php5/fpm/pool.d/www.conf
EXPOSE 9000
CMD php5-fpm
What is being returned:
File not found.
Thanks for your help!

Categories