Nginx with PHP: Difference between revisions

From Alpine Linux
No edit summary
m (Fixed reference to php 7 in php 8.2 section)
 
(One intermediate revision by the same user not shown)
Line 2: Line 2:
{{:Nginx}}
{{:Nginx}}


== PHP5 Installation ==
== PHP8.2 Installation ==
PHP packages is available in the Alpine Linux repositories. To install php5 with modules run:
PHP packages is available in the Alpine Linux repositories. To install php8.2 with modules run:
{{cmd|apk add php5-fpm php5-mcrypt php5-soap php5-openssl php5-gmp php5-pdo_odbc php5-json php5-dom php5-pdo php5-zip php5-mysql php5-mysqli php5-sqlite3 php5-apcu php5-pdo_pgsql php5-bcmath php5-gd php5-xcache php5-odbc php5-pdo_mysql php5-pdo_sqlite php5-gettext php5-xmlreader php5-xmlrpc php5-bz2 php5-memcache php5-mssql php5-iconv php5-pdo_dblib php5-curl php5-ctype}}
{{cmd|apk add php82-fpm php82-soap php82-openssl php82-gmp php82-pdo_odbc php82-json php82-dom php82-pdo php82-zip php82-mysqli php82-sqlite3 php82-apcu php82-pdo_pgsql php82-bcmath php82-gd php82-odbc php82-pdo_mysql php82-pdo_sqlite php82-gettext php82-xmlreader php82-bz2 php82-iconv php82-pdo_dblib php82-curl php82-ctype}}


Perhaps you do not need all these PHP modules. Install modules according to your needs.
Perhaps you do not need all these PHP modules. Install modules according to your needs.


=== Configuration of PHP5 ===
=== Configuration of PHP8.2 ===


