您的位置:首页 > 运维架构 > Apache

LAMP运行环境的简单搭建 推荐

2016-12-21 15:43 218 查看
LAMP运行环境的简单搭建

LAMP指的是Linux+Apache+MySQL+PHP,一组常用来搭建动态网站或服务器的开源软件,其本身都是独立的程序。LAMP网站架构是目前国际流行的Web框架,很多商业应用都是采取这个架构,和Java/J2EE架构相比,LAMP具有Web资源丰富、轻量、快速开发等特点。

一、安装准备
1、安装环境本次安装为手动编译安装,所需要的安装环境为已经安装"Compatibility libraries"及"Development tools"软件包的CentOS64位操作系统(内核版本为2.6.32)。如果未安装这两个软件包,可以使用yum install gcc进行安装。
2、软件包及依赖包为避免在安装过程中出现版本不兼容的问题,本次安装的软件包及版本如下:Apache:httpd-2.4.23.tar.gz
MySQL:mysql-5.5.36-linux2.6-x86_64.tar.gz

php:php-5.6.27.tar.gz

本次安装所需依赖包及其版本如下:
apr-1.5.2.tar.gz、apr-util-1.5.4.tar.gz、pcre-8.32.tar.gz

libmcrypt-2.5.8-9.el6.x86_64.rpm、libmcrypt-devel-2.5.8-9.el6.x86_64.rpm

mhash-0.9.9.9-3.el6.x86_64.rpm、mhash-devel-0.9.9.9-3.el6.x86_64.rpm

需要说明的是:以上安装包及依赖包都放在root用户家目录下

二、开始安装

安装顺序为Apache ->MySQL ->PHP

1、安装Apache

在安装Apache之前我们需要先解决依赖关系——安装apr、apr-util和pcre包,安装过程如下:
#  安装apr
[root@a ~]# tar -xzf apr-1.5.2.tar.gz
[root@a ~]# cd apr-1.5.2
# 选择安装路径为/usr/local/apr
[root@a apr-1.5.2]# ./configure --prefix=/usr/local/apr
[root@a apr-1.5.2]# make && make install

# 安装apr-util
[root@a ~]# tar -xzf apr-util-1.5.4.tar.gz
[root@a ~]# cd apr-util-1.5.4
# 选择安装路径为/usr/local/apr-util,并解决apr包依赖
[root@a apr-util-1.5.4]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@a apr-util-1.5.4]# make && make install

# 安装pcre
[root@a ~]# tar -xzf pcre-8.32.tar.gz
[root@a ~]# cd pcre-8.32
[root@a pcre-8.32]# ./configure --prefix=/usr/local/pcre
[root@a pcre-8.32]# make && make install
现在我们就可以编译安装httpd包了,过程如下:
[root@a ~]#  tar -xzf httpd-2.4.23.tar.gz
[root@a ~]#  cd httpd-2.4.23
# 安装路径为/usr/local/apache;apr、apr-util依赖路径分别如下;
#  --sysconfdir=/etc/httpd表示配置文件路径为/etc/httpd目录
[root@a httpd-2.4.23]#  ./configure --prefix=/usr/local/apache \
--enable-so --enable-rewirte --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 \
--sysconfdir=/etc/httpd  --with-pcre=/usr/local/pcre
[root@a httpd-2.4.23]#  make && make install
如果没什么问题的话,Apache就安装完成了。启动服务来测试一下吧
#  我们知道httpd服务的端口为80,保险起见,我们先来看一下是否已经有本地httpd服务正在运行
[root@a ~]#  netstat -tunlp | grep 80

#  如果80端口没有被占用,我们可以执行如下命令启动httpd服务
[root@a ~]#  /usr/local/apache/bin/apachectl start

# 可能会出现如下信息: 原因是我们没有为httpd服务设置ServerName
AH00557: httpd: apr_sockaddr_info_get() failed for a
AH00558: httpd: Could not reliably determine the server's fully qualified domain name,
using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message

# 编辑配置文件/etc/httpd/http.conf,将ServerName前的注释# 去掉即可,重启服务,进行测试
在浏览器输入linux主机的IP地址,出现如下界面,表示Apache安装成功




2、安装MySQL
在安装前,我们需要为mysql创建一个独立的数据目录,并将此文件夹的属主、属组改为mysql,且去除其它用户的所有权限,命令如下:
[root@a ~]#  mkdir /data
[root@a ~]#  useradd -s /sbin/nologin mysql
[root@a ~]#  chown -R mysql.mysql /data
[root@a ~]#  chmod -R o= /data
开始编译安装mysql吧

# 将mysql-5.5.36-linux2.6-x86_64.tar.gz解压到/usr/local/目录下,创建软链接,改变属主、属组为mysql
[root@a ~]#  tar -xzf mysql-5.5.36-linux2.6-x86_64.tar.gz -C /usr/local/
[root@a ~]#  cd /usr/local/
[root@a local]#  ln -s mysql-5.5.36-linux2.6-x86_64 mysql
[root@a local]#  chown -R mysql.mysql mysql

# 初始化mysql安装
[root@a local]#  cd mysql
[root@a mysql]#  scripts/mysql_install_db --user=mysql --datadir=/data/
出现如下结果表示初始化成功



# 防止其他用户查看,我们把mysql目录下的文件的属主改为root
[root@a mysql]#  chown -R root /usr/local/mysql/*

# 复制support-files目录下的mysql.server(服务脚本)文件到/etc/init.d/目录下,并改名为mysqld
[root@a mysql]#  cp support-files/mysql.server /etc/init.d/mysqld

# 添加mysqld服务
[root@a mysql]#  chkconfig --add mysqld

