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

CentOS 6.5安装LAMP环境

2016-05-30 15:05 375 查看
1、安装编译工具:
yum -y install glibc-devel-2.* gcc-4.* gcc-c++-4.* libaio-devel-0.* libstdc++-devel-4.*

2、检查并卸载默认的低版本环
rpm -qa |grep -i http
rpm -qa |grep -i mysql
rpm -qa |grep -i php

rpm -e 软件包名 进行卸载。

【开始搭建LAMP环境】

搭建LAMP环境时,需要安装的所有软件都要按照一定的顺序安装,我们按照Apache->MySQL->PHP的顺序安装。但是在安 装PHP之前,应先安装PHP5需要的最新版本库文件,例如libxml2、libmcrypt,以及GD2库等文件。安装GD2库是为了让PHP5支持 GIF、PNG和JPEG图片格式,所以在安装GD2库之前还要先安装最新的zlib、libpng、freetype和jpegsrc等库文件。而且中 间还会穿插安装一些软件。

1、解压tar.gz为后缀的压缩包软件
LAMP环境搭建所需要的每个软件的源代码文件,都是以.tar.gz提供给我们的打包压缩文件,所以我们必须将其解压再解包。
批量解压命令:for i in `ls ./`;do tar -xf $i;done
在linux系统中源代码包安装过程
进行解压后的目录,LAMP环境搭建所需要的软件都使用C语言开发的,所以安装源代码文件最少需要配置(configure)、编译(make)、安装(make install)三个步骤。

2、安装libtools
./configure
make && make install && echo OK

3、安装libxml2最新库文
./configure --prefix=/usr/local/libxml2
make && make install && echo OK

注:解析报错信息为:/bin/rm: cannot remove `libtoolT': No such file or directory
解决方法:这时直接打开 configure,把 $RM “$cfgfile” 那行删除掉,重新再运行 ./configure 就可以了。

4、安装libmcrypt最新库文件

安装方法与上面安装libxml2方法相同,注意configure时的路径/usr/local/libmcrypt
如果安装成功就会在/usr/local/libmcrypt/目录下生成bin,include,lib,man,share五个目录。然后在安 装PHP5源代码包的配置时,就可以通过configure命令加上“--with-mcrypt-dir=/usr/local/libmcrypt” 选项,指定这个libmcrypt库文件的位置。

如./configure时报错:configure: error: C++ compiler cannot create executables 。

解决方案:

运行下面命令,然后重新configure(配置)

yum install gcc gcc-c++ gcc-g77
安装完成libmcrypt库以后,不同的linux系统版本有可能还要安装一下libltdl库。安装方法和前面的步骤相同,可以进入到解压缩的 目录/usr/local/src/libmcrypt-2.5.8下,找到libltdl库源代码所在的目录libltdl,进入这个目录按照下面几个 命令配置、编译、安装就可以了。

a.先安装libltdl
/usr/local/src/libmcrypt-2.5.8/libltdl

./configure --enables-ltdl-install
make && make install && echo OK

b、安装libmcrypt
./configure --prefix=/usr/local/libmcrypt
make && make install && echo OK

5、安装zlib最新库文件
./configure --prefix=/usr/local/libmcrypt
make && make install && echo OK

6、安装libpng最新库文件
./configure --prefix=/usr/local/libpng
make && make install && echo OK

注:如果提示:configure: error: ZLib not installed

解决方法如下:
1.进入zlib的源文件目录,执行命令 make clean,清除zlib;
2.重新配置 ./configure,后面不要接--prefix参数;

3.make && make install;
4.进入libpng目录,执行命令 ./configure --prefix=/usr/local/libpng;
5.make && make install;
6.安装成功.

7、安装jpeg8最新库文件

mkdir -p mkdir /usr/local/jpeg8/{bin,lib,include,man/man1}
./configure --prefix=/usr/local/jpeg8 --enable-share --enable-static

8、安装freetype最新库文件
cd /usr/local/src/freetype-2.5.3/builds/unix
./configure --prefix=/usr/local/freetype
cd -
make && make install && echo OK

9、安装atuoconf最新的库文件
./configure --prefix=/usr/local/autoconf
make && make install && echo OK

10、安装最新的GD库文件
./configure --prefix=/usr/local/gd --with-jpeg=/usr/local/jpeg8/ --with-png=/usr/local/libpng/ --with-freetype=/usr/local/freetype/ --with-zlib=/usr/local/zlib/
make && make install && echo OK

如果报错:
make[2]: *** [gd_png.lo] Error 1
make[2]: Leaving directory `/tmp/gd-2.0.35'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/tmp/gd-2.0.35'
make: *** [all] Error 2

