Step-by-Step Guide to Building a LAMP Stack with Apache, PHP, and MariaDB on CentOS

This tutorial walks through setting up a complete LAMP environment on two CentOS machines, covering compilation of APR, APR‑util, Apache 2.4, MariaDB, PHP (both mod_php and PHP‑FPM), XCache integration, and configuring Apache to proxy PHP‑FPM requests, with full command‑line examples and verification steps.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Step-by-Step Guide to Building a LAMP Stack with Apache, PHP, and MariaDB on CentOS

First, the experimental environment is introduced: an HTTP server at 192.168.236.128 (Apache with modules) and a MySQL server at 192.168.236.129.

http server: 192.168.236.128 (php parser based on modules) mysql server: 192.168.236.129

Compile and configure the HTTP server (Apache 2.4+). Because the system’s RPM packages for apr and apr-util are outdated, download and compile them from source.

Install required development groups on both machines:

yum groupinstall "Desktop Platform Development" "Server Platform Development" -y

Compile apr:

tar xf apr-1.4.6.tar.bz2
cd apr-1.4.6
./configure --prefix=/usr/local/apr
make && make install

Compile apr-util:

tar xf apr-util-1.4.1.tar.bz2
cd apr-util-1.4.1
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
make && make install

Compile Apache ( httpd):

tar xf httpd-2.4.6.tar.bz2
cd httpd-2.4.6
./configure --prefix=/usr/local/apache --sysconfdir=/etc/http24 \
  --enable-so --enable-modules=most --enable-mods-shared=most \
  --enable-proxy --enable-proxy-fcgi --enable-mpms-shared=all \
  --enable-cgi --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util \
  --with-z --with-ssl --with-mpm=event --enable-rewrite
make && make install

The build fails due to missing PCRE libraries, so install them: yum install pcre-devel -y After installing PCRE, rerun the make && make install command.

Configure httpd.conf (add DirectoryIndex, AddType for PHP) and create a startup script ( /etc/init.d/httpd24) with the usual init‑d functions (start, stop, reload, status, etc.). Make the script executable and enable it:

chmod +x /etc/init.d/httpd24
chkconfig --add httpd24
service httpd24 start

Verify the HTTP service is listening on port 80 with ss -tnlp.

On the MySQL host, install MariaDB from binary tarball:

tar zxvf mariadb-5.5.36-linux-x86_64.tar.gz
mv mariadb-5.5.36-linux-x86_64 /usr/local/
ln -sv /usr/local/mariadb-5.5.36-linux-x86_64 /usr/local/mysql
mkdir /etc/mysql
cp /usr/local/mysql/support-files/my-large.cnf /etc/mysql/my.cnf

Adjust the data directory, create /data, create mysql user/group, set ownership, and initialize the database:

mkdir /data
groupadd -r mysql
useradd -r -g mysql mysql
chown mysql:mysql /data
cd /usr/local/mysql
chown -R mysql:mysql ./
./scripts/mysql_install_db --user=mysql --datadir=/data --basedir=/usr/local/mysql

Install the MySQL service script and start the service:

cp support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld
service mysqld start

Verify MySQL is listening on port 3306.

Update the MySQL PATH environment:

echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh

Log in to MariaDB and create a test user:

mysql -u root -p
GRANT ALL ON *.* TO 'bwei'@'192.168.236.128' IDENTIFIED BY 'bwei';
FLUSH PRIVILEGES;

Compile PHP 5.4.19 on the HTTP server host (install required dev packages first):

yum -y install bzip2-devel libmcrypt-devel
tar xf php-5.4.19.tar.bz2
cd php-5.4.19
./configure --prefix=/usr/local/php \
  --with-apxs2=/usr/local/apache/bin/apxs --with-config-file-path=/etc \
  --with-config-file-scan-dir=/etc/php.d --with-libxml-dir=/usr --with-zlib \
  --with-bz2 --enable-xml --with-jpeg-dir --with-png-dir --with-freetype-dir \
  --enable-mbstring --with-mcrypt --enable-sockets --with-mysql=mysqlnd \
  --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-maintainer-zts
make && make install

If the build fails because xml2-config is missing, install it: yum install -y libxml2-devel Copy the production php.ini and test the PHP interpreter with a simple phpinfo() script and a MySQL connection test.

Install XCache for the PHP interpreter:

tar xf xcache-3.1.0.tar.bz2
cd xcache-3.1.0
/usr/local/php/bin/phpize
./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config --sysconfdir=/etc/php.d
make && make install
mkdir /etc/php.d
cp xcache.ini /etc/php.d/
sed -i 's|extension =.*|extension=/usr/local/php/lib/php/extensions/no-debug-zts-20100525/xcache.so|' /etc/php.d/xcache.ini
service httpd24 restart

Install phpMyAdmin, configure it to connect to the MariaDB host, and verify access via a browser.

Next, rebuild PHP with FPM on the MySQL host:

tar xf php-5.4.19.tar.bz2
cd php-5.4.19
./configure --prefix=/usr/local/php --enable-fpm \
  --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d \
  --with-openssl --with-zlib --with-bz2 --with-libxml-dir=/usr --enable-xml \
  --with-jpeg-dir --with-png-dir --with-freetype-dir --enable-mbstring \
  --with-mysql=/usr/local/mysql \
  --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-sockets --enable-zip
make && make install

Set up the PHP‑FPM service:

cp sapi/fpm/init.d.php-fpm /etc/init.d/fpm
chmod +x /etc/init.d/fpm
chkconfig --add fpm
service fpm start

Copy php.ini and configure php-fpm.conf (listen on 192.168.236.129:9000, adjust process manager settings) and restart the FPM service.

Install XCache for the FPM build using the same steps as before, then restart FPM.

Configure Apache on the HTTP host to proxy PHP‑FPM requests:

vim /etc/http24/httpd.conf
#Comment out the mod_php line
#LoadModule php5_module modules/libphp5.so
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.236.129:9000/php/$1

Deploy phpMyAdmin under /php/phpadmin on the PHP‑FPM host, adjust its config to point to the MariaDB server, and test it in a browser.

All steps completed; the LAMP stack with Apache, PHP‑FPM, MariaDB, XCache, and phpMyAdmin is operational.

Source: http://www.178linux.com/7705
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPXCacheCentOSphp-fpmMariaDBLAMP
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.