Install LAMP on CentOS 6 & 7

2018-02-17|Categories: Linux, Shell script|

By yum

Installation

# CentOS 6
# If you want to run php as mod_php of httpd,
# then you do NOT need to install `php-fpm`.
yum install -y httpd mysql-server php php-mysql php-fpm

# CentOS 7
yum install -y httpd mariadb-server php php-mysql php-fpm

Configuration

# FastCGI (php-fpm) for httpd
cat >> /etc/httpd/conf/httpd.conf <<- EOF
    DirectoryIndex index.php
    ProxyRequests Off
    ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/var/www/html/\$1
EOF    

Startup

# CentOS 6
for i in httpd mysqld php-fpm; do
    service ${i} start
    chkconfig --add ${i}
    chkconfig ${i} on
done

# CentOS 7
for i in httpd mysqld php-fpm; do
    systemctl start ${i}
    systemctl enable ${i}
done

By compiling

I have written a script to do the LAMP compilation job:

#!/bin/bash
# vim: set fdm=marker:
#
#===== ===== ===== ===== ===== ===== ===== ===== ===== ===== ===== =====
# Filename:     auto_compile_lamp_by_liyang.sh
# Description:
# Date:         2018-02-14
# Author:       Li Yang
# Website:      https://liyang85.com
#===== ===== ===== ===== ===== ===== ===== ===== ===== ===== ===== =====

[ `id -u` -eq 0 ] || { echo "Only root can run this script." && exit 1; }

#===== ===== ===== ===== ===== ===== ===== ===== ===== =====
# Stop and remove yum installed LAMP {{{1
#===== ===== ===== ===== ===== ===== ===== ===== ===== =====
#
# the 3 left braces "{" is fold-marker for Vim

echo "Stopping and removing yum installed LAMP ..."

# If there is a yum installed LAMP version, remove it
for i in "httpd" "mysql-server" "mariadb-server" "php-mysql" "php-fpm"; do
    if [[ `yum list installed "${i}" &>/dev/null` ]]; then
        echo "Removing ${i} ..."    # testing
        yum remove -y "${i}"
    else
        :
    fi
done

# If port 80, 3306 or 9000 still be listened by a process, kill it
kill -9 `lsof -ti :80 -ti :3306 -ti :9000` 2>/dev/null

#===== ===== ===== ===== ===== ===== ===== ===== ===== =====
# Download all source tarballs {{{1
#===== ===== ===== ===== ===== ===== ===== ===== ===== =====

src="/src/lamp/"
[ -d "${src}" ] && cd "${src}" || { mkdir -p "${src}" && cd "${src}"; }

# all files host on qiniu.com for quick downloading
host="http://p3gtzlzk8.bkt.clouddn.com/"
shaFile="lamp-tarballs.sha256"
files[0]="apr-1.6.3.tar.bz2"
files[1]="apr-util-1.6.1.tar.bz2"
files[2]="httpd-2.4.29.tar.bz2"
files[3]="mariadb-10.2.12.tar.gz"
files[4]="php-7.2.1.tar.bz2"
files[5]="libiconv-1.14.tar.gz"
files[6]="lamp-test-phpinfo.php"
files[7]="lamp-test-mysql.php"

for i in ${files[@]}; do
    if [ -e "${i}" ]; then
        :
    else
        wget "${host}${i}"
    fi
done

#===== ===== ===== ===== ===== ===== ===== ===== ===== =====
# Verify integrity and extract all tarballs {{{1
#===== ===== ===== ===== ===== ===== ===== ===== ===== =====

[ -e "${shaFile}" ] || wget "${host}${shaFile}" &> /dev/null
# if verify failed, still there will be error, which can show details
sha256sum -c "${shaFile}" > /dev/null || exit 2

for j in ${files[@]}; do
    if [[ "${j}" =~ \.php$ ]]; then
        :
    else
        echo "Extracting ..."
        tar xf "${j}" \
            && echo "${j} extracted."
    fi
done

#===== ===== ===== ===== ===== ===== ===== ===== ===== =====
# Install all dependencies {{{1
#===== ===== ===== ===== ===== ===== ===== ===== ===== =====

yum -y groupinstall "development tools"

yum -y install \
    openssl-devel \
    pcre-devel    \
    expat-devel

# for php
yum -y install \
    freetype-devel      \
    gd-devel            \
    libcurl-devel       \
    libjpeg-turbo-devel \
    libpng-devel        \
    libxml2-devel       \
    libxslt-devel       \
    readline-devel      \
    zlib-devel

