how to install php with nginx - php

I'm new to all things related to webservers. Until now, I used apache. But I want to give a try to nginx. So I decided to install it, alongside PHP. This are the steps I followed (I mention that I use Windows 7):
download nginx from official website
download php5.6.11 from official website
I extracted nginx in C:/ drive
I extracted and copied php into nginx
I created start.bat file (inside nginx folder), with the following code:
#ECHO OFF
start C:\nginx\nginx.exe
start C:\nginx\php\php-cgi.exe -b 127.0.0.1:9000 -c C:\nginx\php\php.ini-development
ping 127.0.0.1 -n 1>NULL
echo Starting nginx
echo .
echo ..
echo ...
ping 127.0.0.1 >NUL
EXIT
I created stop.bat file (inside nginx folder), with the following code:
#ECHO OFF
taskkill /f /IM nginx.exe
taskkill /f /IM php-cgi.exe
EXIT
I edited nginx/conf/nginx.conf file as follows:
location ~ .php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME C:/nginx/html/$fastcgi_script_name;
include fastcgi_params;
}
I started nginx, with the help of start.bat
navigate to 127.0.0.1 in browser, and I got the nginx welcome page.
I created a new php file (test.php) into nginx/html/ directory, with a simple code:
But when I navigate to 127.0.0.1/test.php, it takes a long time to respond (a minute), and after that I got the following error:
------------------------------------------------------
An error occurred.
Sorry, the page you are looking for is currently unavailable.
Please try again later.
If you are the system administrator of this resource then you should check the error log for details.
Faithfully yours, nginx.
-----------------------------------------------------
If I follow the error log link, I get this screen:
Any solution? Thank you.

After downloading both setups extract them into C:// drive.
put extract php folder in nginx folder.
Now follow steps as I am doing in this video.
To configure PHP in nginx we need to make some changes in nginx.conf file.
Remove old code in nginx.conf file paste below code in nginx.conf.
here is code :
location ~ .php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME C:/nginx/html/$fastcgi_script_name;
}
To start nginx and php use below batch code:
#ECHO OFF
start C:\nginx\nginx.exe
start C:\nginx\php\php-cgi.exe -b 127.0.0.1:9000 -c C:\nginx\php\php.ini
ping 127.0.0.1 -n 1>NUL
echo Starting nginx
echo .
echo ..
echo ...
ping 127.0.0.1 >NUL
EXIT
To stop nginx and php use below batch code.
#ECHO OFF
taskkill /f /IM nginx.exe
taskkill /f /IM php-cgi.exe
Exit

Related

Where is the php7.4-fpm.sock located if I we use the image php:7.4-fpm?

Hi I pulled the image of php:7.4-fpm
FROM php:7.4-fpm
after the containers is up when I exec the the service and cd on this
root#05a7991437c0:/var/run/php#
but it is empty
I tried to exec the container and service php7.4-fpm status
it says php7.4-fpm: unrecognized service
I want to change my nginx.conf to fpm.sock
fastcgi_pass php:9000;
Thank you in advance
I needed same for creating virtual host for local nginx..
Below worked for me :
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
Make sure u remove the file your_local_domain from /etc/nginx/sites-enabled before recreating a soft link like below :
sudo ln -s /etc/nginx/sites-available/your_local_domain /etc/nginx/sites-enabled/

