Source compile Apache2.4 + PHP7.4 with Raspberry Pi and build a Web server --2 PHP introduction

Assumptions and preparations

Linux server building article

-Building a file server with Samba (CentOS 8.1 / openSUSE 15.1 / Ubuntu 20.04) -Source compilation of Apache2.4 + PHP7.4 on Linux-- 1. Apache introduction / [Raspberry Pi] / items / 67686eccaaec73251458) -Source compilation of Apache2.4 + PHP7.4 on Linux-- 2. PHP introduction / [Raspberry Pi: This article] -Source compilation of Apache2.4 + PHP7.4 on Linux-- 3. MySQL introduction / [[Raspberry Pi]](https://qiita.com/kazumi75kitty / items / 4212dacc45944f27ca94) -Apache2.4 + PHP7.4 on Linux --4 Security (chown and firewalld) -Build an IPsec gateway on Linux VPN-- 1. Introduce StrongSwan / [[Ubuntu 20.04 + Raspberry Pi]](https://qiita.com/kazumi75kitty/ items / 08259681247a6c2ebd0d) -Build an IPsec gateway on Linux for VPN-- 2. Check connection to VPN / [[Ubuntu 20.04 + Raspberry Pi]](https://qiita.com / kazumi75kitty / items / c83f920f052d83d62457)

Last time built Apache httpd 2.4.46 on Raspberry Pi by source compilation, but this time, continuing from the previous time, Apache is the backbone of the web application server. Introduce PHP 7.4 by source compilation (⑅ • ᴗ • ⑅)

environment

--Web server program: Apache 2.4.46 + PHP 7.4.10 (source compilation) --Client: Windows10 Pro --Server architecture: Raspberry Pi 3B + (with armv8) Linux distribution: openSUSE 15.1 Leap (64bit) / Raspberry Pi OS 2020.08 version (32bit)

Premise

--User installed as root (in my verification, it is an administrator account called admin, and it is processed by sudo from there) --In openSUSE, the firewall uses firewalld (does not use distribution-specific firewall commands). In Raspberry Pi OS of Raspberry Pi, for firewalld, since the interlocking around IPv6 was dung, I decided to use the Debian standard ufw. -Last time article has completed Apache installation (operation can be confirmed and it is built as a Web server)

Server conditions

IP address

--Client: 192.168.1.11 --Web server: 192.168.1.18 (verified with the same IP address for all distributions) --Affiliation network segment: 192.168.1.0/24 Webサーバー.png

Ability and version to download and install individual packages (as of June 2020)

Other required packages are installed with the distribution's standard package commands (dnf, apt, etc.) and do not need to be downloaded individually.

For download, you can access the official website, download from there and transfer it by FTP, or you can get it with wget if you know the URL of the download file, but the acquisition method is omitted.

Work procedure

PHP installation

Introduced libraries required for PHP source compilation

openSUSE15.1(RaspberryPi)


# zypper -n install libxml2-tools libxml2-devel sqlite3-devel oniguruma-devel

RaspberryPiOS(2020.08)


# apt-get -y install libxml2 libxml2-dev libsqlite3-dev libonig-dev

Up to PHP7.3, it was possible to install to handle multi-byte character strings without using SQLite and Oniguruma libraries, but in PHP7.4 it became mandatory for source compilation.

PHP source compilation (actually I was addicted to forced termination due to lack of swap virtual memory)

It will take about 30-40 minutes to compile. It's bigger than Apache, so you might want to take a break with a cup of coffee while compiling (\ * ˘︶˘ \ *) ...: \ * ♡

# cd [php-7.4.10.tar.Directory where gz is located]
# tar xvzf php-7.4.10.tar.gz
# cd php-7.4.10/
# ./configure --with-apxs2=/usr/local/apache2/bin/apxs --with-mysqli --with-pdo-mysql --enable-mbregex --enable-mbstring
# make
# make test

--MySQL enables PDO --Enable multibyte strings

"Make" and "make test" are commands that take a long time. If there is no error on the way, the compilation is completed, but ** there is a problem with Raspberry Pi OS. ** **

RaspberryPiOS(2020.08)


# make

/bin/bash /home/admin/php-7.4.10/libtool --silent --preserve-dup-deps --mode=compile cc -I/home/admin/php-7.4.10/ext/fileinfo/libmagic -Iext/fileinfo/ -I/home/admin/php-7.4.10/ext/fileinfo/ -DPHP_ATOM_INC -I/home/admin/php-7.4.10/include -I/home/admin/php-7.4.10/main -I/home/admin/php-7.4.10 -I/home/admin/php-7.4.10/ext/date/lib -I/usr/include/libxml2 -I/home/admin/php-7.4.10/ext/mbstring/libmbfl -I/home/admin/php-7.4.10/ext/mbstring/libmbfl/mbfl -I/usr/local/include -I/home/admin/php-7.4.10/TSRM -I/home/admin/php-7.4.10/Zend  -D_REENTRANT -pthread  -I/usr/include -g -O2 -fvisibility=hidden -pthread -Wall -Wno-strict-aliasing -DZTS -DZEND_SIGNALS   -c /home/admin/php-7.4.10/ext/fileinfo/libmagic/apprentice.c -o ext/fileinfo/libmagic/apprentice.lo
cc: fatal error:Forced termination signal terminated program cc1
compilation terminated.
make: *** [Makefile:738: ext/fileinfo/libmagic/apprentice.lo]Error 1

It was forcibly terminated during make like this (´ • ω • ̥`) PHP builds with a lot of processing, so I wondered why only Raspberry Pi OS ... and checked the free memory space ...

RaspberryPiOS(2020.08)


# free
              total        used        free      shared  buff/cache   available
Mem:         948072      389256      367752       99504      191064      394740
Swap:        102396      102256         140

I noticed that the Raspberry Pi OS had only 100MB of swap space, and I had only 1MB of free space ... (openSUSE had 400MB on the Raspberry Pi)

So I decided to increase the swap area to increase the virtual memory. ** On Raspberry Pi OS, swap is defined in dphys-swapfile instead of partition **

--Reference: Increased pie swap

RaspberryPiOS(2020.08)


# vi /etc/dphys-swapfile
CONF_SWAPSIZE=100 ← increase
CONF_SWAPFACTOR=2 ← Set the magnification

# reboot

In my case, I increased it to 200MB and set the magnification to 2. I rebooted and rebuilt and this time it was a success! !! [But it seems that the build went well by restarting]

But ** more problems have occurred **. This time, an event that causes a Segmentation Fault in "make test" has occurred. The build should have been successful, but the test crashed due to a segmentation fault (called an application error in Windows). .. ..

Whether there is a problem with the test tool or the module that I built, I will try running PHP for the time being, so I will suspend the test once.

PHP installation

When the compilation is complete, install it.

# make install
Installing PHP SAPI module:       apache2handler
/usr/local/apache2/build/instdso.sh SH_LIBTOOL='/opt/apr-1.7.0/build-1/libtool' libphp7.la /usr/local/apache2/modules
/opt/apr-1.7.0/build-1/libtool --mode=install install libphp7.la /usr/local/apache2/modules/
libtool: install: install .libs/libphp7.so /usr/local/apache2/modules/libphp7.so
libtool: install: install .libs/libphp7.lai /usr/local/apache2/modules/libphp7.la
libtool: warning: remember to run 'libtool --finish /home/admin/php-7.4.6/libs'
chmod 755 /usr/local/apache2/modules/libphp7.so
[activating module `php7' in /usr/local/apache2/conf/httpd.conf]
Installing shared extensions:     /usr/local/lib/php/extensions/no-debug-zts-20190902/
Installing PHP CLI binary:        /usr/local/bin/
Installing PHP CLI man page:      /usr/local/php/man/man1/
Installing phpdbg binary:         /usr/local/bin/
Installing phpdbg man page:       /usr/local/php/man/man1/
Installing PHP CGI binary:        /usr/local/bin/
Installing PHP CGI man page:      /usr/local/php/man/man1/
Installing build environment:     /usr/local/lib/php/build/
Installing header files:          /usr/local/include/php/
Installing helper programs:       /usr/local/bin/
  program: phpize
  program: php-config
Installing man pages:             /usr/local/php/man/man1/
  page: phpize.1
  page: php-config.1
/home/admin/php-7.4.6/build/shtool install -c ext/phar/phar.phar /usr/local/bin
ln -s -f phar.phar /usr/local/bin/phar
Installing PDO headers:           /usr/local/include/php/ext/pdo/

Some of the PHP libraries are placed in multiple locations depending on the purpose, such as "/ usr / local / include / php /" and "/ usr / local / lib / php /". What's more, this time PHP 7.4.10 is not placed in "/ usr / local / lib64 / php /" on Raspberry Pi's openSUSE, but in the same place as other distributions **. I don't know why, but for the time being, when I implemented PHP 7.4.10 + openSUSE 15.1 with Raspberry Pi, I found that it was not placed in lib64 but in the lib directory.

Also, at this point, ** PHP DLLs (dynamic link libraries, corresponding to external extension applications) are automatically deployed to the Apache folder **.

Next, copy the PHP configuration file. First, copy php.ini to your PHP library folder / usr / local / lib (including openSUSE) /.

# cp php.ini-development /usr/local/lib/php.ini
# ls -l /usr/local/lib
Total 328
-rw-r--r--1 root root 144402 June 24 12:34 libz.a
lrwxrwxrwx 1 root root 14 June 24 12:34 libz.so -> libz.so.1.2.11
lrwxrwxrwx 1 root root 14 June 24 12:34 libz.so.1 -> libz.so.1.2.11
-rwxr-xr-x 1 root root 113656 June 24 12:34 libz.so.1.2.11
drwxr-xr-x 4 root root 37 June 24 16:19 php
-rw-r--r--1 root root 72278 June 24 16:21 php.ini
drwxr-xr-x 2 root root 21 June 24 12:34 pkgconfig

Settings on the Apache side

I need to make the PHP file recognizable by Apache, so edit it. The PHP DLL is automatically written in httpd.conf when PHP is installed, so there is no need to add it. All you have to do on the httpd.conf side is that Apache recognizes the PHP MIME type.

# vi /usr/local/apache2/conf/httpd.conf

/usr/local/apache2/conf/httpd.conf


…
<IfModule dir_module>
    DirectoryIndex index.html ← 「index.Add "php"
</IfModule>
…
<IfModule mime_module>
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    …
    AddType application/x-httpd-php .php ← add this line
</IfModule>
…

PHP preferences

Change the PHP character code and reference library settings.

# vi /usr/local/lib/php.ini

php.ini


#Since the content is long, only the ones that have been partially modified and the ones that have been modified are listed. semicolon";"Is removed. ";It is described including the line that you just remove
output_buffering = On
default_charset = "UTF-8"
include_path = ".:/usr/local/include/php:/usr/local/lib/php"
extension_dir = "/usr/local/include/php/ext"
date.timezone = Asia/Tokyo
mbstring.language = Japanese
mbstring.encoding_translation = Off
mbstring.detect_order = UTF-8, SJIS, EUC-JP, JIS, ASCII
mbstring.substitute_character = none

PHP operation check

PHP confirmation page

First, create a PHP page for confirmation.

# vi /usr/local/apache2/htdocs/phpi.php

phpi.php


<?php phpinfo(); ?>

Why put PHP files in / usr / local / apache2 / htdocs /? ?? However, when Apache is installed by source compilation, the default storage folder for Web page data is "/ usr / local / apache2 / htdocs /". Well, you can change the storage location in httpd.conf, but this time I will omit it.

Restart apache

# systemctl stop httpd
# systemctl start httpd
# systemctl status httpd

If you do not restart, Apache will not work with the PHP module loaded, so stop it and restart it. After that, make sure that it is started with status. phpinfo.png

pdo.png

The image above is from a virtual machine, but I got a similar phpinfo on my Raspberry Pi, so I can use PHP (\ * ˘ᗜ˘ \ *) ... I just didn't do it because it's a hassle to prepare images. .. ..

next time

Connect to MySQL and make the database available to your PHP web app server. ** MySQL is not supported on Raspberry Pi **, so you can't easily install it directly from the repository, and even if you install it, it will only shorten the write life of the microSD, so next time you can connect to the MySQL server * Validate *.

Recommended Posts

Source compile Apache2.4 + PHP7.4 with Raspberry Pi and build a Web server --2 PHP introduction
Source compile Apache2.4 + PHP7.4 with Raspberry Pi and build a Web server ―― 1. Apache introduction
Source compile Apache2.4 + PHP7.4 with Raspberry Pi and build a web server --3. Use MySQL
Source compile Apache2.4 (httpd 2.4.43) + PHP7.4 on Linux and build a Web server ―― 1. Apache introduction
Source compile Apache2.4 (httpd 2.4.43) + PHP7.4 on Linux and build a Web server --2 PHP introduction
Source compile Apache2.4 (httpd 2.4.43) + PHP7.4 on Linux to build a Web server --3 MySQL 8.0 introduction
Create a web surveillance camera with Raspberry Pi and OpenCV
Build a server on Linux and local network with Raspberry Pi NextCloud and desktop sharing
Build a Tensorflow environment with Raspberry Pi [2020]
I made a web server with Raspberry Pi to watch anime
Build a CentOS Linux 8 environment with Docker and start Apache HTTP Server
Launch a web server with Python and Flask
Web server construction with Apache 2.4 (httpd 2.4.43) + PHP 7.4 on Linux ―― 4. Security (chown and firewalld)
Build a distributed environment with Raspberry PI series (Part 3: Install and configure dnsmasq)
Easy introduction to home hack with Raspberry Pi and discord.py
I tried connecting Raspberry Pi and conect + with Web API
Set up a web server with CentOS7 + Anaconda + Django + Apache
Build a speed of light web API server with Falcon
Build a LAMP environment with Vagrant (Linux + Apache + MySQL + PHP)
Build a web application with Django
VPN server construction with Raspberry Pi
Using a webcam with Raspberry Pi
Christmas classic (?) Lighting a Christmas tree with Raspberry Pi and Philips Hue
Make a thermometer with Raspberry Pi and make it viewable with a browser Part 4
Make a Kanji display compass with Raspberry Pi and Sense Hat
Pet monitoring with Rekognition and Raspberry pi
[Raspberry Pi] Add a thermometer and a hygrometer
Make a wireless LAN Ethernet converter and simple router with Raspberry Pi
[Python + PHP] Make a temperature / humidity / barometric pressure monitor with Raspberry Pi
Build a web server on your Chromebook
Make a wash-drying timer with a Raspberry Pi
Operate an oscilloscope with a Raspberry Pi
Introduction and usage of Python bottle ・ Try to set up a simple web server with login function
Start a web server using Bottle and Flask (I also tried using Apache)
Create a car meter with raspberry pi
Put Docker in Windows Home and run a simple web server with Python
Make a thermometer with Raspberry Pi and make it visible on the browser Part 3
Cross-compiling Raspberry Pi and building a remote debugging development environment with VS Code
Creating a temperature control system with Raspberry Pi and ESP32 (3) Recipient Python file
[For beginners] I made a motion sensor with Raspberry Pi and notified LINE!
[Python] How to create a local web server environment with SimpleHTTPServer and CGIHTTPServer
Build a local server with a single command [Mac]
Build a Django environment on Raspberry Pi (MySQL)
Start a simple Python web server with Docker
MQTT RC car with Arduino and Raspberry Pi
Build a virtual environment with pyenv and venv
Compile and run Rust with a single command
Get temperature and humidity with DHT11 and Raspberry Pi
Build a Python development environment on Raspberry Pi
Control music playback on a smartphone connected to Raspberry Pi 3 and bluetooth with AVRCP
Distributed environment construction with Raspberry PI series (Part 4: NFS server construction and client OS import)
Create a color sensor using a Raspberry Pi and a camera
Easy IoT to start with Raspberry Pi and MESH
Build a python virtual environment with virtualenv and virtualenvwrapper
Detect mask wearing status with OpenCV and Raspberry Pi
[Part 2] Let's build a web server on EC2 Linux
Measure temperature and humidity with Raspberry Pi3 and visualize with Ambient
Build Apache HTTP Server and Wildfly on Oracle Linux 8
Build a python virtual environment with virtualenv and virtualenvwrapper
CTF beginner tried to build a problem server (web) [Problem]
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python