#===== ===== ===== ===== ===== ===== ===== ===== ===== =====
# Compile httpd 2.4 {{{1
#===== ===== ===== ===== ===== ===== ===== ===== ===== =====

cd "${src}${files[2]//.tar.bz2}" \
    && cp -r ../apr-1.6.3 srclib/apr           \
    && cp -r ../apr-util-1.6.1 srclib/apr-util \
    || exit 3

getent passwd apache &>/dev/null || useradd -r -s /sbin/nologin apache

./configure \
    --prefix=/app/httpd24    \
    --enable-so              \
    --enable-ssl             \
    --enable-cgi             \
    --enable-rewrite         \
    --with-zlib              \
    --with-pcre              \
    --with-included-apr      \
    --enable-modules=most    \
    --enable-mpms-shared=all \
    --with-mpm=prefork

make -j 2 && make install

#===== ===== ===== ===== ===== ===== ===== ===== ===== =====
# Configure httpd 2.4 {{{1
#===== ===== ===== ===== ===== ===== ===== ===== ===== =====

lampEnv="/etc/profile.d/lamp.sh"
echo 'PATH="/app/httpd24/bin:$PATH"' > "${lampEnv}"
source "${lampEnv}"

# 1) change owner and group of the httpd process;
# 2) change `ServerName` can fix the "AH00558: httpd: Could not reliably
#   determine the server's fully qualified domain name" error;
# 3) enable fastcgi module: proxy_module and proxy_fcgi_module both need
#   to be enabled;
# 4) include the separate fastcgi.conf which need to be created manually;
myServName=`hostname`
confPath="/app/httpd24/conf"
sed -i.bak -r \
    -e 's/(User )daemon/\1apache/'                      \
    -e 's/(Group )daemon/\1apache/'                     \
    -e "s/^#(ServerName ).*(:80)/\1${myServName}\2/"    \
    -e 's/^#(.* proxy_module .*)/\1/'                   \
    -e 's/^#(.* proxy_fcgi_module .*)/\1/'              \
    -e '$a Include conf/vhosts/*.conf'                  \
    "${confPath}/httpd.conf"

# fastcgi.conf
mkdir "${confPath}/vhosts"
cat > "${confPath}/vhosts/fastcgi.conf" <<- EOF
    DirectoryIndex index.php
    ProxyRequests Off
    ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/app/httpd24/htdocs/\$1
EOF

# fix the `AH00557: httpd: apr_sockaddr_info_get() failed` error
sed -i.bak -r "/127.0.0.1/s/$/ ${myServName}/" /etc/hosts

# copy testing files
cp "${src}${files[6]}" /app/httpd24/htdocs/
cp "${src}${files[7]}" /app/httpd24/htdocs/

#===== ===== ===== ===== ===== ===== ===== ===== ===== =====
# Compile mariadb 10.2 {{{1
#===== ===== ===== ===== ===== ===== ===== ===== ===== =====

cd "${src}${files[3]//.tar.gz}" || exit 3

getent passwd mysql &>/dev/null || useradd -r -s /sbin/nologin mysql

mysqlDir="/app/${files[3]//.tar.gz}"
mysqlDataDir="/mysqldb"
mkdir -p "${mysqlDir}" "${mysqlDataDir}" \
    && chown mysql.mysql "${mysqlDir}" "${mysqlDataDir}"

cmake . \
    -DCMAKE_INSTALL_PREFIX="${mysqlDir}"       \
    -DDEFAULT_CHARSET=utf8                     \
    -DDEFAULT_COLLATION=utf8_general_ci        \
    -DENABLED_LOCAL_INFILE=1                   \
    -DMYSQL_DATADIR="${mysqlDataDir}"          \
    -DMYSQL_UNIX_ADDR="${mysqlDir}/mysql.sock" \
    -DMYSQL_USER=mysql                         \
    -DWITH_ARCHIVE_STORAGE_ENGINE=1            \
    -DWITH_BLACKHOLE_STORAGE_ENGINE=1          \
    -DWITH_INNOBASE_STORAGE_ENGINE=1           \
    -DWITH_PARTITION_STORAGE_ENGINE=1          \
    -DWITHOUT_MROONGA_STORAGE_ENGINE=1         \
    -DWITH_DEBUG=0                             \
    -DWITH_LIBWRAP=0                           \
    -DWITH_READLINE=1                          \
    -DWITH_SSL=system                          \
    -DWITH_ZLIB=system

make -j 2 && make install

