需要yum源的,可以参考我之前的文章:

http://www.muyesanren.com/2017/03/18/centos-require-aliyun-yum-repo/

 

安装下基本的所需组件:

yum groupinstall “Development Tools” “Additional Development”

 

一、安装Apache httpd:

先安装一个依赖包:

yum -y install pcre-devel

安装apr:

tar xf apr-1.4.6.tar.gz

cd apr-1.4.6

./configure –prefix=/usr/local/apr

make

make install

安装apr-util包:

tar xf apr-util-1.5.4.tar.gz

cd apr-util-1.5.4

./configure –prefix=/usr/local/apr-util –with-apr=/usr/local/apr

make

make install

开始安装httpd:

tar xf httpd-2.4.20.tar.gz

cd httpd-2.4.20

./configure --prefix=/usr/local/httpd \
--sysconfdir=/etc/httpd \
--enable-so \
--enable-rewrite \
--enable-ssl \
--enable-cgi \
--enable-cgid \
--enable-modules=most \
--enable-mods-shared=most \
--enable-mpms-shared=all \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util

make && make install

额外说明:

–enable-so必须指定,不然php将无法被apache加载!

 

修改httpd的主配置文件,设置其Pid文件的路径

编辑/etc/httpd/httpd.conf,添加如下行, 最后一行也行!(为下方脚本作准备,不加会报错):

PidFile “/var/run/httpd.pid”

#!/bin/bash
#
# httpd        Startup script for the Apache HTTP Server
#
# chkconfig: - 85 15
# description: Apache is a World Wide Web server.  It is used to serve \
#	       HTML files and CGI.
# processname: httpd
# config: /etc/httpd/conf/httpd.conf
# config: /etc/sysconfig/httpd
# pidfile: /var/run/httpd.pid

# Source function library.
. /etc/rc.d/init.d/functions

if [ -f /etc/sysconfig/httpd ]; then
        . /etc/sysconfig/httpd
fi

# Start httpd in the C locale by default.
HTTPD_LANG=${HTTPD_LANG-"C"}

# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""

# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
# with the thread-based "worker" MPM; BE WARNED that some modules may not
# work correctly with a thread-based MPM; notably PHP will refuse to start.

# Path to the apachectl script, server binary, and short-form for messages.
apachectl=/usr/local/httpd/bin/apachectl
httpd=${HTTPD-/usr/local/httpd/bin/httpd}
prog=httpd
pidfile=${PIDFILE-/var/run/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0

start() {
        echo -n $"Starting $prog: "
        LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch ${lockfile}
        return $RETVAL
}

stop() {
	echo -n $"Stopping $prog: "
	killproc -p ${pidfile} -d 10 $httpd
	RETVAL=$?
	echo
	[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
    echo -n $"Reloading $prog: "
    if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
        RETVAL=$?
        echo $"not reloading due to configuration syntax error"
        failure $"not reloading $httpd due to configuration syntax error"
    else
        killproc -p ${pidfile} $httpd -HUP
        RETVAL=$?
    fi
    echo
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  status)
        status -p ${pidfile} $httpd
	RETVAL=$?
	;;
  restart)
	stop
	start
	;;
  condrestart)
	if [ -f ${pidfile} ] ; then
		stop
		start
	fi
	;;
  reload)
        reload
	;;
  graceful|help|configtest|fullstatus)
	$apachectl $@
	RETVAL=$?
	;;
  *)
	echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"
	exit 1
esac

exit $RETVAL

可能出现的错误一:

Starting httpd: AH00558: httpd: Could not reliably determine the server’s fully qualified domain name, using 61.183.1.186. Set the ‘ServerName’ directive globally to suppress this message

解决如下:

vim /etc/httpd/httpd.conf

删除ServerName的注释#, 并设置为localhost或者是127.0.0.1

可能出现的错误二:

Stopping httpd: [FAILED]

Starting httpd: httpd (pid 17161) already running

解决: 请确保上方步骤中那个pid文件信息行行已加入/etc/httpd/httpd.conf

配置开机自启动:

chkconfig –add httpd

chkconfig httpd on

二、安装Mysql:

同: http://www.muyesanren.com/2017/03/19/centos6-lnmp-build-explain

三、安装PHP:

yum install -y libmcrypt libmcrypt-devel

tar xf php-5.6.24.tar.gz

cd php-5.6.24

./configure --prefix=/usr/local/php \
 --with-mysql=/usr/local/mysql \
 --with-openssl \
 --with-mysqli=/usr/local/mysql/bin/mysql_config \
 --enable-mbstring \
 --with-freetype-dir \
 --with-jpeg-dir \
 --with-png-dir \
 --with-zlib \
 --with-libxml-dir=/usr \
 --enable-xml \
 --enable-sockets \
 --with-apxs2=/usr/local/httpd/bin/apxs \
 --with-mcrypt \
 --with-config-file-path=/etc \
 --with-config-file-scan-dir=/etc/php.d \
 --with-bz2 \
 --enable-maintainer-zts

 

make && make install

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

编辑apache配置文件httpd.conf,以apache支持php

vim /etc/httpd/httpd.conf, 查找AddType出现的相似的代码块,或在最后面添加也行,如下:

AddType application/x-httpd-php .php

AddType application/x-httpd-php-source .phps

同样再定位至DirectoryIndex index.html , 修改为:

DirectoryIndex index.php index.html

 

创建/usr/local/httpd/htdocs/test.php, 插入如下代码:

<?php

phpinfo();

 

可能出现页面无法解析或者下载的情况:

排错方案一:查看apache是否加载了php模块,或者说modules目录下是否有存在php模块

ls -l /usr/local/httpd/modules/ | grep php

libphp5.so

如果有这个模块, 接着:

/usr/local/httpd/bin/httpd -M | grep php

php5_module (shared)

这两个都确定存在,最后再次 vim /etc/httpd/httpd.conf

查看是否有这么一行, 如果不存在就添加之:

LoadModule php5_module modules/libphp5.so

排错方案二:按照方案一设置了, 还是无法解决php无法解析问题

导出一下头文件和库文件

ln -sv /usr/local/httpd/include /usr/include/httpd

ldconfig

最后再次: service httpd restart, 然后再次测试, 一切OK!