LAMP运行环境的简单搭建
LAMP指的是Linux+Apache+MySQL+PHP,一组常用来搭建动态网站或服务器的开源软件,其本身都是独立的程序。LAMP网站架构是目前国际流行的Web框架,很多商业应用都是采取这个架构,和Java/J2EE架构相比,LAMP具有Web资源丰富、轻量、快速开发等特点。
一、安装准备
1、安装环境
本次安装为手动编译安装,所需要的安装环境为已经安装"Compatibility libraries"及"Development tools"软件包的CentOS64位操作系统(内核版本为)。如果未安装这两个软件包,可以使用yum install gcc进行安装。
2、软件包及依赖包
为避免在安装过程中出现版本不兼容的问题,本次安装的软件包及版本如下:
-
Apache:httpd-
-
MySQL:mysql---x86_
-
php:php-
本次安装所需依赖包及其版本如下:
-
apr-、apr-util-、pcre-
-
libmcrypt--_、libmcrypt-devel--_
-
mhash--_、mhash-devel--_
需要说明的是:以上安装包及依赖包都放在root用户家目录下
二、开始安装
安装顺序为Apache ->MySQL ->PHP
1、安装Apache
在安装Apache之前我们需要先解决依赖关系——安装apr、apr-util和pcre包,安装过程如下:
# 安装apr [root@a ~]# tar -xzf apr- [root@a ~]# cd apr- # 选择安装路径为/usr/local/apr [root@a apr-]# ./configure --prefix=/usr/local/apr [root@a apr-]# make && make install # 安装apr-util [root@a ~]# tar -xzf apr-util- [root@a ~]# cd apr-util- # 选择安装路径为/usr/local/apr-util,并解决apr包依赖 [root@a apr-util-]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr [root@a apr-util-]# make && make install # 安装pcre [root@a ~]# tar -xzf pcre- [root@a ~]# cd pcre- [root@a pcre-]# ./configure --prefix=/usr/local/pcre [root@a pcre-]# make && make install
现在我们就可以编译安装httpd包了,过程如下:
[root@a ~]# tar -xzf httpd- [root@a ~]# cd httpd- # 安装路径为/usr/local/apache;apr、apr-util依赖路径分别如下; # --sysconfdir=/etc/httpd表示配置文件路径为/etc/httpd目录 [root@a httpd-]# ./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-]# 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 . Set the 'ServerName' directive globally to suppress this message # 编辑配置文件/etc/httpd/,将ServerName前的注释# 去掉即可,重启服务,进行测试
在浏览器输入linux主机的IP地址,出现如下界面,表示Apache安装成功
2、安装MySQL
在安装前,我们需要为mysql创建一个独立的数据目录,并将此文件夹的属主、属组改为mysql,且去除其它用户的所有权限,命令如下:
[root@a ~]# mkdir /data [root@a ~]# useradd -s /sbin/nologin mysql [root@a ~]# chown -R /data [root@a ~]# chmod -R o= /data
开始编译安装mysql吧
# 将mysql---x86_解压到/usr/local/目录下,创建软链接,改变属主、属组为mysql [root@a ~]# tar -xzf mysql---x86_ -C /usr/local/ [root@a ~]# cd /usr/local/ [root@a local]# ln -s mysql---x86_64 mysql [root@a local]# chown -R 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目录下的(服务脚本)文件到/etc//目录下,并改名为mysqld [root@a mysql]# cp support-files/ /etc//mysqld # 添加mysqld服务 [root@a mysql]# chkconfig --add mysqld # 在support-files目录下选择合适的配置文件(主要看内存free -m)复制到/etc/ [root@a mysql]# free -m # 查看自己的内存 # 选择合适的配置文件复制到/etc/ [root@a mysql]# cp support-files/my-合适的文件 /etc/ # 修改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'); # 直接修改表中Password字段值,重读配置文件 mysql> UPDATE 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--_ libmcrypt-devel--_ [root@a ~]# rpm -ivh mhash--_ mhash-devel--_
开始编译安装PHP
[root@a ~]# tar -xzf php- [root@a ~]# cd php- [root@a php-]# ./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/ --with-bz2 \ --enable-maintainer-zts [root@a php-]# make && make install
复制php解压文件夹下的-production到/etc/(--with-config-file-path定义的路径)
# -development:用于开发环境 # -production:用于生产环境 [root@a php-]# cp -production /etc/
# 修改httpd服务的配置文件/etc/httpd/,添加如下内容: AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps # 在DirectoryIndex后添加主页文件,即改为 DirectoryIndex
测试
在/usr/lcoal/apache/htdocs/目录下新建一个文件,内容如下:<?php phpinfo(); ?>.刷新浏览器,若出现如下界面,表示apache和php整合成功
修改文件,内容如下:
<?php
$conn=mysqli_connect('localhost','mysql用户名','对应密码');
if ($conn)
echo "success...";
else
echo "fail...";
phpinfo();
?>
刷新浏览器,若出现如下界面,表示LAMP运行环境搭建成功
三、安装后的工作
虽然我们编译安装成功了,但是我们发现,每次启动或重启相关服务、执行相关命令时,使用的总是绝对路径,这样感觉太麻烦了;而且我们想要看一下编译安装生成的可执行命令时,发现man不出结果。所以还需要做一些工作,使LAMP运行环境更易用
1、添加环境变量
对于编译安装生成的bin目录下的可执行文件,我们可通过追加环境变量的方式来使命令在任何目录下均可执行,具体方法如下:
# 在/etc//目录下、文件,内容分别如下: [root@a ]# cat export PATH=$PATH:/usr/local/apache/bin [root@a ]# cat export PATH=$PATH:/usr/local/mysql/bin # 然后执行如下命令即可完成对环境变量的添加 [root@a ]# source [root@a ]# source # 由于/usr/local/php/bin下的可执行文件基本没用到,我们可以不做添加
2、添加man文件路径
对于编译安装承德的bin目录下的可执行文件,我们可以通过追加man文档路径的方式来使该命令在使用man帮助时,输出该命令的帮助信息,具体如下:
# 在/etc/文件中添加以下内容: 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//目录下新建httpd文件(若存在则修改),具体如下:
# 修改/etc/httpd/配置文件,在最后添加如下内容: PidFile "/var/run/" # 新建/etc//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/ # config: /etc/sysconfig/httpd # pidfile: /var/run/ # Source function library. . /etc///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/ 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/} 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