解决方案:
vi gd_png.c
找到#include "png.h"改成#include "/usr/local/libpng/include/png.h"

11、安装新版本的apache服务器
a.卸载系统自带的apr、apr-util
yum remove apr apr-util -y

b. 安装apr 、apr-util
./configure --prefix=/usr/local/apr-httpd/ && make && make install

./configure --prefix=/usr/local/apr-util-httpd/ --with-apr=/usr/local/apr-httpd/ && make && make install
c、安装pcre
./configure --prefix=/usr/local/pcre
make && make install && echo OK

d、安装apache
./configure --prefix=/usr/local/apache243 --enable-mods-shared=all --enable-deflate --enable-speling --enable-cache --enable-file-cache --enable-disk-cache --enable-mem-cache --enable-so --enable-expires=shared --enable-rewrite=shared --enable-static-support --sysconfdir=/etc/httpd --with-z=/usr/local/zlib --with-apr=/usr/local/apr-httpd --with-apr-util=/usr/local/apr-util-httpd --with-pcre=/usr/local/pcre --disable-userdir

make && make install && echo OK

安装完成后,进入/usr/local/apache243/目录下,检查是否有以下文件:
bin build cgi-bin error htdocs icons include logs man manual modules

启动Apache服务器,并查端口是否开启,启动Apache服务器的命令行如下:
#/usr/local/apache243/bin/apachectl start

提示信息:
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using ::1. Set the 'ServerName' directive globally to suppress this message
解决方案:
vi /etc/httpd/httpd.conf
加上下面一行,重启apache
ServerName localhost:8

设置开机启动httpd:
echo "/usr/local/apache243/bin/apachectl start" >> /etc/rc.d/rc.local

12、安装MySQL数据库管理系统
a、增加用户名和用户组
#groupadd mysql
#useradd -g mysql mysq
b、安装mysql
注:checking for termcap functions library... configure: error: No curses/termcap library found解决
yum -y install ncurses-devel