PHP script won't execute bash script when called from webpage (NGINX web server) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm trying to setup a web interface for some controlled plug sockets I have, the sockets are controlled by a Raspberry Pi. I want to have a couple of links on a web page that I can use to turn the switches on and off. I've been trying to use PHP to do this and for some reason it just won't work.
I've tried various suggestions (see below links). All I'm getting is a white page whenever I click the link, and it doesn't do what its supposed to i.e. turn the switch on and off. Running the PHP script from the command line works as expected, the issue seems to be only when trying to run it from the webpage.
I've looked at the permissions and for the script I've set the permissions with:
chmod 777 /path/to/script
I've tried storing the script in my home folder and in the /var/www/html folder with no joy. Nothing appears in the NGINX logs or PHP-FPM log to indicate any error.
I've tried editing the sudoers file to give www-data access to the script (www-data ALL:=/path/to/script/ and even tried it with all permissions for www-data (www-data ALL=(ALL:ALL) ALL) neither have worked.
I did think it might be because the script I'm trying to run involves starting an SSH session but I can't even get a local command to work to create a blank file either in the /home/pi/ directory or /var/www/html.
I've put the script I'm trying to run below along with the PHP I'm using to call the script and a second PHP file I've used to try other commands.
Any help or pointers in the right direction would be appreciated. I think the script is running but its failing somewhere and I can't work out where. The only thing I get back in a web browser is the echo $username line so I know its working in part but when I try to execute a command nothing happens.
PHP SCRIPT:
<?php
$username = posix_getpwuid(posix_geteuid())['name'];
echo $username;
exec("/home/pi/scripts/switch2off");
?>
TEST SCRIPT:
<?php
exec("touch /var/www/html/s/test.txt");
?>
SWITCH2OFF SCRIPT
#! /bin/bash
ssh pi#example 'python /home/pi/switches/switch_2_off.py'
NGINX CONFIG:
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
server {
listen 80;
listen [::]:80;
return 301 https://$server_name$request_uri;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name example.com;
location /.well-known/ {
allow all;
}
}
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name example.com;
include snippets/ssl-example.conf;
include snippets/ssl-params.conf;
root /var/www/html;
location / {
limit_req zone=one burst=5;
root /var/www/html;
auth_basic "Please Log In";
auth_basic_user_file /etc/nginx/.htpasswd;
proxy_set_header X-Content-Type-Options: nosniff;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
add_header X-Frame-Options "allow-from example.com";
}
location /.well-known/ {
allow all;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
server {
error_page 401 403 404 /404.html;
}
PHP-FPM LOG:
[03-Aug-2020 05:00:01] NOTICE: Terminating ...
[03-Aug-2020 05:00:01] NOTICE: exiting, bye-bye!
[03-Aug-2020 05:00:29] NOTICE: fpm is running, pid 620
[03-Aug-2020 05:00:29] NOTICE: ready to handle connections
[03-Aug-2020 05:00:29] NOTICE: systemd monitor interval set to 10000ms
MY RESEARCH/THINGS I'VE TRIED:
Nginx serves .php files as downloads, instead of executing them - I started here as initially I had a config issue when instead of running the PHP scripts it served them as a download instead.
Run a shell script with an html button - this is where I got the code from for the PHP script
PHP code is not being executed, instead code shows on the page - not quite the same issue as I'm seeing. The web browser doesn't display any code from the php file even when going to view source
https://askubuntu.com/questions/520566/why-wont-this-php-script-execute-bash-script
https://unix.stackexchange.com/questions/115054/php-shell-exec-permission-on-linux-ubuntu
https://www.linode.com/docs/web-servers/nginx/serve-php-php-fpm-and-nginx/
Thanks for all the help. I found the issue. It wasn't with PHP or NGINX. The owner on /var/www/.ssh was set to pi for some reason. I've changed it to www-data and the script has started working now from the webpage. I'm still not sure why my second script to create a file wouldn't work (probably a permissions issue) but I was experimenting and found that other commands would work (like ls) which brought me back to thinking it had to be a permissions error somewhere.
So I went back through all the scripts and folders and checked and it was the .ssh folder. A quick chown fixed the problem.
Thank you again for all your suggestions and help!

Is possible to have PHP + MongoDb + Nginx in the same Dockerfile?

I need to have the 3 services running into only one docker container. I have this "system" done in one docker-compose, but due to some limitations I need to have it into only one dockerfile. The application that I want to run is in PHP (with Symfony) + MongoDb.
Right now, I'm executing this command:
sudo docker run -p 9091:80 -p 27017:27017 myapp
And the maximum that I get is a 502 error when I browse to localhost:9091.
Thanks
Dockerfile:
FROM php:7.3-fpm-stretch
COPY --from=composer /usr/bin/composer /usr/bin/composer
RUN apt update
RUN apt install -y \
nginx \
mongodb
COPY nginx.conf /etc/nginx/nginx.conf
RUN pecl install mongodb
RUN docker-php-ext-enable mongodb
RUN service nginx start
RUN service mongodb start
EXPOSE 9091
EXPOSE 27017
COPY entrypoint.sh /
WORKDIR /usr/www
ENTRYPOINT ["/entrypoint.sh"]
COPY . /usr/www
entrypoint.sh
#!/bin/bash
set -e -u
service nginx start
service mongodb start
tail -f /dev/null
nginx.conf
events {
worker_connections 1024;
}
http {
server {
listen 80;
server_name localhost;
root /usr/www/public;
location / {
try_files $uri /index.php$is_args$args;
}
location ~ ^/index\.php(/|$) {
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
}
error_log /dev/stderr debug;
access_log /dev/stdout;
}
}
Yes, it is possible, but it is not the recommended approach in the context of docker.
Limiting each container to one process is a good rule of thumb.
Decouple applications
Each container should have only one concern. Decoupling applications into multiple containers makes it easier to scale horizontally and reuse containers. For instance, a web application stack might consist of three separate containers, each with its own unique image, to manage the web application, database, and an in-memory cache in a decoupled manner.
So better to use docker-compose and launch three container and use the docker network for communication.
It will less or more likely this
or using docker-compose
As far the issue in your current as mentioned in the comment you are not starting PHP, better to check logs of the container.
The problem with your dockerfile is you are not starting php-fpm. You shall use supervisord to manage multiple services in a single container , This document explains this use case very clearly.
You can also refer this example for the complete supervisord conf for nginx,php-fpm and mysql .
As a piece of opinion ,It is unadvisable to run multiple services in a single container .

Why is my PHP not autostarting with NginX on Windows 10?

I'm running a new install of Windows 10. I need to create a local testing environment for PHP. I've set up and run NginX/PHP servers on Ubuntu Linux before, but never on Windows. I've installed the NginX and PHP binaries for Windows.
After I've booted up and logged in, if I cd to C:\nginx and run nginx.exe, the Nginx server starts up and runs fine, and I get the "Welcome to NginX" screen at http://127.0.0.1.
Then, if I cd to C:\nginx\php and run php-cgi.exe -b 127.0.0.1:9000 -c c:/nginx/php/php.ini, the PHP server runs, and I can access http://127.0.0.1/php.info and get the output of php_info();. So it seems everything is installed okay.
My goal now, is to to have the PHP server autostart when I boot the machine. I downloaded the Non-Sucking Service Manager, and opened a command prompt in Administraror mode, and ran nssm edit nginx. I filled out the screens as follows:
Then I did the same for PHP:
However, while NginX seems to be starting at boot, PHP does not. After a boot, without manually starting anything at the command line, I get the NginX welcome screen. However, if I try to view the PHP info page, I get the following message:
Unable to connect
Firefox can’t establish a connection to the server at 127.0.0.1.
How do I get PHP to autostart on boot?
This is my nginx.conf file:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type text/html;
sendfile on;
keepalive_timeout 65;
server {
#Uncomment and edit the line below if you want to use a custom domainname
#server_name your.domain.com;
listen 80;
root c:/nginx/html;
location ~ .php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME c:/nginx/html/$fastcgi_script_name;
include fastcgi_params;
}
}
}
Your nginx.conf seems OK.
... However, while NginX seems to be starting at boot ...
I wouldn't trust NGINX welcome page, it most likely comes from cache. I also don't trust a service status Running unless there are nginx processes started by nssm.
In my tests both nginx and php services were not started properly.
I needed to set AppNoConsole=1 for both services to make them work.
According to the author(s) this is a known issue with Windows 10 Creators Update.
2017-04-26: Users of Windows 10 Creators Update should use prelease
build 2.2.4-101 to avoid an issue with services failing to start. If
for some reason you cannot use that build you can also set
AppNoConsole=1 in the registry, noting that applications which expect
a console window may behave unexpectedly.
You can change this setting via NSSM Service Editor GUI > Process > Console window. Just clear the checkbox and click Edit service. It's done.
The same operation can be done with commands too.
net stop php
net stop nginx
nssm set php AppNoConsole 1
nssm set nginx AppNoConsole 1
net start nginx
net start php

