PHP on Raspberry PI
We want to run Embedded Webserver, and use PHP to run executable on Raspberry PI board from remote.
Information
Board : Raspberry Pi v4
Linux OS : Debian GNU/Linux 11 (bullseye)
PHP installation
Step 1: First step is to download source code of latest PHP version
https://www.php.net/downloads
at the time of writing this blog, latest version as php-8.1.12.
Download php-8.1.12.tar.gz file.
Step 2: Extract file
$ tar zxfv php-8.1.12.tar.gz
$ cd php-8.1.12/
Step 3: Meeting dependencies
$ sudo apt install autoconf automake libtool re2c bison libxml2-dev libsqlite3-dev
Step 4: Configuring php
$ ./configure --enable-fpm --with-mysqli
Note: Other possible configuration options ...
--without-libxml --without-sqlite3
for all possible options run this command ...
./configure --help
After successful configuration, Makefile will be generated.
Step 5: Build
$ make
Step 6: Install
$ sudo make install
PHP Configuration
PHP Related configuration files
1. /usr/local/etc/php-fpm.conf
Include path for php_fpm.d/*.conf
2. /usr/local/etc/php-fpm.d/www.conf
Entry for www-data user and group
3. /usr/local/php/php.ini
Config settings for : upload sizes, file timeouts, and resource limits.
Settings cgi.fix_pathinfo=0
NGINX Related config files
1. /etc/nginx/sites-available/default
Setting for location, fastcgi_* etc.
Let us copy configuration files present into the PHP directory "~/php-8.1.12". Here is how we did copy of configuration files on our system.
$ cd ~/php-8.1.12
$ sudo cp php.ini-development /usr/local/php/php.ini
$ sudo cp /usr/local/etc/php-fpm.d/www.conf.default /usr/local/etc/php-fpm.d/www.conf
$ sudo cp sapi/fpm/php-fpm.conf /usr/local/etc/php-fpm.conf
Copy php-fpm (FastCGI Process Manager) to bin
$ sudo cp sapi/fpm/php-fpm /usr/local/bin
Modify location
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
root html;
index index.php index.html index.htm;
}
location ~ \.php$ {
# include snippets/fastcgi-php.conf;
# # With php-fpm (or other unix sockets):
# fastcgi_pass unix:/run/php/php7.4-fpm.sock;
# # With php-cgi (or other tcp sockets):
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
Running PHP
start php-fpm
$ sudo php-fpm
We can verify whether PHP is running. We can use "ps -ef" command. And observe process php-fpm.
$ pi@raspberrypi:~/php-8.1.12 $ ps -ef | grep php
root 91554 1 0 19:19 ? 00:00:00 php-fpm: master process (/usr/local/etc/php-fpm.conf)
www-data 91555 91554 0 19:19 ? 00:00:00 php-fpm: pool www
www-data 91556 91554 0 19:19 ? 00:00:00 php-fpm: pool www
pi 91581 1731 0 19:23 pts/0 00:00:00 grep --color=auto php
PHP page
we will create index.php in "/var/www/html/" directory. Following will be the content of php file...
<?php phpinfo(); ?>
if you can see such webpage, than php is properly configured and running.
Further Reading
PHP
https://www.php.net/
NGINX
https://nginx.org/en/docs/install.html
About EmbeddedCraft
EmbeddedCraft is the information portal for Embedded System and IOT. We are running Website, Blog, YouTube Channel. Stay in touch and many new informative articles are going to publish.
Visit us at …
Our Website: http://embeddedcraft.org/
Embeddedcraft blog YouTube Channel
Comments
Post a Comment