#===== ===== ===== ===== ===== ===== ===== ===== ===== =====
# Configure mariadb 10.2 {{{1
#===== ===== ===== ===== ===== ===== ===== ===== ===== =====

ln -s ${files[3]//.tar.gz} /app/mysql

echo 'PATH="/app/mysql/bin:$PATH"' >> "${lampEnv}"
source "${lampEnv}"

scripts/mysql_install_db \
    --basedir=/app/mysql        \
    --datadir="${mysqlDataDir}" \
    --user=mysql

cp support-files/my-huge.cnf /etc/my.cnf
cp support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld

#===== ===== ===== ===== ===== ===== ===== ===== ===== =====
# Compile php 7.2 (php-fpm) {{{1
#===== ===== ===== ===== ===== ===== ===== ===== ===== =====

# compile libiconv, which is a dependency
cd "${src}${files[5]//.tar.gz}" || exit 3
./configure --prefix=/usr/local/libiconv
make -j 2 && make install

cd "${src}${files[4]//.tar.bz2}" || exit 3
phpDir="/app/${files[4]//.tar.bz2}"
./configure \
    --prefix="${phpDir}"                 \
    --disable-rpath                      \
    --enable-bcmath                      \
    --enable-fpm                         \
    --enable-ftp                         \
    --enable-inline-optimization         \
    --enable-mbregex                     \
    --enable-mbstring                    \
    --enable-pcntl                       \
    --enable-shmop                       \
    --enable-short-tags                  \
    --enable-soap                        \
    --enable-sockets                     \
    --enable-static                      \
    --enable-sysvsem                     \
    --enable-xml                         \
    --with-curl                          \
    --with-fpm-group=apache              \
    --with-fpm-user=apache               \
    --with-freetype-dir                  \
    --with-gd                            \
    --with-iconv-dir=/usr/local/libiconv \
    --with-jpeg-dir                      \
    --with-libxml-dir=/usr               \
    --with-mysqli=mysqlnd                \
    --with-openssl                       \
    --with-pdo-mysql=mysqlnd             \
    --with-png-dir                       \
    --with-readline                      \
    --with-xmlrpc                        \
    --with-xsl                           \
    --with-zlib

make -j 2 && make install

#===== ===== ===== ===== ===== ===== ===== ===== ===== =====
# Configure php 7.2 (php-fpm) {{{1
#===== ===== ===== ===== ===== ===== ===== ===== ===== =====

ln -s "${files[4]//.tar.bz2}" /app/php

echo 'PATH="/app/php/bin:$PATH"' >> "${lampEnv}"
source "${lampEnv}"

cp php.ini-production /app/php/etc/php.ini

# enable opcache
sed -i.bak -r \
    -e 's/;(opcache.enable=).*/\11/'                    \
    -e 's/;(opcache.memory_consumption=).*/\1128/'      \
    -e 's/;(opcache.interned_strings_buffer=).*/\18/'   \
    -e 's/;(opcache.max_accelerated_files=).*/\110000/' \
    -e 's/;(opcache.validate_timestamps=).*/\11/'       \
    -e 's/;(opcache.revalidate_freq=).*/\160/'          \
    /app/php/etc/php.ini

# php-fpm
cp /app/php/etc/php-fpm.conf{.default,}
cp /app/php/etc/php-fpm.d/www.conf{.default,}
cp /src/lamp/"${files[4]//.tar.bz2}"/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
chkconfig --add php-fpm

#===== ===== ===== ===== ===== ===== ===== ===== ===== =====
# Start all services and testing {{{1
#===== ===== ===== ===== ===== ===== ===== ===== ===== =====
#
# Move all start operations to the end of the script is a good way
# to see the start logs.

# `apachectl` = `apachectl start` = `httpd -k start`
# `man httpd`: In general, httpd should NOT be invoked directly,
# but rather should be invoked via apachectl on Unix-based systems.
apachectl

# `mysqladmin` command must execute after MariaDB started,
# or it will fail.
# There must use double quotes to enclose the messages,
# single quotes will cause the line continuation (\) fails.
service mysqld start
echo "Waiting for mysqld startup done ..." && sleep 3
mysqladmin -u root password centos \
    && echo -e "Password of MariaDB's root has been set to 'centos' \
        for testing.\nYou should set a stronger one."

service php-fpm start

# Print phpinfo(), which has too many contents, so need the `-I`
# curl -I http://localhost/"${files[6]}"
# Test the connection to MariaDB
curl http://localhost/"${files[7]}"

Leave A Comment