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 15:32] – [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 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 latest):+===== 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: +
-<code> +
-cat >/etc/php/7.4/fpm/pool.d/${MYDOMAIN}.conf <<EOF+
 ; pool name ('www' here) ; pool name ('www' here)
 [${DOMAIN}] [${DOMAIN}]
Line 56: 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 85: Line 98:
  
 Here's my nginx config.  Adjust as you need: Here's my nginx config.  Adjust as you need:
 +Highlight and copy this block and paste it directly into your shell.
 <code> <code>
 cat >/etc/nginx/sites-available/${DOMAIN}.conf  <<EOF cat >/etc/nginx/sites-available/${DOMAIN}.conf  <<EOF
Line 91: Line 105:
         listen 80;         listen 80;
         server_name www.${DOMAIN};         server_name www.${DOMAIN};
-        return 301 $scheme://${DOMAIN}$request_uri;+        return 301 \$scheme://${DOMAIN}\$request_uri;
 } }
 server { server {
Line 104: Line 118:
    
         location / {         location / {
-                try_files $uri $uri/ /index.html;+                try_files \$uri \$uri/ /index.html;
         }         }
    
         # pass the PHP scripts to FastCGI         # pass the PHP scripts to FastCGI
         location ~ \.php$ {         location ~ \.php$ {
-                try_files $uri = 404;+                try_files \$uri = 404;
                 fastcgi_split_path_info ^(.+\.php)(/.+)$;                 fastcgi_split_path_info ^(.+\.php)(/.+)$;
-                # php7.4-fpm : +                # php${PHP_VER}-fpm : 
-                fastcgi_pass unix:/var/run/php/php7.4-fpm-local.sock;+                fastcgi_pass unix:/var/run/php/php${PHP_VER}-fpm-local.sock;
                 fastcgi_index index.php;                 fastcgi_index index.php;
 #                include fastcgi_params; #                include fastcgi_params;
Line 140: Line 154:
 Now create the subdirectories. Now create the subdirectories.
 <code> <code>
 +mkdir /home/${DOMAIN}
 cd /home/${DOMAIN} cd /home/${DOMAIN}
 mkdir logs sessions tmp www mkdir logs sessions tmp www
 chown -R user001.group001 * chown -R user001.group001 *
 +chown user001.group001 /home/${DOMAIN}
 </code> </code>
  
-To make this a bit more generic, you could define the php version (export PHP_VER=7.4) and then change all the versions to use the variablePHP_VER as we did with the domain.+===== 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 2023when I found a couple of bugs and fixed them
  
-This is untested.   
  
nginx_php.1667831562.txt.gz · Last modified: 2022/11/07 15:32 by admin