./configure --prefix=/usr/local/mysql --enable-thread-safe-client --with-extra-charsets=all
make && make install && echo OK
(选:
创建mysql数据库服务器的配置文件,可以使用源码包support-files目录中的my-medium.cnf文件作为模板,将其复制到/etc/目录下,命名为my.cnf文件即可。
[root@localhost mysql-5.0.18]# cp support-files/my-medium.cnf /etc/my.cnf

1、如果还没安装过MySQL,必须创建MySQL授权表。进入到安装目录/usr/local/mysql下,执行bin目录下的mysql_install_db脚本,用来初始化MySQL数据库的授权表,其中存储了服务器访问允许。

[root@localhost mysql-5.0.18]#cd /usr/local/mysql
[root@localhost mysql]# bin/mysql_install_db --user=mysql //创建授权表

如果使用root用户运行上面的命令,应当使用--user选项,选项的值应当与你在第一步为运行服务器所创建的登录账户(mysql用户)相同。 如果用mysql用户登录来运行上面的命令,可以省略--user选项。用mysql_install_db创建mysql授权表后,需要手动重新启动服 务器。

2、将程序二进制的所有权改为root用户,数据目录的所有权改为运行mysqld程序的mysql用户。如果现在位于安装目录(/usr/local/mysql)下,命令行如下:
[root@localhost mysql]# chown -R root .(注意有点) //将文件的所有属性改为root用户
[root@localhost mysql]# chown -R mysql var //将数据目录的所有属性改为mysql用户
[root@localhost mysql]# chgrp -R mysql . //将组属性改为mysql组
[root@localhost mysql]# ls -l
total 40
drwxr-xr-x. 2 root mysql 4096 Feb 7 20:50 bin
drwxr-xr-x. 3 root mysql 4096 Feb 7 20:50 include
drwxr-xr-x. 2 root mysql 4096 Feb 7 20:50 info
drwxr-xr-x. 3 root mysql 4096 Feb 7 20:50 lib
drwxr-xr-x. 2 root mysql 4096 Feb 7 20:50 libexec
drwxr-xr-x. 3 root mysql 4096 Feb 7 20:50 man
drwxr-xr-x. 7 root mysql 4096 Feb 7 20:50 mysql-test
drwxr-xr-x. 3 root mysql 4096 Feb 7 20:50 share
drwxr-xr-x. 5 root mysql 4096 Feb 7 20:50 sql-bench
drwx------. 4 mysql mysql 4096 Feb 7 20:53 var

3、在所需要的东西被安装完成后,应当使用下面的命令启动MySQL服务了,命令行如下:
[root@localhost mysql]#/usr/local/mysql/bin/mysqld_safe --user=mysql &
报告信息(不理会):
[root@localhost mysql]# /usr/local/mysql/bin/mysqld_safe --user=mysql &
[1] 4720
[root@localhost mysql]# nohup: ignoring input and redirecting stderr to stdout
Starting mysqld daemon with databases from /usr/local/mysql/var

4、MySQL数据库服务器启动之后,查看一下它的端口3306是否打开,如果看到以下结果表明mysql服务启动成功。命令行如下
[root@localhost mysql]# netstat -tnl|grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN

5、使用mysqladmin验证服务器在运行中。以下命令提供了简单的测试,可检查服务器是否已经开启并能响应连接。命令行如下:
[root@localhost mysql]# bin/mysqladmin version
Enter password:
bin/mysqladmin Ver 8.41 Distrib 5.0.18, for pc-linux-gnu on i686
Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL license
Server version 5.0.18-log
Protocol version 10
Connection Localhost via UNIX socket
UNIX socket /tmp/mysql.sock
Uptime: 18 min 59 sec
Threads: 1 Questions: 3 Slow queries: 0 Opens: 0 Flush tables: 1 Open tables: 6 Queries per second avg: 0.003

[root@localhost mysql]# bin/mysqladmin variables //查看所有mysql参数

6、设置访问权限,在mysql安装过程中,使用mysql_install_db程序安装了mysql数据库授权表,表定义了初始mysql用户 账户和访问权限,所有初始化账户均没有密码。这些账户为超用户账户,可以执行任何操作。初始root账户的密码为空,因此任何人可以用root账户不用任 何密码来连接mysql服务器,并具有所有权限,这意味着mysql安装未受保护。如果你想要防止客户端不使用密码用匿名用户来连接,你应当为匿名账户指 定密码或删掉匿名帐户,应当为mysql root账户指定密码。使用mysql -u root启动mysql客户端控制台,连接mysql服务器。命 令行如下:

[root@localhost mysql]# bin/mysql -u root //没有密码可直接登录本机服务器
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5 to server version: 5.0.18-log
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

如果有匿名账户存在,它拥有全部的权限,因此删掉它可以提高安全,在mysql客户端执行SQL语如下:
mysql> delete from mysql.user where host='localhost' and user='';
Query OK, 1 row affected (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

7、可以用几种方法为root账户指定密码,我们选择用其中的一种。在mysql客户端命令行上使用set password指定密码,一定要使 用password()函数来加密密码。例如下面设置localhost域的密码为ios100。其他域可以使用同样的语句,使用的SQL语句如下。
mysql> set password for 'root'@'localhost'=password('ios100');
Query OK, 0 rows affected (0.00 sec)

8、如果想退出mysql客户端,可以在mysql客户端提示符下输入命令exit或者quit,还可以按键盘ctrl+c组合键,都可以从 mysql客户端退出。因为已经给mysql服务器的root账号设置了密码,所以再次登录mysql客户端就要提供密码才能进入。退出mysql客户端 和重新启动mysql客户端的控制台命令如下。
mysql> exit
Bye

[root@localhost mysql]# bin/mysql -u root -h localhost -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6 to server version: 5.0.18-log
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

如果想关闭mysql服务器,在命令行使用mysql服务器的mysqladmin命令,通过-u参数给出mysql数据库管理员用户名root和通过-p参数给出密码,即可关闭mysql服务器。如下示:
[root@localhost mysql]# bin/mysqladmin -u root -p shutdown
Enter password:
STOPPING server from pid file /usr/local/mysql/var/localhost.pid
130207 21:27:05 mysqld ended
[1]+ Done /usr/local/mysql/bin/mysqld_safe --user=mysql

9、mysql服务器和apache服务器一样也有必要设置为开机自动运行,设置方法进入到mysql源代码目录/usr/local/src /mysql-5.0.18,将子目录support-files下的mysql.server文件复制到/etc/rc.d/init.d目录中,并重 命名为mysqld,命令行如下。
[root@localhost mysql]# cd /usr/local/src/mysql-5.0.18
[root@localhost mysql-5.0.18]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld

修改一下权限
[root@localhost mysql-5.0.18]# chown root.root /etc/rc.d/init.d/mysqld
[root@localhost mysql-5.0.18]# chmod 755 /etc/rc.d/init.d/mysqld

把mysqld添加到chkconfig中
[root@localhost mysql-5.0.18]# chkconfig --add mysqld

在图形和字符集界面下自动启动mysqld
[root@localhost mysql-5.0.18]# chkconfig --level 3 mysqld on
[root@localhost mysql-5.0.18]# chkconfig --level 5 mysqld on

再使用chkconfig --list命令检查设置
[root@localhost mysql-5.0.18]# chkconfig --list mysqld
mysqld 0:off 1:off 2:on 3:on 4:off 5:on 6:off
)

13、安装最新版的PHP模块

./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql=/usr/local/mysql/ --with-libxml-dir=/usr/local/libxml2/ --with-png-dir=/usr/local/libpng/ --with-jpeg-dir=/usr/local/jpeg8/ --with-freetype-dir=/usr/local/freetype/ --with-gd=/usr/local/gd/ --with-zlib-dir=/usr/local/zlib/ --with-mcrypt=/usr/local/libmcrypt/ --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-soap --enable-mbstring=all --enable-sockets

注:报错信息:
If configure fails try --with-vpx-dir=<DIR> configure: error: jpeglib.h not found.

解决方法:
yum -y install libjpeg-devel

重新./configure
make(配置)

提示信息:
Build complete.
Don't forget to run 'make test'.

解决方案:
不要make test 直接make install
配置时可能会出现下面的错误:
checking for MySQL support... yes
checking for specified location of the MySQL UNIX socket... no
checking for MySQL UNIX socket location... no
configure: error: Cannot find libmysqlclient_r under /usr/local/mysql. Note that the MySQL client library is not bundled anymore!

其实这跟PHP没有关系,那是因为在编译APACHE的时候,使用--with-mpm模块,所以就必须在编译MYSQL的时候加上 --enable-thread-safe-client.参数
这是PHP5.2的一个改进,在PHP5.2.0之前的版本都不需要MYSQL启用安全线程。关于--enable-thread-safe- client项的官方介绍如下:如何生成线程式客户端库总是线程安全的。最大的问题在于从套接字读取的net.c中的子程序并不是中断安全的。或许你可能 希望用自己的告警中断对服务器的长时间读取,以此来解决问题。如果为SIGPIPE中断安装了中断处理程序,套接字处理功能应是线程安全的。 SupeSite/X-为了避免连接中断时放弃程序,MySQL将在首次调用mysql_server_init()、mysql_init()或 mysql_connect()时屏蔽SIGPIPE。如果你打算使用自己的SIGPIPE处理程序,首先应调用 mysql_server_init(),然后安装你的处理程序.
还有第二种解决方法比较方便 :编译之前,先处理一下mysql的库,默认查找libmysqlclient_r.so,可是mysql默认为libmysqlclient.so,内容完全一样,做个链接即可

# cd /usr/local/mysql/lib/mysql/
# ln -s libmysqlclient.so.15.0.0 libmysqlclient_r.so

(以上解决方法来自互联网!)
还会报make: *** [ext/gd/gd.lo] error

解决方法如下:
好像说这个错误算是php5.4的bug,下面对应的两篇文章有对应的说明: https://bugs.php.net/bug.php?id=55224 https://bugs.php.net/bug.php?id=60108

解决方法:
vi <gd_dir>/include/gd_io.h
gdIOCtx结构中增加void *data;
格式如下
typedef struct gdIOCtx
{
int (*getC) (struct gdIOCtx *);
int (*getBuf) (struct gdIOCtx *, void *, int);

void (*putC) (struct gdIOCtx *, int);
int (*putBuf) (struct gdIOCtx *, const void *, int);

/* seek must return 1 on SUCCESS, 0 on FAILURE. Unlike fseek! */
int (*seek) (struct gdIOCtx *, const int);

long (*tell) (struct gdIOCtx *);

void (*gd_free) (struct gdIOCtx *);
void (*data);
}
gdIOCtx;

我的GD安装在/usr/local/gd2目录下,所以是#vi vi /usr/local/gd/include/gd_io.h
libltdl.so.3: cannot open shared object file: No such file or directory
make: *** [ext/phar/phar.php] Error 127

解决方法:
ln -s /usr/local/lib/libltdl.so.3 /usr/lib/libltdl.so.3
[root@localhost ~]# cd /usr/local/libpng/lib/
[root@localhost lib]# ls
libpng15.a libpng15.so libpng15.so.15.10.0 libpng.la pkgconfig
libpng15.la libpng15.so.15 libpng.a libpng.so

可以看到libpng15.so.15
然后修改/etc/ld.so.conf 文件:vi /etc/ld.so.conf
在第一行下面追加/usr/local/libpng/lib这个路径。
然后重新编译安装即可。
提示信息:
=====================================================================
=====================================================================
WARNED TEST SUMMARY
---------------------------------------------------------------------
Bug #52062 (large timestamps with DateTime::getTimestamp and DateTime::setTimestamp) (32 bit) [ext/date/tests/bug52062.phpt] (warn: XFAIL section but test passes)
=====================================================================
You may have found a problem in PHP.
This report can be automatically sent to the PHP QA team at http://qa.php.net/reports and http://news.php.net/php.qa.reports This gives us a better understanding of PHP's behavior.
If you don't want to send the report immediately you can choose
option "s" to save it. You can then email it to qa-reports@lists.php.net later.
Do you want to send this report now? [Yns]:

解决方案:
不要make test 直接make install

安装完成后,需要建立PHP配置文件。

在使用configure命令安装配置时使用“--with-config-file-path=/usr /local/php/etc/”选项,指定了配置文件的位置。

将源码包下面的php.ini-development 文件复制到/usr/local /php/etc/中,并改名为php.ini即可,如下示:

[root@localhost php-5.4.11]# cp php.ini-development /usr/local/php/etc/php.ini

修改php.ini 把;date.timezone 前面的分号去掉,改成date.timezone ="PRC"
整合Apache与PHP,上面编译之前,我们使用configure命令安装配置时,使用--with-apxs2=/usr/local /apache242/bin/apxs选项以使Apache

将PHP作为功能模块使用。但我们还要修改Apahce配置文件,添加PHP的支持,告 诉Apache将哪些后缀作为PHP解析。例如,让Apache把.php或.phtml后缀名解析为PHP.使用vi打开Apache的配置文件 /etc/httpd/httpd.conf,找到AddType application/x-gzip .gz .tgz指令选项,并在其下方添加一 条指令AddType application/x-httpd-php .php .phtml。也可以将任何后缀的文件解析为PHP,只要在添加的语 句中加入并用空格分开,这里以多添加一个.phtml来示例,

如下示:
# If the AddEncoding directives above are commented-out, then you
# probably should define those extensions to indicate media types:
#
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType application/x-httpd-php .php .phtml

修改完成后必须重启Apache服务器,才能重新加载配置文件使修改生效。

[root@localhost php-5.4.11]#/usr/local/apache243/bin/apachectl stop
[root@localhost php-5.4.11]#/usr/local/apache243/bin/apachectl start

测试PHP环境是否可以正常运行,在/usr/local/apache243/htdocs目录下建一个test.php或test.phtml的文件,内容如下示:

<?php
phpinfo();
?>

打开浏览器,在地址栏输入http://192.168.146.129/test.php来运行该文件,如果出现下图内容,说明LAMP环境安装成功。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Linux CentOS LAMP