User Tools

Site Tools


nginx_php

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
nginx_php [2022/11/07 12:34] – [Details] adminnginx_php [2023/01/28 21:10] (current) – [Finish] admin
Line 2: Line 2:
 ===== Credits ===== ===== Credits =====
 The original article was published at [[https://raspberry-hosting.com/en/faq/how-install-nginx-php-fpm-raspberry-pi|Raspberry Hosting]]. It was written for an earlier Raspberry Pi OS.  I've since installed nginx and php on the latest OS, Bullseye.  The kernel was last compiled on October 26, 2022. The original article was published at [[https://raspberry-hosting.com/en/faq/how-install-nginx-php-fpm-raspberry-pi|Raspberry Hosting]]. It was written for an earlier Raspberry Pi OS.  I've since installed nginx and php on the latest OS, Bullseye.  The kernel was last compiled on October 26, 2022.
-===== Details =====+===== Package Installation =====
 Please refer to the original article for the discussion.  I'd repost it but I suspect I'd get in trouble for copyright violations.   Please refer to the original article for the discussion.  I'd repost it but I suspect I'd get in trouble for copyright violations.  
  
Line 18: Line 18:
 apt-get install nginx php-fpm php-apcu apt-get install nginx php-fpm php-apcu
 </code> </code>
-The repo doesn't have mysql any longer.  So install mariadb instead:+The repo doesn't have mysql any longer.  So install mariadb instead.  Note it will ask you which server to configure phpmyadmin.  The choices are apache2 and lighttpd.  Just press OK and go on.  If you plan on using phpmyadmin with nginx, you'll have to configure it yourself.
 <code> <code>
 apt-get install mariadb-client mariadb-server php-mysql phpmyadmin apt-get install mariadb-client mariadb-server php-mysql phpmyadmin
Line 25: Line 25:
 <code> <code>
 export DOMAIN=mydomain.com export DOMAIN=mydomain.com
 +export PHP_VER=7.4
 </code> </code>
-That way you can copy and paste the commands without substituting your domain name.  Just remember to change that export command.  +That way you can copy and paste the commands without substituting your domain name and php version.  Just remember to set the variables to match your system values.  Use this command to determine the php version: 
 +<code> 
 +dpkg -l | grep fpm 
 +</code> 
 +It will return something like: 
 +<code> 
 +ii  php-fpm                               2:7.3+69                                all          server-side, HTML-embedded scripting language (FPM-CGI binary) (default) 
 +ii  php7.3-fpm                            7.3.31-1~deb10u1                        armhf        server-side, HTML-embedded scripting language (FPM-CGI binary) 
 +</code> 
 +So in this instance, set PHP_VER to 7.3.
  
 This section is identical to the original This section is identical to the original
Line 37: Line 47:
 The backslash at the end of the adduser is a continuation character.  The adduser command ends with the "user001" This wiki doesn't wrap, so without the backslash you wouldn't see the entire command without scrolling. The backslash at the end of the adduser is a continuation character.  The adduser command ends with the "user001" This wiki doesn't wrap, so without the backslash you wouldn't see the entire command without scrolling.
  
-Here's where we start to use the DOMAIN variable we set earlier.  Note also that the version of php is now 7.4.  Don't blindly copy this command (and others later) without making sure that is the version that was installed earlier and adjust the commands accordingly.  Look for something like this (sample output from an earlier OS version, not the lateest):+===== Configuration Files ===== 
 +Here's where we start to use the DOMAIN and PHP_VER variables we set earlier.  
 <code> <code>
-The following NEW packages will be installed: +cp /etc/php/${PHP_VER}/fpm/pool.d/www.conf /etc/php/${PHP_VER}/fpm/pool.d/${DOMAIN}.conf
-  php-apcu php-apcu-bc php-common php-fpm php7.3-cli php7.3-common php7.3-fpm php7.3-json php7.3-opcache +
-  php7.3-readline+
 </code> </code>
 +So you could edit the ${DOMAIN}.conf file and make some changes, or you could use this script to generate it. 
  
 +WARNING if you are tempted  to simply copy/paste the configuration data rather than copy the entire code block, don't unless you understand here documents and escaping special characters.  
 +
 +Otherwise you'll have problems you don't want to have to fix.  Same goes for the nginx config file.
 +
 +If you don't/can't write to the pool.d file, change the file name to something else like /tmp/sample and let the cat command write the file.  To do that change the first line of this script to  'cat >/tmp/sample.conf <<EOF' then figure out how to get that data into the /etc/php/${PHP_VER}/fpm/pool.d/${DOMAIN}.conf file.  This is the way you would do it if you are attempting to run these instructions as a normal user.
 +
 +Highlight and copy this block and paste it directly into your shell.
 <code> <code>
-cp /etc/php/7.4/fpm/pool.d/www.conf /etc/php/7.4/fpm/pool.d/${MYDOMAIN}.conf +cat >/etc/php/${PHP_VER}/fpm/pool.d/${DOMAIN}.conf <<EOF
-</code> +
-So you could edit that file and make some changes, or you could use this one: +
-cat >/etc/php/7.4/fpm/pool.d/${MYDOMAIN}.conf <<EOF+
 ; pool name ('www' here) ; pool name ('www' here)
 [${DOMAIN}] [${DOMAIN}]
Line 55: Line 69:
 group = group001 group = group001
 ; The address on which to accept FastCGI requests. ; The address on which to accept FastCGI requests.
-;listen = /var/run/php7.4-fpm-${DOMAIN}.sock +;listen = /var/run/php${PHP_VER}-fpm-${DOMAIN}.sock 
-listen = /var/run/php/php7.4-fpm-local.sock+listen = /var/run/php/php${PHP_VER}-fpm-local.sock
 ; Set permissions for unix socket, if one is used. In Linux, read/write ; Set permissions for unix socket, if one is used. In Linux, read/write
 listen.owner = user001 listen.owner = user001
Line 83: Line 97:
 You may want to change a few of those settings.  Namely the pm = ondemand could be pm = dynamic and depending on your load you could adjust the pm.max_children setting.  Also unless you are on the east coast of the USA you'll want to change the timezone. You may want to change a few of those settings.  Namely the pm = ondemand could be pm = dynamic and depending on your load you could adjust the pm.max_children setting.  Also unless you are on the east coast of the USA you'll want to change the timezone.
  
 +Here's my nginx config.  Adjust as you need:
 +Highlight and copy this block and paste it directly into your shell.
 +<code>
 +cat >/etc/nginx/sites-available/${DOMAIN}.conf  <<EOF
 +# redirect www
 +server {
 +        listen 80;
 +        server_name www.${DOMAIN};
 +        return 301 \$scheme://${DOMAIN}\$request_uri;
 +}
 +server {
 +        listen   80; ## listen for ipv4
 + 
 +        # root dir of your pages
 +        root /home/${DOMAIN}/www/;
 + 
 +        index index.php index.html index.htm;
 + 
 +        server_name ${DOMAIN};
 + 
 +        location / {
 +                try_files \$uri \$uri/ /index.html;
 +        }
 + 
 +        # pass the PHP scripts to FastCGI
 +        location ~ \.php$ {
 +                try_files \$uri = 404;
 +                fastcgi_split_path_info ^(.+\.php)(/.+)$;
 +                # php${PHP_VER}-fpm :
 +                fastcgi_pass unix:/var/run/php/php${PHP_VER}-fpm-local.sock;
 +                fastcgi_index index.php;
 +#                include fastcgi_params;
 +                include fastcgi.conf;
 +        }
 + 
 +        # deny access to .htaccess files, if Apache's document root concurs with nginx's one
 +        location ~ /\.ht {
 +                deny all;
 +        }
 + 
 +        # error and access logs
 +        error_log /home/${DOMAIN}/logs/nginx-error.log debug;
 +        access_log /home/${DOMAIN}/logs/nginx-access.log;
 + 
 +        # other converting rewrite rules search on:
 +        # http://nginx.org/en/docs/http/converting_rewrite_rules.html
 +        #
 + 
 +}
 +EOF
 +ln -s /etc/nginx/sites-available/${DOMAIN}.conf /etc/nginx/sites-enabled/
 +rm -f /etc/nginx/sites-enabled/default
 +</code>
 +Note: I had to replace the "include fastcgi_params" with include "fastcgi.conf" The original file was missing a required parameter.
 +
 +Now create the subdirectories.
 +<code>
 +mkdir /home/${DOMAIN}
 +cd /home/${DOMAIN}
 +mkdir logs sessions tmp www
 +chown -R user001.group001 *
 +chown user001.group001 /home/${DOMAIN}
 +</code>
 +
 +===== Finish =====
 +<code>
 +service nginx restart && service php${PHP_VER}-fpm restart
 +</code>
 +
 +Test with these commands:
 +<code>
 +cd /home/${DOMAIN}/www
 +cat >phpinfo.php <<EOF
 +<?php
 +phpinfo( );
 +?>
 +EOF
 +</code>
 +Now bring up a browser and go to http://YOUR_DOMAIN/phpinfo.php.
 +
 +Where YOUR_DOMAIN is the value you set into ${DOMAIN} earlier.
 +
 +This is somewhat tested. 
 +
 +This was used to install nginx/php on a 32 bit Bullseye Pi4 on 28 January 2023, when I found a couple of bugs and fixed them. 
  
  
nginx_php.1667820858.txt.gz · Last modified: 2022/11/07 12:34 by admin