记录关于lamp搭建的流程,这里只介绍Apache和PHP的安装,MySQL的安装请移步:lnmp环境搭建之mysql篇。
Apache的下载网址:http://httpd.apache.org/download.cgi
Apache2.4文档:http://httpd.apache.org/docs/2.4/zh-cn/
PHP官网:http://www.php.net/
这里安装的是Nginx2.4.28 PHP5.6 MySQL5.7.18
Linux版本:
Mysql版本:
搭建前准备:
由于Linux是最小化安装,所以要安装一些基础的工具包:
yum grouplist # 列出可以安装的yum rpm包组合
yum -y groupinstall "Development Tools" # 安装开发工具包组合
yum -y install wget apr-devel apr-util-devel pcre-devel
Apache安装:
cd /usr/local/src/ # 习惯性地进入src目录
wget -c http://www-us.apache.org/dist//httpd/httpd-2.4.28.tar.gz
tar -zxf httpd-2.4.28.tar.gz # 解压
cd httpd-2.4.28 # 进入源码目录
./configure -h|--help # 列出当前可用的所有参数,这步是多余的
./configure # 配置
make # 编译
make install # 编译安装
测试:
/usr/local/apache2/bin/apachectl -k start # 启动Apache服务
curl 127.0.0.1 # 测试
启动|停止命令:
/usr/local/apache2/bin/apachectl -k start # 启动
/usr/local/apache2/bin/apachectl -f /usr/local/apache2/conf/httpd.conf # 使用http.conf配置启动
/usr/local/apache2/bin/apachectl -k stop # 停止
/usr/local/apache2/bin/apachectl -k restart # 重启
/usr/local/apache2/bin/apachectl -k graceful # 平滑重启
/usr/local/apache2/bin/apachectl -k graceful-stop # 平滑停止
常见问题:
-bash: wget: command not found
yum -y install wget
configure: error: APR not found. Please read the documentation.
yum -y install apr-devel
configure: error: APR-util not found. Please read the documentation.
yum -y install apr-util-devel
configure: error: pcre-config for libpcre not found. PCRE is required and available from http://pcre.org/
yum -y install pcre-devel
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
vi /usr/local/apache2/conf/httpd.conf
ServerName 127.0.0.1:80
PHP安装:
准备工作:
yum -y install libxml2-devel bzip2-devel libcurl-devel libjpeg-turbo-devel libpng-devel freetype-devel libmcrypt
这里libmcrypt是没有安装成功的:
要找源码包手动安装:
cd /usr/local/src/ # 习惯性地把源码包放在src目录下
wget -c ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz # 下载libmcrypt源码包
tar -zxf libmcrypt-2.5.7.tar.gz # 解压
cd libmcrypt-2.5.7 # 进入源码目录
./configure # 配置
make # 编译
make install # 编译安装
安装:
cd /usr/local/src/ # 再次习惯性地把源码包放在src目录下
wget -c http://hk1.php.net/distributions/php-5.6.31.tar.gz # 下载源码包
tar -zxf php-5.6.31.tar.gz # 解压
cd php-5.6.31 # 进入源码目录,总是少不了这两步
./configure -h|--help # 列出当前可用的所有参数,这步是多余的
./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs --with-mcrypt \
--enable-mbstring --enable-pdo --with-curl --disable-debug --disable-rpath \
--enable-inline-optimization --with-bz2 --with-zlib --enable-sockets \
--enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex \
--with-mhash --enable-zip --with-pcre-regex --with-mysql --with-mysqli \
--with-gd --with-jpeg-dir --with-freetype-dir --enable-calendar --with-pdo-mysql=/usr/local/mysql
make && make install # 编译并编译安装
测试:
/usr/local/php/bin/php -v
复制php.ini配置文件:
/usr/local/php/bin/php -i|grep php.ini # 查看读取php.ini的目录:
cp php.ini-development /usr/local/php/lib/php.ini # 复制php.ini-development到上面目录
常见问题:
configure: error: Don't know how to define struct flock on this system, set --enable-opcache=no
配置时加上--enable-opcache=no
configure: error: off_t undefined; check your library configuration
echo "/usr/local/lib
/usr/local/lib64
/usr/lib
/usr/lib64" >> /etc/ld.so.conf && ldconfig –v
如果再次出现这个问题:
echo '/usr/local/mysql/lib' >> /etc/ld.so.conf && ldconfig –v
configure: error: PDO_MYSQL configure failed, MySQL 4.1 needed. Please check config.log for more information.
把--with-pdo-mysql=/usr /local/mysql改成--with-pdo-mysql就应该没问题,如果还有问题先去掉--with-pdo-mysql=/usr,之后再添加pdo_mysql扩展。
配置PHP和Apache
PHP手册:http://php.net/manual/zh/install.unix.apache2.php
vi /usr/local/apache2/conf/httpd.conf # 加入以下代码
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
/usr/local/apache2/bin/apachectl -k graceful # 修改配置文件后都要重启服务
vi /usr/local/apache2/htdocs/test.php # 新增PHP文件
curl 127.0.0.1/test.php # curl测试,下图表示测试成功:
测试PDO连接mysql
/etc/init.d/mysql.server start # 启动mysql
vi /usr/local/apache2/htdocs/pdo_mysql_test.php
<?php
$db_host = '127.0.0.1';
$db_name = 'mysql';
$db_user = 'root';
$db_pass = 'root';
$db_port = 3306;
$db_charset = 'utf8';
try{
$dbh = new PDO("mysql:host=$db_host;dbname=$db_name;port=$db_port;charset=$db_charset", $db_user, $db_pass);
}catch(PDOException $e){
exit('error: '. $e->getMessage());
}
$sql = 'show databases';
$query = $dbh->query($sql);
$query->setFetchMode(PDO::FETCH_ASSOC);
$result = $query->fetchAll();
print_r($result); // 打印所有数据库名称
$dbh = null; // 关闭连接
curl 127.0.0.1/pdo_mysql_test.php # 保存退出,运行此命令,如果出现数据库信息就说明没问题
或者
vi /usr/local/apache2/htdocs/phpinfo.php # 输入以下内容,保存退出
systemctl stop firewalld # 关闭防火墙
用局域网的机器访问:
ip/phpinfo.php
搜索PDO即可看到pdo_mysql是否安装成功。
phpMyAdmin
cd /usr/local/apache2/htdocs/ # 进入网站根目录
wget -c https://files.phpmyadmin.net/phpMyAdmin/4.7.4/phpMyAdmin-4.7.4-all-languages.tar.gz # 下载phpMyAdmin到当前目录
tar -zxf phpMyAdmin-4.7.4-all-languages.tar.gz # 解压
mv phpMyAdmin-4.7.4-all-languages phpmyadmin # 重命名
systemctl stop firewalld # 关闭防火墙
用局域网内的机器访问:
ip/phpmyadmin
The end.THX.