Deploying the LNMP Stack: Nginx, MySQL 8, and PHP 7 from Source

Prerequisites and Source Acquisition

To build the LNMP (Linux, Nginx, MySQL, PHP) environment, begin by establishing a build directory and fetching the required packages. This setup utilizes Nginx 1.17, PHP 7.3, and the MySQL 8.0 RPM bundle. Additionally, compiling certain dependencies requires a modern version of CMake.

mkdir -p /opt/stack_build && cd /opt/stack_build

# Fetch core packages
curl -O http://nginx.org/download/nginx-1.17.2.tar.gz
curl -O https://www.php.net/distributions/php-7.3.7.tar.gz
curl -O https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.16-2.el7.x86_64.rpm-bundle.tar

# Extract archives
tar -zxf nginx-1.17.2.tar.gz
tar -zxf php-7.3.7.tar.gz
tar -zxf mysql-8.0.16-2.el7.x86_64.rpm-bundle.tar

# Install base compilation tools
yum install -y gcc gcc-c++ make automake

# Build and install CMake 3.15
curl -O https://github.com/Kitware/CMake/releases/download/v3.15.1/cmake-3.15.1.tar.gz
tar -zxf cmake-3.15.1.tar.gz
cd cmake-3.15.1
./bootstrap --prefix=/usr/local/cmake
make && make install
ln -s /usr/local/cmake/bin/cmake /usr/bin/cmake
cd /opt/stack_build

Deploying MySQL 8.0

MySQL 8 is installed using the RPM packages extracted earlier. First, set up the dedicated system user, then install the required RPMs followed by basic initialization.

# Create system group and user
groupadd db_grp
useradd -g db_grp db_user

# Resolve dependencies
yum install -y libaio

# Install RPMs ignoring dependencies to force sequential setup
rpm -ivh mysql-community-common-8.0.16-2.el7.x86_64.rpm --nodeps --force
rpm -ivh mysql-community-libs-8.0.16-2.el7.x86_64.rpm --nodeps --force
rpm -ivh mysql-community-client-8.0.16-2.el7.x86_64.rpm --nodeps --force
rpm -ivh mysql-community-server-8.0.16-2.el7.x86_64.rpm --nodeps --force

# Assign correct ownership and start the service
chown -R db_user:db_grp /var/lib/mysql
systemctl start mysqld.service
systemctl enable mysqld

# Retrieve the temporary root password
grep 'temporary password' /var/log/mysqld.log

# Log in and update the root credentials
# mysql -u root -p
# ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyStr0ngP@ss!';

Compiling PHP 7.3

Building PHP from source requires numerous development libraries. During configuration, you might encounter missing libzip or off_t definition errors, which require manual compilation of libzip and updating the linker configuration.

# Install PHP build dependencies
yum install -y epel-release
yum install -y screen git openssl curl gmp-devel libc-client-devel bzip2-devel \
  enchant-devel libwebp-devel libXpm-devel openldap openldap-devel php-pspell \
  aspell-devel readline-devel libtidy-devel libxslt-devel libxml2-devel openssl-devel \
  curl-devel libjpeg-devel libpng-devel freetype-devel autoconf libc-client-devel libtidy \
  libtidy-devel

# Resolve libzip dependency manually
yum remove libzip -y
curl -O https://libzip.org/download/libzip-1.5.1.tar.gz
tar -zxf libzip-1.5.1.tar.gz
cd libzip-1.5.1
mkdir _build && cd _build
cmake ..
make && make install
cd /opt/stack_build/php-7.3.7

# Fix off_t undefined error by updating shared library paths
echo '/usr/local/lib64 /usr/local/lib /usr/lib /usr/lib64' >> /etc/ld.so.conf
ldconfig -v

# Configure PHP with FPM and database drivers
./configure \
  --prefix=/usr/local/php7 \
  --with-config-file-path=/usr/local/php7/etc \
  --with-config-file-scan-dir=/usr/local/php7/conf.d \
  --enable-fpm --enable-cgi --enable-phpdbg \
  --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd \
  --with-sqlite3 --with-pdo-sqlite \
  --with-zlib --with-zlib-dir --with-bz2 \
  --with-gd --with-webp-dir --with-jpeg-dir --with-png-dir --with-xpm-dir --with-freetype-dir \
  --enable-gd-jis-conv --enable-mbstring --with-iconv \
  --with-openssl --with-curl --with-ldap --with-ldap-sasl \
  --enable-sockets --enable-ftp --enable-soap --enable-zip \
  --enable-opcache --with-pear

# Resolve libc-client and libldap linking issues
ln -s /usr/lib64/libc-client.so /usr/lib/libc-client.so
cp -frp /usr/lib64/libldap* /usr/lib/

# Fix Makefile for 'ld returned 1 exit status' by appending -llber to EXTRA_LIBS
sed -i 's/^EXTRA_LIBS = \(.*\)/EXTRA_LIBS = \1 -llber/' Makefile

# Compile using all available CPU cores
make -j $(nproc) && make install

Building and Configuring Nginx

Finally, compile the Nginx web server. After installation, adjust the configuration file to forward PHP requests to the PHP-FPM service.

cd /opt/stack_build/nginx-1.17.2
./configure --prefix=/usr/local/nginx
make && make install

# Verify the web server responds
curl -I 127.0.0.1

Edit the Nginx configuration file to integrate PHP-FPM:

vim /usr/local/nginx/conf/nginx.conf

Insert the folloiwng location block inside the server context to handle PHP file processing:

location ~ \.php$ {
    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME /$document_root$fastcgi_script_name;
    include        fastcgi_params;
}

Launch the Nginx server to apply the changes:

/usr/local/nginx/sbin/nginx

Tags: LNMP nginx MySQL 8 PHP 7 CMake

Posted on Thu, 27 Aug 2026 16:46:19 +0000 by toma42