# 在support-files目录下选择合适的配置文件(主要看内存free -m)复制到/etc/my.cnf
[root@a mysql]# free -m    # 查看自己的内存

# 选择合适的配置文件复制到/etc/my.cnf
[root@a mysql]#  cp  support-files/my-合适的文件 /etc/my.cnf

# 修改mysql的配置文件 [mysqld]子域中
thread_concurrency= CPU个数*2
并在后面添加 datadir = /data
到此 mysqld服务可以启动: service mysqld start



为了安全起见及以后php链接mysql测试,我们要在mysql安装完成后为root用户添加密码
# linux命令修改密码
[root@a mysql]#  mysqladmin -u root -h localhost password 'NEW_PASS'

# 进入mysql交互模式改密码
mysql> SET PASSWORD FOR 'USERNAME'@'HOST'=PASSWORD('new_pass');

# 直接修改mysql.user表中Password字段值,重读配置文件
mysql> UPDATE mysql.user SET PASSWORD=PASSWORD('new_pass') WHERE CONDITION;
mysql> FLUSH PRIVILEGES;


3、安装PHP
解决依赖关系——安装libmcrypt、libmcrypt-devel、mhash、mhash-devel、llibxml2、libxml2-devel、bzip2-devel等包
[root@a ~]#  yum install libxml2 libxml2-devel bzip2-devel
[root@a ~]#  rpm -ivh libmcrypt-2.5.8-9.el6.x86_64.rpm libmcrypt-devel-2.5.8-9.el6.x86_64.rpm
[root@a ~]# rpm -ivh mhash-0.9.9.9-3.el6.x86_64.rpm mhash-devel-0.9.9.9-3.el6.x86_64.rpm
开始编译安装PHP
[root@a ~]#  tar -xzf php-5.6.27.tar.gz
[root@a ~]#  cd php-5.6.27
[root@a php-5.6.27]#  ./configure --prefix=/usr/local/php --with-mysqli=/usr/local/mysql \
--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/apache/bin/apxs --with-mcrypt \
--with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 \
--enable-maintainer-zts
[root@a php-5.6.27]#  make && make install
复制php解压文件夹下的php.ini-production到/etc/php.ini(--with-config-file-path定义的路径)
# php.ini-development:用于开发环境
# php.ini-production:用于生产环境
[root@a php-5.6.27]# cp php.ini-production /etc/php.ini
# 修改httpd服务的配置文件/etc/httpd/httpd.conf,添加如下内容:
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
# 在DirectoryIndex后添加index.php主页文件,即改为
DirectoryIndex index.php index.html
测试
在/usr/lcoal/apache/htdocs/目录下新建一个index.php文件,内容如下:<?php phpinfo(); ?>.刷新浏览器,若出现如下界面,表示apache和php整合成功



修改index.php文件,内容如下:
<?php
$conn=mysqli_connect('localhost','mysql用户名','对应密码');
if ($conn)
echo "success...";
else
echo "fail...";
phpinfo();
?>
刷新浏览器,若出现如下界面,表示LAMP运行环境搭建成功




三、安装后的工作
虽然我们编译安装成功了,但是我们发现,每次启动或重启相关服务、执行相关命令时,使用的总是绝对路径,这样感觉太麻烦了;而且我们想要看一下编译安装生成的可执行命令时,发现man不出结果。所以还需要做一些工作,使LAMP运行环境更易用

1、添加环境变量
对于编译安装生成的bin目录下的可执行文件,我们可通过追加环境变量的方式来使命令在任何目录下均可执行,具体方法如下:

# 在/etc/profile.d/目录下httpd.sh、mysqld.sh文件,内容分别如下:
[root@a profile.d]#  cat httpd.sh
export PATH=$PATH:/usr/local/apache/bin
[root@a profile.d]#  cat mysqld.sh
export PATH=$PATH:/usr/local/mysql/bin

# 然后执行如下命令即可完成对环境变量的添加
[root@a profile.d]#  source httpd.sh
[root@a profile.d]#  source mysql.sh

# 由于/usr/local/php/bin下的可执行文件基本没用到,我们可以不做添加

2、添加man文件路径
对于编译安装承德的bin目录下的可执行文件,我们可以通过追加man文档路径的方式来使该命令在使用man帮助时,输出该命令的帮助信息,具体如下:

# 在/etc/man.config文件中添加以下内容:
MANPATH /usr/local/apache/man
MANPATH /usr/local/mysql/man
MANPATH /usr/local/php/php/man


3、添加httpd服务
在做完上述配置后,我们发现命令可以直接执行了,也可以直接查看man帮助了,但是我们的httpd服务好像只能通过如下命令来改变状态。
[root@a ~]#  apachectl start|stop|restart|reload
我们想要让httpd服务像其他服务一样,通过service httpd start|stop|restart|reload该怎么做呢?我们可以修改配置文件,并在/etc/init.d/目录下新建httpd文件(若存在则修改),具体如下:
# 修改/etc/httpd/httpd.conf配置文件,在最后添加如下内容:
PidFile "/var/run/httpd.pid"

# 新建/etc/init.d/httpd服务脚本,内容如下:

#!/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/apache/bin/apachectl
httpd=${HTTPD-/usr/local/apache/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
这样我们就可以使用service httpd start|stop|restart|reload来改变httpd服务状态了
#  设置开机启动
[root@a ~]#  chkconfig --add httpd
[root@a ~]# chkconfig httpd on
[root@a ~]# chkconfig --list | grep httpd
httpd           0:off   1:off   2:on    3:on    4:on    5:on    6:off
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux MySQL apache