Step by Step: Compile Install PHP 8 on Rocky 9 / RHEL-based others

Estimated reading time: 2 minutes

* Compiling Needs Memory Space!\
If the memory space of your device is Lower than 2GB, You had better Create a Swap.

Tested OS Tested SW Ver.

  1. Prepare

    dnf groupinstall "Development Tools"
    
    dnf config-manager --set-enabled crb
    
    dnf makecache --refresh
    
    dnf install libxml2-devel libicu-devel sqlite-devel libxslt-devel libpng-devel libjpeg-devel freetype-devel libzip-devel git systemd-devel curl-devel

    Install Oniguruma

    git clone https://github.com/kkos/oniguruma.git
    cd oniguruma
    ./autogen.sh
    ./configure --bindir=/usr/sbin/ \
            --sbindir=/usr/sbin/ \
            --libexecdir=/usr/libexec \
            --sysconfdir=/etc/ \
            --localstatedir=/var \
            --libdir=/usr/lib64/  \
            --includedir=/usr/include/ \
            --datarootdir=/usr/share \
            --infodir=/usr/share/info \
            --localedir=/usr/share/locale \
            --mandir=/usr/share/man/ \
            --docdir=/usr/share/doc/onig
    make && make install

    Add user for FPM

    adduser www
  2. Download the Source
    Downloads OSS-DL

    Copy the link you need and download and then unzip.

    wget https://www.php.net/distributions/php-8.1.2.tar.gz
    
    tar -xzf php-8.1.2.tar.gz
  3. Pre-Compile

    See all options in Option

    Here are the recommended options (FPM included):

    cd php-8.1.2
    # Replace UR_WEB_USER into your user for php-fpm
    ./configure --prefix=/usr/local/php/ --enable-fpm --with-openssl --enable-bcmath --with-curl --enable-ftp --enable-gd --enable-mbstring --enable-sockets --enable-pcntl --with-zlib --enable-intl --with-fpm-systemd --enable-pdo --enable-xml --with-zip --with-gettext --with-freetype --enable-opcache --enable-shmop --with-fpm-user=UR_WEB_USER --with-fpm-group=UR_WEB_USER
    

    If need to support MySQL, add those options you need. The Directories and Configs depend on your env.
    (MySQL here installed by DNF)

    --enable-mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli --with-mysql-sock=/var/lib/mysql/mysql.sock
  4. Compile & Install

    make && make install
  5. Finish Config

    Copy Config File to the Install Directory

    cp PATH_TO_THE_SRC/php.ini-production /usr/local/php/php.ini
    cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
    cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

    Add Php to systemd as a service (Recommended, make it easier to manage)

    1. Edit PID Location
    # Open the File
    vim /usr/local/php/etc/php-fpm.conf
    
    # Find the part and Change into
    pid = /var/run/php-fpm.pid
    1. Write the systemd script
    # Create one and edit
    vim /lib/systemd/system/php-fpm.service
    1. Then, add the content.
    [Unit]
    Description=The PHP FastCGI Process Manager
    After=syslog.target network.target
    
    [Service]
    Type=forking
    PIDFile=/var/run/php-fpm.pid
    ExecStart=/usr/local/php/sbin/php-fpm
    ExecReload=/bin/kill -USR2 $MAINPID
    PrivateTmp=true
    
    [Install]
    WantedBy=multi-user.target
    1. Reload systemd
    systemctl daemon-reload
  6. Enjoy it!

Leave a Reply

Your email address will not be published. Required fields are marked *