nginx doesn't parse php request

env:
ubuntu 9.10 /
use gotdeb source /
php version 5.3.1 /
php5-fpm installed /
php5-fpm is running , and listening 9000 port test by 2 commands below
pgrep php5-fpm
telnet localhost 9000
however if I visit from anthor computer in LAN ,using this address
http://192.168.1.103/index.php
the browser ask me to save this index.php , but if visit index.php , everything goes well
sites-enabled/default file content
env:
ubuntu 9.10 /
use gotdeb source /
php version 5.3.1 /
php5-fpm installed /
php5-fpm is running , and listening 9000 port test by 2 commands below
pgrep php5-fpm
telnet localhost 9000
however if I visit from anthor computer in LAN ,using this address
http://192.168.1.103/index.php
the browser ask me to save this index.php , but if visit index.html , everything goes well
sites-enabled/default file content
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000
fastcgi_index index.php
fastcgi_param SCRIPT_FILENAME /var/www/$fastcgi_script_name
include fastcgi_params
}
A common pitfall is when your code is using short open tags <? instead of <?php and it's not enabled in php.ini on a fresh install so it won't get parsed.
To enable it set short_open_tag=On
It seems you need to set your website's index.
You can see a full example on nginx's official wiki, but here's the relevant part:
location / {
index index.html index.htm index.php;
}
As you see, the virtual host needs to know it's supposed to process index.php when accessing the website via it's root dir (i.e. http://domain.com/ or http://domain.com/site/), with no file specified.
I do have an index:
location / {
index index.php;
}
I am not using <? PHP tags.

Categories