Defining ENV variables which will be used in configuration. You can do this e.g. in [https://stackoverflow.com/questions/35325856/where-to-set-system-default-environment-variables-in-alpine-linux /etc/profile.d/php5.sh].
Defining ENV variables which will be used in configuration. You can do this e.g. in [https://stackoverflow.com/questions/35325856/where-to-set-system-default-environment-variables-in-alpine-linux /etc/profile.d/php82.sh].
{{cmd|<nowiki>PHP_FPM_USER="www"
{{cmd|<nowiki>PHP_FPM_USER="www"
PHP_FPM_GROUP="www"
PHP_FPM_GROUP="www"
Line 24: Line 24:
Modify variables according to your needs.
Modify variables according to your needs.


Modifying configuration file php-fpm.conf
Modifying configuration file www.conf
{{cmd|<nowiki>sed -i "s|;listen.owner\s*=\s*nobody|listen.owner = ${PHP_FPM_USER}|g" /etc/php5/php-fpm.conf
{{cmd|<nowiki>sed -i "s|;listen.owner\s*=\s*nobody|listen.owner = ${PHP_FPM_USER}|g" /etc/php82/php-fpm.d/www.conf
sed -i "s|;listen.group\s*=\s*nobody|listen.group = ${PHP_FPM_GROUP}|g" /etc/php5/php-fpm.conf
sed -i "s|;listen.group\s*=\s*nobody|listen.group = ${PHP_FPM_GROUP}|g" /etc/php82/php-fpm.d/www.conf
sed -i "s|;listen.mode\s*=\s*0660|listen.mode = ${PHP_FPM_LISTEN_MODE}|g" /etc/php5/php-fpm.conf
sed -i "s|;listen.mode\s*=\s*0660|listen.mode = ${PHP_FPM_LISTEN_MODE}|g" /etc/php82/php-fpm.d/www.conf
sed -i "s|user\s*=\s*nobody|user = ${PHP_FPM_USER}|g" /etc/php5/php-fpm.conf
sed -i "s|user\s*=\s*nobody|user = ${PHP_FPM_USER}|g" /etc/php82/php-fpm.d/www.conf
sed -i "s|group\s*=\s*nobody|group = ${PHP_FPM_GROUP}|g" /etc/php5/php-fpm.conf
sed -i "s|group\s*=\s*nobody|group = ${PHP_FPM_GROUP}|g" /etc/php82/php-fpm.d/www.conf
sed -i "s|;log_level\s*=\s*notice|log_level = notice|g" /etc/php5/php-fpm.conf #uncommenting line </nowiki>}}
sed -i "s|;log_level\s*=\s*notice|log_level = notice|g" /etc/php82/php-fpm.d/www.conf #uncommenting line </nowiki>}}


Modifying configuration file php.ini
Modifying configuration file php.ini
{{cmd|<nowiki>sed -i "s|display_errors\s*=\s*Off|display_errors = ${PHP_DISPLAY_ERRORS}|i" /etc/php5/php.ini
{{cmd|<nowiki>sed -i "s|display_errors\s*=\s*Off|display_errors = ${PHP_DISPLAY_ERRORS}|i" /etc/php82/php.ini
sed -i "s|display_startup_errors\s*=\s*Off|display_startup_errors = ${PHP_DISPLAY_STARTUP_ERRORS}|i" /etc/php5/php.ini
sed -i "s|display_startup_errors\s*=\s*Off|display_startup_errors = ${PHP_DISPLAY_STARTUP_ERRORS}|i" /etc/php82/php.ini
sed -i "s|error_reporting\s*=\s*E_ALL & ~E_DEPRECATED & ~E_STRICT|error_reporting = ${PHP_ERROR_REPORTING}|i" /etc/php5/php.ini
sed -i "s|error_reporting\s*=\s*E_ALL & ~E_DEPRECATED & ~E_STRICT|error_reporting = ${PHP_ERROR_REPORTING}|i" /etc/php82/php.ini
sed -i "s|;*memory_limit =.*|memory_limit = ${PHP_MEMORY_LIMIT}|i" /etc/php5/php.ini
sed -i "s|;*memory_limit =.*|memory_limit = ${PHP_MEMORY_LIMIT}|i" /etc/php82/php.ini
sed -i "s|;*upload_max_filesize =.*|upload_max_filesize = ${PHP_MAX_UPLOAD}|i" /etc/php5/php.ini
sed -i "s|;*upload_max_filesize =.*|upload_max_filesize = ${PHP_MAX_UPLOAD}|i" /etc/php82/php.ini
sed -i "s|;*max_file_uploads =.*|max_file_uploads = ${PHP_MAX_FILE_UPLOAD}|i" /etc/php5/php.ini
sed -i "s|;*max_file_uploads =.*|max_file_uploads = ${PHP_MAX_FILE_UPLOAD}|i" /etc/php82/php.ini
sed -i "s|;*post_max_size =.*|post_max_size = ${PHP_MAX_POST}|i" /etc/php5/php.ini
sed -i "s|;*post_max_size =.*|post_max_size = ${PHP_MAX_POST}|i" /etc/php82/php.ini
sed -i "s|;*cgi.fix_pathinfo=.*|cgi.fix_pathinfo= ${PHP_CGI_FIX_PATHINFO}|i" /etc/php5/php.ini</nowiki>}}
sed -i "s|;*cgi.fix_pathinfo=.*|cgi.fix_pathinfo= ${PHP_CGI_FIX_PATHINFO}|i" /etc/php82/php.ini</nowiki>}}


To add PHP support to Nginx we should modify Nginx configuration file:
To add PHP support to Nginx we should modify Nginx configuration file:
Line 64: Line 64:
         listen                  80;
         listen                  80;
         root                    /www;
         root                    /www;
         index                  index.html index.htm index.php;
         index                  index.php index.html index.htm;
         server_name            localhost;
         server_name            localhost;
         client_max_body_size    32m;
         client_max_body_size    32m;
Line 80: Line 80:
</pre>
</pre>
In our configuration we have line: "fastcgi_pass  127.0.0.1:9000" <br />
In our configuration we have line: "fastcgi_pass  127.0.0.1:9000" <br />
It should be corresponing to the line "listen = 127.0.0.1:9000" in PHP configuration file /etc/php5/php-fpm.conf
It should be corresponing to the line "listen = 127.0.0.1:9000" in PHP configuration file /etc/php82/php-fpm.d/www.conf


=== Timezone ===
=== Timezone ===
Line 90: Line 90:
cp /usr/share/zoneinfo/${TIMEZONE} /etc/localtime
cp /usr/share/zoneinfo/${TIMEZONE} /etc/localtime
echo "${TIMEZONE}" > /etc/timezone
echo "${TIMEZONE}" > /etc/timezone
sed -i "s|;*date.timezone =.*|date.timezone = ${TIMEZONE}|i" /etc/php5/php.ini</nowiki>}}
sed -i "s|;*date.timezone =.*|date.timezone = ${TIMEZONE}|i" /etc/php82/php.ini</nowiki>}}


=== Sample PHP page ===
=== Sample PHP page ===
Line 100: Line 100:
</pre>
</pre>


=== Starting Nginx with PHP5 ===
=== Starting Nginx with PHP82 ===
Nginx should be restarted because we have changed it's configuration. Restart it by running:
Nginx should be restarted because we have changed it's configuration. Restart it by running:
{{cmd|rc-service nginx restart}}
{{cmd|rc-service nginx restart}}


After the installation PHP is not running. Start it by running:
After the installation PHP is not running. Start it by running:
{{cmd|rc-service php-fpm start}}
{{cmd|rc-service php-fpm82 start}}


=== Runlevel ===
=== 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.
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.
{{cmd|rc-update add nginx default
{{cmd|rc-update add nginx default
rc-update add php-fpm default}}
rc-update add php-fpm82 default}}


Now they should start automatically when you boot your machine next time. To test that run:
Now they should start automatically when you boot your machine next time. To test that run:
Line 122: Line 122:
   263 root      0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
   263 root      0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
   264 www        0:00 nginx: worker process
   264 www        0:00 nginx: worker process
   291 root      0:00 php-fpm: master process (/etc/php5/php-fpm.conf)
   291 root      0:00 php-fpm: master process (/etc/php82/php-fpm.conf)
   302 www        0:00 php-fpm: pool www
   302 www        0:00 php-fpm: pool www
   303 www        0:00 php-fpm: pool www
   303 www        0:00 php-fpm: pool www
Line 143: Line 143:


Make sure that configuration files do not contain errors
Make sure that configuration files do not contain errors
{{cmd|vi /etc/php5/php-fpm.conf
{{cmd|vi /etc/php82/php-fpm.conf
vi /etc/php5/php.ini}}
vi /etc/php82/php.ini}}


== PHP7 Installation ==
== PHP7 Installation ==
Line 289: Line 289:
{{cmd|vi /etc/php7/php-fpm.conf
{{cmd|vi /etc/php7/php-fpm.conf
vi /etc/php7/php.ini}}
vi /etc/php7/php.ini}}
== PHP5 Installation ==
PHP packages is available in the Alpine Linux repositories. To install php5 with modules run:
{{cmd|apk add php5-fpm php5-mcrypt php5-soap php5-openssl php5-gmp php5-pdo_odbc php5-json php5-dom php5-pdo php5-zip php5-mysql php5-mysqli php5-sqlite3 php5-apcu php5-pdo_pgsql php5-bcmath php5-gd php5-xcache php5-odbc php5-pdo_mysql php5-pdo_sqlite php5-gettext php5-xmlreader php5-xmlrpc php5-bz2 php5-memcache php5-mssql php5-iconv php5-pdo_dblib php5-curl php5-ctype}}
Perhaps you do not need all these PHP modules. Install modules according to your needs.
=== Configuration of PHP5 ===
Defining ENV variables which will be used in configuration. You can do this e.g. in [https://stackoverflow.com/questions/35325856/where-to-set-system-default-environment-variables-in-alpine-linux /etc/profile.d/php5.sh].
{{cmd|<nowiki>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</nowiki>}}
Modify variables according to your needs.
Modifying configuration file php-fpm.conf
{{cmd|<nowiki>sed -i "s|;listen.owner\s*=\s*nobody|listen.owner = ${PHP_FPM_USER}|g" /etc/php5/php-fpm.conf
sed -i "s|;listen.group\s*=\s*nobody|listen.group = ${PHP_FPM_GROUP}|g" /etc/php5/php-fpm.conf
sed -i "s|;listen.mode\s*=\s*0660|listen.mode = ${PHP_FPM_LISTEN_MODE}|g" /etc/php5/php-fpm.conf
sed -i "s|user\s*=\s*nobody|user = ${PHP_FPM_USER}|g" /etc/php5/php-fpm.conf
sed -i "s|group\s*=\s*nobody|group = ${PHP_FPM_GROUP}|g" /etc/php5/php-fpm.conf
sed -i "s|;log_level\s*=\s*notice|log_level = notice|g" /etc/php5/php-fpm.conf #uncommenting line </nowiki>}}
Modifying configuration file php.ini
{{cmd|<nowiki>sed -i "s|display_errors\s*=\s*Off|display_errors = ${PHP_DISPLAY_ERRORS}|i" /etc/php5/php.ini
sed -i "s|display_startup_errors\s*=\s*Off|display_startup_errors = ${PHP_DISPLAY_STARTUP_ERRORS}|i" /etc/php5/php.ini
sed -i "s|error_reporting\s*=\s*E_ALL & ~E_DEPRECATED & ~E_STRICT|error_reporting = ${PHP_ERROR_REPORTING}|i" /etc/php5/php.ini
sed -i "s|;*memory_limit =.*|memory_limit = ${PHP_MEMORY_LIMIT}|i" /etc/php5/php.ini
sed -i "s|;*upload_max_filesize =.*|upload_max_filesize = ${PHP_MAX_UPLOAD}|i" /etc/php5/php.ini
sed -i "s|;*max_file_uploads =.*|max_file_uploads = ${PHP_MAX_FILE_UPLOAD}|i" /etc/php5/php.ini
sed -i "s|;*post_max_size =.*|post_max_size = ${PHP_MAX_POST}|i" /etc/php5/php.ini
sed -i "s|;*cgi.fix_pathinfo=.*|cgi.fix_pathinfo= ${PHP_CGI_FIX_PATHINFO}|i" /etc/php5/php.ini</nowiki>}}
To add PHP support to Nginx we should modify Nginx configuration file:
{{cmd|vi /etc/nginx/nginx.conf}}
<pre>
user                            www;
worker_processes                1;
error_log                      /var/log/nginx/error.log warn;
pid                            /var/run/nginx/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;
        }
    }
}
</pre>
In our configuration we have line: "fastcgi_pass  127.0.0.1:9000" <br />
It should be corresponing to the line "listen = 127.0.0.1:9000" in PHP configuration file /etc/php5/php-fpm.conf
=== Timezone ===
For configuring Timezone you may use tzdata package which can be installed by running:
{{cmd|apk add tzdata}}
Timezone configuration
{{cmd|<nowiki>TIMEZONE="Europe/Helsinki"
cp /usr/share/zoneinfo/${TIMEZONE} /etc/localtime
echo "${TIMEZONE}" > /etc/timezone
sed -i "s|;*date.timezone =.*|date.timezone = ${TIMEZONE}|i" /etc/php5/php.ini</nowiki>}}
=== Sample PHP page ===
{{cmd|vi /www/phpinfo.php}}
<pre>
<?php
phpinfo();
?>
</pre>
=== Starting Nginx with PHP5 ===
Nginx should be restarted because we have changed it's configuration. Restart it by running:
{{cmd|rc-service nginx restart}}
After the installation PHP is not running. Start it by running:
{{cmd|rc-service php-fpm 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.
{{cmd|rc-update add nginx default
rc-update add php-fpm default}}
Now they should start automatically when you boot your machine next time. To test that run:
{{cmd|reboot}}
To make sure that Nginx and PHP are started run command:
{{cmd|<nowiki>ps aux | grep 'nginx\|php-fpm'</nowiki>}}
You should get something like this:
<pre>
  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/php5/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
</pre>
=== 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
<pre>
http://X.X.X.X/phpinfo.php
</pre>
where X.X.X.X is IP address of your web server<br />
<br />
If everything was set up correctly, you should see information about your web server.
=== Troubleshooting ===
If PHP is not started check php-fpm log file
{{cmd|less /var/log/php-fpm.log}}
Make sure that configuration files do not contain errors
{{cmd|vi /etc/php5/php-fpm.conf
vi /etc/php5/php.ini}}


[[Category:Web Server]]
[[Category:Web Server]]
[[Category:PHP]]
[[Category:PHP]]

Latest revision as of 07:56, 3 June 2024

This material is obsolete ...

PHP5 was removed in 3.9. PHP7 is not in 3.16 or later repositories. (Discuss)

Nginx (engine x) is an HTTP and reverse proxy server, a mail proxy server, and a generic TCP/UDP proxy server

Installation

Nginx package is available in the Alpine Linux repositories. To install it run:

apk update apk add nginx

Creating new user and group 'www' for nginx

adduser -D -g 'www' www

Create a directory for html files

mkdir /www chown -R www:www /var/lib/nginx chown -R www:www /www

Configuration

You may want to make backup of original nginx.conf file before writting your own

mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf.orig

Configuring Nginx to listen to port 80 and process .html or .htm files

vi /etc/nginx/nginx.conf

user                            www;
worker_processes                auto; # it will be determinate automatically by the number of core

error_log                       /var/log/nginx/error.log warn;
#pid                             /var/run/nginx/nginx.pid; # it permit you to use rc-service nginx reload|restart|stop|start

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;
        server_name             localhost;
        client_max_body_size    32m;
        error_page              500 502 503 504  /50x.html;
        location = /50x.html {
              root              /var/lib/nginx/html;
        }
    }
}

Sample page

vi /www/index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>HTML5</title>
</head>
<body>
    Server is online
</body>
</html>

Controlling nginx

Start Nginx

After the installation Nginx is not running. To start Nginx, use start.

rc-service nginx start

You will get a feedback about the status.

 * Caching service dependencies ...                     [ ok ]
 * /run/nginx: creating directory
 * /run/nginx: correcting owner                         
 * Starting nginx ...                                   [ ok ]

Test configuration

When you've made any changes to your nginx configuration files, you should check it for errors before restarting/reloading nginx.
This will check for any duplicate configuration, syntax errors etc. To do this, run:

nginx -t

You will get a feedback if it failed or not. If everything is fine, you'll see the following and can then move ahead to reload the nginx server.

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Reload and Restart Nginx

Changes made in the configuration file will not be applied until the command to reload configuration is sent to nginx or it is restarted.
Reloading will do a "hot reload" of the configuration without server downtime. It will start the new worker processes with a new configuration and gracefully shutdown the old worker processes. If you have pending requests, then these will be handled by the old worker processes before it dies, so it's an extremely graceful way to reload configs. If you want to reload the web server, use reload.

rc-service nginx reload

If you want to restart the web server, use restart.

rc-service nginx restart

Stop Nginx

If you want to stop the web server, use stop.

rc-service nginx stop

Runlevel

Normally you want to start the web server when the system is launching. This is done by adding Nginx to the needed runlevel.

rc-update add nginx default

Now Nginx should start automatically when you boot your machine next time. To test that run:

reboot

To make sure that Nginx is started run:

ps aux | grep nginx

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
  310 root       0:00 grep nginx

Testing Nginx

This section is assuming that nginx is running and sample html page "/www/index.html" is created. Launch a web browser and point it to your web server. You should get:

Server is online

Troubleshooting

If Nginx is not started check Nginx log file

less /var/log/nginx/error.log

Make sure that configuration file does not contain errors. Edit the file in case there are any errors.

nginx -t vi /etc/nginx/nginx.conf

Nginx with PHP

Setting Up Nginx with PHP5
Setting Up Nginx with PHP7
Setting Up Nginx as Reverse Proxy with acme (Let's Encrypt)

PHP8.2 Installation

PHP packages is available in the Alpine Linux repositories. To install php8.2 with modules run:

apk add php82-fpm php82-soap php82-openssl php82-gmp php82-pdo_odbc php82-json php82-dom php82-pdo php82-zip php82-mysqli php82-sqlite3 php82-apcu php82-pdo_pgsql php82-bcmath php82-gd php82-odbc php82-pdo_mysql php82-pdo_sqlite php82-gettext php82-xmlreader php82-bz2 php82-iconv php82-pdo_dblib php82-curl php82-ctype

Perhaps you do not need all these PHP modules. Install modules according to your needs.

Configuration of PHP8.2

Defining ENV variables which will be used in configuration. You can do this e.g. in /etc/profile.d/php82.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

Modify variables according to your needs.

Modifying configuration file www.conf

sed -i "s|;listen.owner\s*=\s*nobody|listen.owner = ${PHP_FPM_USER}|g" /etc/php82/php-fpm.d/www.conf sed -i "s|;listen.group\s*=\s*nobody|listen.group = ${PHP_FPM_GROUP}|g" /etc/php82/php-fpm.d/www.conf sed -i "s|;listen.mode\s*=\s*0660|listen.mode = ${PHP_FPM_LISTEN_MODE}|g" /etc/php82/php-fpm.d/www.conf sed -i "s|user\s*=\s*nobody|user = ${PHP_FPM_USER}|g" /etc/php82/php-fpm.d/www.conf sed -i "s|group\s*=\s*nobody|group = ${PHP_FPM_GROUP}|g" /etc/php82/php-fpm.d/www.conf sed -i "s|;log_level\s*=\s*notice|log_level = notice|g" /etc/php82/php-fpm.d/www.conf #uncommenting line

Modifying configuration file php.ini

sed -i "s|display_errors\s*=\s*Off|display_errors = ${PHP_DISPLAY_ERRORS}|i" /etc/php82/php.ini sed -i "s|display_startup_errors\s*=\s*Off|display_startup_errors = ${PHP_DISPLAY_STARTUP_ERRORS}|i" /etc/php82/php.ini sed -i "s|error_reporting\s*=\s*E_ALL & ~E_DEPRECATED & ~E_STRICT|error_reporting = ${PHP_ERROR_REPORTING}|i" /etc/php82/php.ini sed -i "s|;*memory_limit =.*|memory_limit = ${PHP_MEMORY_LIMIT}|i" /etc/php82/php.ini sed -i "s|;*upload_max_filesize =.*|upload_max_filesize = ${PHP_MAX_UPLOAD}|i" /etc/php82/php.ini sed -i "s|;*max_file_uploads =.*|max_file_uploads = ${PHP_MAX_FILE_UPLOAD}|i" /etc/php82/php.ini sed -i "s|;*post_max_size =.*|post_max_size = ${PHP_MAX_POST}|i" /etc/php82/php.ini sed -i "s|;*cgi.fix_pathinfo=.*|cgi.fix_pathinfo= ${PHP_CGI_FIX_PATHINFO}|i" /etc/php82/php.ini

To add PHP support to Nginx we should modify Nginx configuration file:

vi /etc/nginx/nginx.conf

user                            www;
worker_processes                1;

error_log                       /var/log/nginx/error.log warn;
pid                             /var/run/nginx/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.php index.html index.htm;
        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;
        }
    }
}

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/php82/php-fpm.d/www.conf

Timezone

For configuring Timezone you may use tzdata package which can be installed by running:

apk add tzdata

Timezone configuration

TIMEZONE="Europe/Helsinki" cp /usr/share/zoneinfo/${TIMEZONE} /etc/localtime echo "${TIMEZONE}" > /etc/timezone sed -i "s|;*date.timezone =.*|date.timezone = ${TIMEZONE}|i" /etc/php82/php.ini

Sample PHP page

vi /www/phpinfo.php

<?php
	phpinfo();
?>

Starting Nginx with PHP82

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-fpm82 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-fpm82 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/php82/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

If everything was set up correctly, you should see information about your web server.

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/php82/php-fpm.conf vi /etc/php82/php.ini

PHP7 Installation

PHP packages is available in the Alpine Linux repositories. To install php7 with modules run:

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

Perhaps you do not need all these PHP modules. Install modules according to your needs.

Configuration of PHP7

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

Modify variables according to your needs.

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

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

To add PHP support to Nginx we should modify Nginx configuration file:

vi /etc/nginx/nginx.conf

user                            www;
worker_processes                1;

error_log                       /var/log/nginx/error.log warn;
pid                             /var/run/nginx/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;
        }
    }
}

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

Timezone

For configuring Timezone you may use tzdata package which can be installed by running:

apk add tzdata

Timezone configuration

TIMEZONE="Europe/Helsinki" cp /usr/share/zoneinfo/${TIMEZONE} /etc/localtime echo "${TIMEZONE}" > /etc/timezone sed -i "s|;*date.timezone =.*|date.timezone = ${TIMEZONE}|i" /etc/php7/php.ini

Sample PHP page

vi /www/phpinfo.php

<?php
	phpinfo();
?>

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

If everything was set up correctly, you should see information about your web server.

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


PHP5 Installation

PHP packages is available in the Alpine Linux repositories. To install php5 with modules run:

apk add php5-fpm php5-mcrypt php5-soap php5-openssl php5-gmp php5-pdo_odbc php5-json php5-dom php5-pdo php5-zip php5-mysql php5-mysqli php5-sqlite3 php5-apcu php5-pdo_pgsql php5-bcmath php5-gd php5-xcache php5-odbc php5-pdo_mysql php5-pdo_sqlite php5-gettext php5-xmlreader php5-xmlrpc php5-bz2 php5-memcache php5-mssql php5-iconv php5-pdo_dblib php5-curl php5-ctype

Perhaps you do not need all these PHP modules. Install modules according to your needs.

Configuration of PHP5

Defining ENV variables which will be used in configuration. You can do this e.g. in /etc/profile.d/php5.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

Modify variables according to your needs.

Modifying configuration file php-fpm.conf

sed -i "s|;listen.owner\s*=\s*nobody|listen.owner = ${PHP_FPM_USER}|g" /etc/php5/php-fpm.conf sed -i "s|;listen.group\s*=\s*nobody|listen.group = ${PHP_FPM_GROUP}|g" /etc/php5/php-fpm.conf sed -i "s|;listen.mode\s*=\s*0660|listen.mode = ${PHP_FPM_LISTEN_MODE}|g" /etc/php5/php-fpm.conf sed -i "s|user\s*=\s*nobody|user = ${PHP_FPM_USER}|g" /etc/php5/php-fpm.conf sed -i "s|group\s*=\s*nobody|group = ${PHP_FPM_GROUP}|g" /etc/php5/php-fpm.conf sed -i "s|;log_level\s*=\s*notice|log_level = notice|g" /etc/php5/php-fpm.conf #uncommenting line

Modifying configuration file php.ini

sed -i "s|display_errors\s*=\s*Off|display_errors = ${PHP_DISPLAY_ERRORS}|i" /etc/php5/php.ini sed -i "s|display_startup_errors\s*=\s*Off|display_startup_errors = ${PHP_DISPLAY_STARTUP_ERRORS}|i" /etc/php5/php.ini sed -i "s|error_reporting\s*=\s*E_ALL & ~E_DEPRECATED & ~E_STRICT|error_reporting = ${PHP_ERROR_REPORTING}|i" /etc/php5/php.ini sed -i "s|;*memory_limit =.*|memory_limit = ${PHP_MEMORY_LIMIT}|i" /etc/php5/php.ini sed -i "s|;*upload_max_filesize =.*|upload_max_filesize = ${PHP_MAX_UPLOAD}|i" /etc/php5/php.ini sed -i "s|;*max_file_uploads =.*|max_file_uploads = ${PHP_MAX_FILE_UPLOAD}|i" /etc/php5/php.ini sed -i "s|;*post_max_size =.*|post_max_size = ${PHP_MAX_POST}|i" /etc/php5/php.ini sed -i "s|;*cgi.fix_pathinfo=.*|cgi.fix_pathinfo= ${PHP_CGI_FIX_PATHINFO}|i" /etc/php5/php.ini

To add PHP support to Nginx we should modify Nginx configuration file:

vi /etc/nginx/nginx.conf

user                            www;
worker_processes                1;

error_log                       /var/log/nginx/error.log warn;
pid                             /var/run/nginx/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;
        }
    }
}

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/php5/php-fpm.conf

Timezone

For configuring Timezone you may use tzdata package which can be installed by running:

apk add tzdata

Timezone configuration

TIMEZONE="Europe/Helsinki" cp /usr/share/zoneinfo/${TIMEZONE} /etc/localtime echo "${TIMEZONE}" > /etc/timezone sed -i "s|;*date.timezone =.*|date.timezone = ${TIMEZONE}|i" /etc/php5/php.ini

Sample PHP page

vi /www/phpinfo.php

<?php
	phpinfo();
?>

Starting Nginx with PHP5

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-fpm 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-fpm 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/php5/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

If everything was set up correctly, you should see information about your web server.

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/php5/php-fpm.conf vi /etc/php5/php.ini