Skip to content

Instantly share code, notes, and snippets.

@mariotpc
Created January 27, 2021 01:19
Show Gist options
  • Save mariotpc/1276d421ea5d8d190465d0ff6e1ac6b0 to your computer and use it in GitHub Desktop.
Save mariotpc/1276d421ea5d8d190465d0ff6e1ac6b0 to your computer and use it in GitHub Desktop.
NGINX with PHP7
As the first step install whole packages for php7
#########################################################
apk add php7-fpm php7-mcrypt php7-soap php7-openssl php7-gmp php7-pdo_odbc php7-json php7-dom php7-pdo php7-zip php7-mysqli php7-sqlite3 php7-apcu php7-pdo_pgsql php7-bcmath php7-gd php7-odbc php7-pdo_mysql php7-pdo_sqlite php7-gettext php7-xmlreader php7-xmlrpc php7-bz2 php7-iconv php7-pdo_dblib php7-curl php7-ctype
Second:
Defining ENV variables which will be used in configuration. You can do this e.g. in /etc/profile.d/php7.sh
PHP_FPM_USER="www"
PHP_FPM_GROUP="www"
PHP_FPM_LISTEN_MODE="0660"
PHP_MEMORY_LIMIT="512M"
PHP_MAX_UPLOAD="50M"
PHP_MAX_FILE_UPLOAD="200"
PHP_MAX_POST="100M"
PHP_DISPLAY_ERRORS="On"
PHP_DISPLAY_STARTUP_ERRORS="On"
PHP_ERROR_REPORTING="E_COMPILE_ERROR\|E_RECOVERABLE_ERROR\|E_ERROR\|E_CORE_ERROR"
PHP_CGI_FIX_PATHINFO=0
after that source the system with it:
$ source /etc/profile.d/php7.sh
third
########################################
Modifying configuration file www.conf
sed -i "s|;listen.owner\s*=\s*nobody|listen.owner = ${PHP_FPM_USER}|g" /etc/php7/php-fpm.d/www.conf
sed -i "s|;listen.group\s*=\s*nobody|listen.group = ${PHP_FPM_GROUP}|g" /etc/php7/php-fpm.d/www.conf
sed -i "s|;listen.mode\s*=\s*0660|listen.mode = ${PHP_FPM_LISTEN_MODE}|g" /etc/php7/php-fpm.d/www.conf
sed -i "s|user\s*=\s*nobody|user = ${PHP_FPM_USER}|g" /etc/php7/php-fpm.d/www.conf
sed -i "s|group\s*=\s*nobody|group = ${PHP_FPM_GROUP}|g" /etc/php7/php-fpm.d/www.conf
sed -i "s|;log_level\s*=\s*notice|log_level = notice|g" /etc/php7/php-fpm.d/www.conf #uncommenting line
Fourth:
Modifying configuration file php.ini
##################################################################################
sed -i "s|display_errors\s*=\s*Off|display_errors = ${PHP_DISPLAY_ERRORS}|i" /etc/php7/php.ini
sed -i "s|display_startup_errors\s*=\s*Off|display_startup_errors = ${PHP_DISPLAY_STARTUP_ERRORS}|i" /etc/php7/php.ini
sed -i "s|error_reporting\s*=\s*E_ALL & ~E_DEPRECATED & ~E_STRICT|error_reporting = ${PHP_ERROR_REPORTING}|i" /etc/php7/php.ini
sed -i "s|;*memory_limit =.*|memory_limit = ${PHP_MEMORY_LIMIT}|i" /etc/php7/php.ini
sed -i "s|;*upload_max_filesize =.*|upload_max_filesize = ${PHP_MAX_UPLOAD}|i" /etc/php7/php.ini
sed -i "s|;*max_file_uploads =.*|max_file_uploads = ${PHP_MAX_FILE_UPLOAD}|i" /etc/php7/php.ini
sed -i "s|;*post_max_size =.*|post_max_size = ${PHP_MAX_POST}|i" /etc/php7/php.ini
sed -i "s|;*cgi.fix_pathinfo=.*|cgi.fix_pathinfo= ${PHP_CGI_FIX_PATHINFO}|i" /etc/php7/php.ini
Fifth:
To add PHP support to Nginx we should modify Nginx configuration file:
###################################################################################
vi /etc/nginx/nginx.conf
Sixth:
user www;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
sendfile on;
access_log /var/log/nginx/access.log;
keepalive_timeout 3000;
server {
listen 80;
root /www;
index index.html index.htm index.php;
server_name localhost;
client_max_body_size 32m;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/lib/nginx/html;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
}
##########################################################################
PD: In our configuration we have line: "fastcgi_pass 127.0.0.1:9000"
It should be corresponing to the line "listen = 127.0.0.1:9000" in PHP configuration file /etc/php7/php-fpm.d/www.conf
Seventh:
Set the timezome
########################################################################################
Timezone
For configuring Timezone you may use tzdata package which can be installed by running:
apk add tzdata
Timezone configuration
TIMEZONE="America/Guatemala"
cp /usr/share/zoneinfo/${TIMEZONE} /etc/localtime
echo "${TIMEZONE}" > /etc/timezone
sed -i "s|;*date.timezone =.*|date.timezone = ${TIMEZONE}|i" /etc/php7/php.ini
Eighth
Create a sample of the phpinfo() function
################################################################
vi /www/phpinfo.php
<?php
phpinfo();
?>
Nineth
##########################################################################################################
### Order the task to start, add to the runlevel default and Test it !!!!
Starting Nginx with PHP7
#####################################################
Nginx should be restarted because we have changed it's configuration. Restart it by running:
rc-service nginx restart
After the installation PHP is not running. Start it by running:
#####################################################
rc-service php-fpm7 start
Runlevel
Normally you want to start the web server when the system is launching. This is done by adding Nginx and PHP to the needed runlevel.
#####################################################
rc-update add nginx default
rc-update add php-fpm7 default
Now they should start automatically when you boot your machine next time. To test that run:
#####################################################
reboot
To make sure that Nginx and PHP are started run command:
#####################################################
ps aux | grep 'nginx\|php-fpm'
You should get something like this:
#####################################################
263 root 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
264 www 0:00 nginx: worker process
291 root 0:00 php-fpm: master process (/etc/php7/php-fpm.conf)
302 www 0:00 php-fpm: pool www
303 www 0:00 php-fpm: pool www
310 root 0:00 grep nginx\|php-fpm
Testing Nginx with PHP
This section is assuming that nginx is running and sample html page "/www/phpinfo.php" is created. Launch a web browser and point it to
#####################################################
http://X.X.X.X/phpinfo.php
where X.X.X.X is IP address of your web server
Tenth
#####################################################
Troubleshooting
If PHP is not started check php-fpm log file
#####################################################
less /var/log/php-fpm.log
Make sure that configuration files do not contain errors
#####################################################
vi /etc/php7/php-fpm.conf
vi /etc/php7/php.ini
Resulto as follows
#####################################################
http://xxx.xxx.xxx.xxx/phpinfo.php
Returns
#####################################################
PHP logo
PHP Version 7.4.14
System Linux alpine 5.10.7-0-virt #1-Alpine SMP Thu, 14 Jan 2021 07:31:13 UTC x86_64
Build Date Jan 7 2021 11:46:25
....
....
Default timezone America/Guatemala
....
PHP License
This program is free software; you can redistribute it and/or modify it under the terms of the PHP License as published by the PHP Group and included in the distribution in the file: LICENSE
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
If you did not receive a copy of the PHP license, or have any questions about PHP licensing, please contact [email protected].
#####################################################
This information comes from:
https://wiki.alpinelinux.org/wiki/Nginx_with_PHP#Configuration_of_PHP7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment