目录
1. 概述
LAMP=Linux+Apache+Mysql+PHP
LAMP的工作原理:当客户端访问php页面的时候,apache会将php转给libphp5.so模块处理,libphp5.so模块会把php页面解析成html文件再交给apache处理,然后apache将这些html文件返回给客户端。
Apache的libphp5.so模块是随着apache服务器一起运行的,所以修改配置文件时只需要重启apache服务就可以。
2. 环境准备
2.1 机器准备
#一台虚拟机
hostname:myhost
ip:ip_address
2.2 系统环境
[root@myhost ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[root@myhost ~]# firewall-cmd --state #查看防火墙的状态
running
[root@myhost ~]# firewall-cmd --list-ports #查看防火墙已经开放的端口
[root@myhost ~]# firewall-cmd --list-services #查看防火墙开放的服务
dhcpv6-client ssh
#httpd服务默认的端口是80,防火墙允许80/tcp端口访问
[root@myhost ~]# firewall-cmd --zone=public --permanent --add-port=80/tcp
success
#mysqld服务默认的端口是3306,防火墙允许3306/tcp端口访问
[root@myhost ~]# firewall-cmd --zone=public --permanent --add-port=3306/tcp
success
[root@myhost ~]# firewall-cmd --reload #重新加载防火墙配置
success
[root@myhost ~]# setenforce 0 && getenforce
Permissive
[root@myhost ~]# sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/sysconfig/selinux
3. Apache安装部署
3.1 下载httpd源码安装包
[root@myhost ~]# cd /home/tools/
[root@myhost tools]# ll
总用量 22256
-rw-r--r--. 1 root root 639864682 1月 8 09:46 mysql-5.7.13-linux-glibc2.5-x86_64.tar.gz
[root@myhost tools]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.43.tar.gz
3.2 解压安装包
[root@myhost tools]# ll
总用量 41388
-rw-r--r--. 1 root root 9348230 3月 30 22:21 httpd-2.4.43.tar.gz
-rw-r--r--. 1 root root 639864682 1月 8 09:46 mysql-5.7.13-linux-glibc2.5-x86_64.tar.gz
[root@myhost tools]# tar xf httpd-2.4.43.tar.gz
[root@myhost tools]# cd httpd-2.4.43
[root@myhost httpd-2.4.43]# ll
总用量 2200
-rw-r--r--. 1 root dip 13497 2月 21 08:33 ABOUT_APACHE
-rw-r--r--. 1 root dip 25703 2月 21 08:06 acinclude.m4
-rw-r--r--. 1 root dip 66792 3月 26 2019 Apache-apr2.dsw
-rw-r--r--. 1 root dip 81919 3月 26 2019 Apache.dsw
-rw-r--r--. 1 root dip 9907 12月 18 2009 apache_probes.d
-rw-r--r--. 1 root dip 2512 12月 22 2008 ap.d
drwxr-sr-x. 6 root dip 4096 3月 26 22:46 build
-rw-r--r--. 1 root dip 2644 8月 24 2007 BuildAll.dsp
-rw-r--r--. 1 root dip 2784 1月 14 2018 BuildBin.dsp
-rwxr-xr-x. 1 root dip 10679 9月 8 2017 buildconf
-rw-r--r--. 1 root dip 287086 3月 24 01:54 CHANGES
-rw-r--r--. 1 root dip 47641 10月 25 21:27 CMakeLists.txt
-rw-r--r--. 1 root dip 14180 2月 21 08:39 config.layout
-rwxr-xr-x. 1 root dip 1209103 3月 26 22:46 configure ###编译安装脚本###
-rw-r--r--. 1 root dip 30571 2月 3 15:56 configure.in
drwxr-sr-x. 10 root dip 4096 3月 26 22:46 docs
-rw-r--r--. 1 root dip 403 11月 22 2004 emacs-style
-rw-r--r--. 1 root dip 2218 3月 23 2016 httpd.dep
-rw-r--r--. 1 root dip 4124 6月 12 2008 httpd.dsp
-rw-r--r--. 1 root dip 8994 3月 23 2016 httpd.mak
-rw-r--r--. 1 root dip 17313 3月 26 22:46 httpd.spec
drwxr-sr-x. 2 root dip 4096 3月 26 22:46 include
-rw-r--r--. 1 root dip 3782 5月 18 2016 INSTALL
-rw-r--r--. 1 root dip 2909 11月 15 2011 InstallBin.dsp
-rw-r--r--. 1 root dip 4097 11月 16 2013 LAYOUT
-rw-r--r--. 1 root dip 79070 3月 23 2016 libhttpd.dep
-rw-r--r--. 1 root dip 21528 1月 16 2018 libhttpd.dsp
-rw-r--r--. 1 root dip 38913 1月 16 2018 libhttpd.mak
-rw-r--r--. 1 root dip 25479 1月 15 01:39 LICENSE
-rw-r--r--. 1 root dip 11166 8月 8 2018 Makefile.in
-rw-r--r--. 1 root dip 53749 3月 26 2019 Makefile.win
drwxr-sr-x. 28 root dip 4096 3月 26 22:43 modules
-rw-r--r--. 1 root dip 720 1月 2 03:42 NOTICE
-rw-r--r--. 1 root dip 13884 3月 20 2016 NWGNUmakefile
drwxr-sr-x. 7 root dip 4096 3月 26 22:46 os
-rw-r--r--. 1 root dip 4642 1月 24 2014 README
-rw-r--r--. 1 root dip 13317 3月 23 2016 README.cmake
-rw-r--r--. 1 root dip 4418 8月 28 2014 README.platforms
-rw-r--r--. 1 root dip 10184 2月 21 08:33 ROADMAP
drwxr-sr-x. 3 root dip 4096 3月 26 22:46 server
drwxr-sr-x. 2 root dip 24 3月 26 22:46 srclib
drwxr-sr-x. 4 root dip 4096 3月 26 22:46 support
drwxr-sr-x. 2 root dip 4096 3月 26 22:46 test
-rw-r--r--. 1 root dip 8186 2月 21 08:33 VERSIONING
3.3 进行编译安装
[root@myhost httpd-2.4.43]#
./configure --prefix=/usr/local/apache-2.4.43/ \
--enable-deflate \
--enable-expires \
--enable-headers \
--enable-modules=most \
--enable-so \
--with-mpm=worker \
--enable-rewrite
-------------------------------------------------------------------------------------
checking for chosen layout... Apache
checking for working mkdir -p... yes
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
configure:
configure: Configuring Apache Portable Runtime library...
configure:
checking for APR... no
configure: error: APR not found. Please read the documentation.
上面编译过程中报错,根据报错信息进行处理,这里缺少APR依赖包,yum安装APR依赖包并再次对apache进行编译。
[root@myhost httpd-2.4.43]# yum install -y apr*
#再次进行编译
[root@myhost httpd-2.4.43]#
./configure --prefix=/usr/local/apache-2.4.43/ \
--enable-deflate \
--enable-expires \
--enable-headers \
--enable-modules=most \
--enable-so \
--with-mpm=worker \
--enable-rewrite
[root@myhost httpd-2.4.43]# echo $?
0
[root@myhost httpd-2.4.43]# make && make install && echo $?
[root@myhost httpd-2.4.43]# cd /usr/local/apache-2.4.43/
[root@myhost apache-2.4.43]# ll
总用量 44
drwxr-xr-x. 2 root root 4096 4月 14 15:22 bin
drwxr-xr-x. 2 root root 4096 4月 14 15:22 build
drwxr-xr-x. 2 root root 74 4月 14 15:22 cgi-bin
drwxr-xr-x. 4 root root 79 4月 14 15:22 conf
drwxr-xr-x. 3 root root 4096 4月 14 15:22 error
drwxr-sr-x. 2 root root 23 3月 26 22:46 htdocs
drwxr-xr-x. 3 root root 8192 4月 14 15:22 icons
drwxr-xr-x. 2 root root 4096 4月 14 15:22 include
drwxr-xr-x. 2 root root 6 4月 14 15:22 logs
drwxr-xr-x. 4 root root 28 4月 14 15:22 man
drwxr-sr-x. 14 root root 8192 3月 26 22:46 manual
drwxr-xr-x. 2 root root 4096 4月 14 15:22 modules
3.4 检查+启动httpd服务
[root@myhost apache-2.4.43]# /usr/local/apache-2.4.43/bin/apachectl -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using xx.xx.xx.xx. Set the 'ServerName' directive globally to suppress this message
Syntax OK
[root@myhost apache-2.4.43]# grep ServerName /usr/local/apache-2.4.43/conf/httpd.conf
# ServerName gives the name and port that the server uses to identify itself.
#ServerName www.example.com:80
[root@myhost apache-2.4.43]# vi /usr/local/apache-2.4.43/conf/httpd.conf +198
[root@myhost apache-2.4.43]# grep ServerName /usr/local/apache-2.4.43/conf/httpd.conf
# ServerName gives the name and port that the server uses to identify itself.
#ServerName www.example.com:80
ServerName 127.0.0.1:80
[root@myhost apache-2.4.43]# /usr/local/apache-2.4.43/bin/apachectl -t
Syntax OK
[root@myhost apache-2.4.43]# /usr/local/apache-2.4.43/bin/apachectl start
[root@myhost apache-2.4.43]# /usr/local/apache-2.4.43/bin/apachectl stop
[root@myhost apache-2.4.43]# /usr/local/apache-2.4.43/bin/apachectl start
[root@myhost apache-2.4.43]# /usr/local/apache-2.4.43/bin/apachectl status
/usr/local/apache-2.4.43/bin/apachectl:行95: lynx: 未找到命令
[root@myhost apache-2.4.43]# ps -ef |grep httpd
root 16708 1 0 15:31 ? 00:00:00 /usr/local/apache-2.4.43//bin/httpd
daemon 16709 16708 0 15:31 ? 00:00:00 /usr/local/apache-2.4.43//bin/httpd
daemon 16710 16708 0 15:31 ? 00:00:00 /usr/local/apache-2.4.43//bin/httpd
daemon 16714 16708 0 15:31 ? 00:00:00 /usr/local/apache-2.4.43//bin/httpd
root 16805 63217 0 15:32 pts/0 00:00:00 grep --color=auto httpd
[root@myhost apache-2.4.43]# netstat -lntup |grep httpd
tcp6 0 0 :::80 :::* LISTEN 16708/httpd
4. MySQL安装部署
4.1 软件安装约定
安装包存放目录:/home/tools
Mysql安装目录:/usr/local/mysql
数据库保存位置:/usr/local/mysql/data #需要自行建立,并修改属主和属组为mysql:mysql
日志保存位置:/usr/local/mysql/log #需要自行建立,并修改属主和属组为mysql:mysql
4.2 下载源码安装包
- MySQL的官网下载地址:http://www.mysql.com/downloads
- MySQL的官网下载地址:https://dev.mysql.com/downloads/mysql
- MySQL版本:MySQL-5.7.13
[root@myhost ~]#cd /home/tools
[root@myhost tools]# wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.13-linux-glibc2.5-x86_64.tar.gz
[root@myhost tools]# ll
total 835560
-rw-r--r--. 1 root root 639864682 May 25 2016 mysql-5.7.13-linux-glibc2.5-x86_64.tar.gz
4.3 解压安装部署
解压安装部署过程省略,参考之前写两篇,注意安装路径防火墙开放端口等灵活处理。
5. PHP安装部署
5.1 yum安装PHP依赖包
# 先更新yum源,不然有的依赖包找不到。
[root@myhost php-5.4.25]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@myhost tools]# yum install -y libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel zlib-devel gd-devel curl-devel mhash mhash-devel mcrypt
# libiconv 依赖yum安装不了的话,就编译安装吧
[root@myhost ~]# cd /home/tools/
[root@myhost tools]# wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
[root@myhost tools]# ll
总用量 680572
drwxr-sr-x. 12 root dip 4096 4月 14 15:22 httpd-2.4.43
-rw-r--r--. 1 root root 9348230 3月 30 22:21 httpd-2.4.43.tar.gz
-rw-r--r--. 1 root root 4984397 4月 14 16:34 libiconv-1.14.tar.gz
-rw-r--r--. 1 root root 639864682 1月 8 09:46 mysql-5.7.13-linux-glibc2.5-x86_64.tar.gz
[root@myhost tools]# tar xf libiconv-1.14.tar.gz
[root@myhost tools]# ll
总用量 680576
drwxr-sr-x. 12 root dip 4096 4月 14 15:22 httpd-2.4.43
-rw-r--r--. 1 root root 9348230 3月 30 22:21 httpd-2.4.43.tar.gz
drwxr-xr-x. 22 root root 4096 8月 8 2011 libiconv-1.14
-rw-r--r--. 1 root root 4984397 4月 14 16:34 libiconv-1.14.tar.gz
-rw-r--r--. 1 root root 639864682 1月 8 09:46 mysql-5.7.13-linux-glibc2.5-x86_64.tar.gz
[root@myhost tools]# cd ./libiconv-1.14
[root@myhost libiconv-1.14]# ./configure --prefix=/usr/local/libiconv
[root@myhost libiconv-1.14]# make && make install && echo $?
......
In file included from progname.c:26:0:
./stdio.h:1010:1: 错误:‘gets’未声明(不在函数内)
_GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead");
^
make[2]: *** [progname.o] 错误 1
make[2]: 离开目录“/home/tools/libiconv-1.14/srclib”
make[1]: *** [all] 错误 2
make[1]: 离开目录“/home/tools/libiconv-1.14/srclib”
make: *** [all] 错误 2
# 编译过程中报错,根据报错信息进行处理,./stdio.h文件1010行有错,对其进行注释。
[root@myhost libiconv-1.14]# vi /home/tools/libiconv-1.14/srclib/stdio.h +1010
//_GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead");
[root@myhost libiconv-1.14]# make && make install && echo $?
[root@myhost libiconv-1.14]# ll /usr/local/libiconv/
总用量 4
drwxr-xr-x. 2 root root 18 4月 14 16:39 bin
drwxr-xr-x. 2 root root 60 4月 14 16:39 include
drwxr-xr-x. 2 root root 4096 4月 14 16:39 lib
drwxr-xr-x. 5 root root 39 4月 14 16:39 share
# 到此,php安装环境已经准备好!接下来便可以安装部署PHP啦!
5.1 下载PHP源码安装包
[root@myhost ~]# cd /home/tools/
[root@myhost tools]# wget https://www.php.net/distributions/php-5.4.25.tar.gz
[root@myhost tools]# ll
总用量 680576
drwxr-sr-x. 12 root dip 4096 4月 14 15:22 httpd-2.4.43
-rw-r--r--. 1 root root 9348230 3月 30 22:21 httpd-2.4.43.tar.gz
drwxr-xr-x. 22 root root 4096 4月 14 16:36 libiconv-1.14
-rw-r--r--. 1 root root 4984397 4月 14 16:34 libiconv-1.14.tar.gz
-rw-r--r--. 1 root root 639864682 1月 8 09:46 mysql-5.7.13-linux-glibc2.5-x86_64.tar.gz
-rw-r--r--. 1 root root 14648900 1月 21 2015 php-5.4.25.tar.gz ### php源码安装包 ###
5.2 解压安装包
[root@myhost tools]# tar xf php-5.4.25.tar.gz
[root@myhost tools]# ll
总用量 680576
drwxr-sr-x. 12 root dip 4096 4月 14 15:22 httpd-2.4.43
-rw-r--r--. 1 root root 9348230 3月 30 22:21 httpd-2.4.43.tar.gz
drwxr-xr-x. 22 root root 4096 4月 14 16:36 libiconv-1.14
-rw-r--r--. 1 root root 4984397 4月 14 16:34 libiconv-1.14.tar.gz
-rw-r--r--. 1 root root 639864682 1月 8 09:46 mysql-5.7.13-linux-glibc2.5-x86_64.tar.gz
drwxr-xr-x. 13 501 games 4096 2月 5 2014 php-5.4.25
-rw-r--r--. 1 root root 14648900 1月 21 2015 php-5.4.25.tar.gz
5.3 LAMP 编译安装 (apache)
- php安装路径:/usr/local/php-5.4.25
- httpd安装路径:/usr/local/apache-2.4.43
- libiconv安装路径:/usr/local/libiconv
[root@myhost tools]# cd php-5.4.25
[root@myhost php-5.4.25]#
./configure --prefix=/usr/local/php-5.4.25 \
--with-apxs2=/usr/local/apache-2.4.43/bin/apxs \
--with-mysql=mysqlnd \
--with-iconv-dir=/usr/local/libiconv \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--enable-xml \
--disable-rpath \
--enable-safe-mode \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--with-curlwrappers \
--enable-mbregex \
--enable-mbstring \
--with-mcrypt \
--with-gd \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--enable-short-tags \
--enable-zend-multibyte \
--enable-static \
--with-xsl \
--enable-ftp
[root@myhost php-5.4.25]# make && make install && echo $?
5.4 拷贝php配置文件
[root@myhost php-5.4.25]# cd /home/tools/php-5.4.25
[root@myhost php-5.4.25]# pwd
/home/tools/php-5.4.25
[root@myhost php-5.4.25]# ll php.ini*
-rw-r--r--. 1 501 games 66920 2月 5 2014 php.ini-development ### 开发人员使用的测试环境配置文件
-rw-r--r--. 1 501 games 66952 2月 5 2014 php.ini-production ### 正式的生产环境配置文件
说明:通过对比两个配置文件我们知道开发环境更多的是开启日志、调试信息等。
[root@myhost php-5.4.25]# cp /home/tools/php-5.4.25/php.ini-production /usr/local/php-5.4.25/lib/php.ini
说明:将源码包中的正式的生产环境的配置文件 拷贝到我们php安装目录下并重命名为php.ini
[root@myhost apache-2.4.43]# cd /usr/local/apache-2.4.43
[root@myhost apache-2.4.43]# ll
总用量 44
drwxr-xr-x. 2 root root 4096 4月 14 15:22 bin
drwxr-xr-x. 2 root root 4096 4月 14 15:22 build
drwxr-xr-x. 2 root root 74 4月 14 15:22 cgi-bin
drwxr-xr-x. 4 root root 100 4月 14 16:52 conf
drwxr-xr-x. 3 root root 4096 4月 14 15:22 error
drwxr-sr-x. 2 root root 23 3月 26 22:46 htdocs
drwxr-xr-x. 3 root root 8192 4月 14 15:22 icons
drwxr-xr-x. 2 root root 4096 4月 14 15:22 include
drwxr-xr-x. 2 root root 55 4月 14 15:31 logs
drwxr-xr-x. 4 root root 28 4月 14 15:22 man
drwxr-sr-x. 14 root root 8192 3月 26 22:46 manual
drwxr-xr-x. 2 root root 4096 4月 14 16:52 modules ## 模块目录 ##
安装php时默认会在apache目录下产生文件和配置:
[root@myhost apache-2.4.43]# cd /usr/local/apache-2.4.43/modules/
root@myhost modules]# ll httpd.exp
-rw-r--r-- 1 root root 15822 Apr 13 14:23 httpd.exp
[root@myhost modules]# ll libphp5.so
-rwxr-xr-x 1 root root 36551784 Apr 13 14:47 libphp5.so
[root@myhost apache-2.4.43]# cd /usr/local/apache-2.4.43/conf/
[root@myhost conf]# grep libphp5.so /usr/local/apache-2.4.43/conf/httpd.conf
LoadModule php5_module modules/libphp5.so
5.5 配置apache支持php
注意:修改配置文件前养成备份的好习惯。
[root@myhost conf]# cp httpd.conf httpd.conf_bak20200414
[root@myhost conf]# ll
总用量 140
drwxr-xr-x. 2 root root 4096 4月 14 15:22 extra
-rw-r--r--. 1 root root 18717 4月 14 16:52 httpd.conf
-rw-r--r--. 1 root root 18668 4月 14 16:52 httpd.conf.bak
-rw-r--r--. 1 root root 18717 4月 14 16:59 httpd.conf_bak20200414
-rw-r--r--. 1 root root 13064 4月 14 15:22 magic
-rw-r--r--. 1 root root 60847 4月 14 15:22 mime.types
drwxr-xr-x. 3 root root 35 4月 14 15:22 original
在 httpd.conf 配置文件199行下面,添加:ServerName 127.0.0.1:80
[root@myhost conf]# vi httpd.conf +199
#ServerName www.example.com:80
ServerName 127.0.0.1:80
创建apche服务所需要的www用户和组,在httpd.conf 配置文件168行左右修改:
说明:这个用户修改并不是必须的,只是从更安全的角度出发。
[root@myhost conf]# useradd www -s /sbin/nologin -M
[root@myhost conf]# id www
uid=1005(www) gid=1005(www) 组=1005(www)
[root@myhost conf]# vi httpd.conf +168
#User daemon
#Group daemon
User www
Group www
在 httpd.conf 配置文件258行左右,修改默认的首页文件,将默认的首页文件修改为index.php:
[root@myhost conf]# vi httpd.conf +258
<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>
在 httpd.conf 配置文件396行左右,添加两行对php 的解析支持内容:
说明: 如果不添加以下几行内容,php的文件被apache无法解析,打开页面就是直接下载文件
[root@myhost conf]# vi httpd.conf +396
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType application/x-httpd-php .php .phtml ######
AddType application/x-httpd-php-source .phps ######
5.6 修改apache安装目录权限并重启
[root@myhost conf]# chown -R www.www /usr/local/apache-2.4.43/
[root@myhost conf]# /usr/local/apache-2.4.43/bin/apachectl restart
[root@myhost conf]# /usr/local/apache-2.4.43/bin/apachectl graceful
[root@myhost conf]# ps -ef |grep httpd
root 16708 1 0 15:31 ? 00:00:00 /usr/local/apache-2.4.43//bin/httpd
www 32188 16708 0 17:09 ? 00:00:00 /usr/local/apache-2.4.43//bin/httpd
www 32189 16708 0 17:09 ? 00:00:00 /usr/local/apache-2.4.43//bin/httpd
www 32190 16708 0 17:09 ? 00:00:00 /usr/local/apache-2.4.43//bin/httpd
root 32274 63217 0 17:09 pts/0 00:00:00 grep --color=auto httpd
[root@myhost conf]# netstat -lntup |grep httpd
tcp6 0 0 :::80 :::* LISTEN 16708/httpd
5.7 编写一个index.php文件测试能否连上apache服务
[root@myhost htdocs]# cd /usr/local/apache-2.4.43/htdocs/
[root@myhost htdocs]# ll
总用量 4
-rw-r--r--. 1 www www 45 6月 12 2007 index.html
[root@myhost htdocs]# vi index.php
[root@myhost htdocs]# cat index.php
<?php
phpinfo();
?>
[root@myhost conf]# chown -R www.www /usr/local/apache-2.4.43/
[root@myhost conf]# /usr/local/apache-2.4.43/bin/apachectl restart
[root@myhost conf]# /usr/local/apache-2.4.43/bin/apachectl graceful
说明:此时,访问 http://ip_address:80/ 时,默认还是访问 /usr/local/apache-2.4.43/htdocs下面的index.php的内容,因为配置文件里面配置此目录下的index.php比index.html优先访问。
5.8 编写一个mysql.php文件测试能否连上mysql服务
[root@myhost htdocs]# cat mysql.php
<?php
$link_id=mysql_connect('localhost:3306', 'root', 'root123456') or mysql_error();
if($link_id){
echo 'mysql successful by yuki!';
}else{
echo mysql_error();
}
?>
[root@myhost conf]# chown -R www.www /usr/local/apache-2.4.43/
[root@myhost conf]# /usr/local/apache-2.4.43/bin/apachectl restart
[root@myhost conf]# /usr/local/apache-2.4.43/bin/apachectl graceful
5.9 解决Linux下php连接MySQL no such file or directory方法
很多人在编译完MySQL跟php环境之后,用mysql_connect(host:port,user,pwd)连接数据了,发现会报连接失败:no such file or directory的错误,其实,是跟mysql.sock有关,此类问题一般是mysql.sock文件路径造成的。
ps -ef | grep mysqld 查看mysql.sock的位置
[root@myhost www]# ps -ef|grep mysqld
root 16381 1 0 Apr13 ? 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/hb-web.pid
mysql 16965 16381 0 Apr13 ? 00:03:04 /usr/local/mysql//bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql//lib/plugin --user=mysql --log-error=/usr/local/mysql/log/mysqld_3306.err --open-files-limit=10240 --pid-file=/usr/local/mysql/data/hb-web.pid --socket=/usr/local/mysql/mysql.sock --port=3306
root 7883 7035 0 11:04 pts/1 00:00:00 grep --color=auto mysqld
再用phpinfo脚本查看下php读取的mysql.sock的位置,发现两者并不一样,所以导致了奇怪的错误信息。解决办法很简单,做个软连接 ln -s /usr/local/mysql/mysql.sock /tmp/mysql.sock
[root@myhost htdocs]# ln -s /usr/local/mysql/mysql.sock /tmp/mysql.sock
最后刷新下页面发现此事已经正常了,此时访问 http://ip_address:80/mysql.php 时, 如果mysql的连接端口、连接账户以及连接密码都正确页面会显示打印输出"mysql successful by yuki!"
6. 安装部署php扩展之php_pdo_mysql
在lamp环境搭建好环境后,访问项目时,显示 could not find driver,这是因为php配置文件默认开启连接的只有sqlite数据库驱动,要想项目连接上mysql数据库,windows上比较简单,打开php.ini,取消注释:extension=php_pdo_mysql.dll;然而,linux上面除了取消注释以外还要自己手动安装php扩展之php_pdo_mysql。
[[root@myhost mysql]# cd /usr/local/php-5.4.25/lib/
[[root@myhost lib]## ll
总用量 72
drwxr-xr-x. 14 root root 4096 12月 25 14:48 php
-rw-r--r--. 1 root root 66952 12月 25 15:01 php.ini
[[root@myhost lib]# vi php.ini
6.1 进入php源码安装包下的 ./ext/pdo_mysql
注意:linux下安装的pdo_mysql扩展一定要跟你的PHP版本一致,(建议从源码包安装)不能随便乱安装,否则你会发现就算成功安装这个扩展生成了so,但是phpinfo里面依然不会成功显示。
[root@myhost lib]# cd /usr/local/mysql
[root@myhost mysql]# cd /home/tools/php-5.4.25/ext/pdo_mysql
[root@myhost mysql]# pwd
/home/tools/php-5.4.25/ext/pdo_mysql
[root@myhost pdo_mysql]# ll
总用量 136
-rwxr-xr-x. 1 501 games 5449 2月 5 2014 config.m4
-rw-r--r--. 1 501 games 751 2月 5 2014 config.w32
-rwxr-xr-x. 1 501 games 93 2月 5 2014 CREDITS
-rw-r--r--. 1 501 games 643 2月 5 2014 get_error_codes.php
-rw-r--r--. 1 501 games 21938 2月 5 2014 mysql_driver.c
-rw-r--r--. 1 501 games 25092 2月 5 2014 mysql_statement.c
-rw-r--r--. 1 501 games 3159 2月 5 2014 package2.xml
-rw-r--r--. 1 501 games 7471 2月 5 2014 pdo_mysql.c
-rw-r--r--. 1 501 games 1606 2月 5 2014 php_pdo_mysql.h
-rw-r--r--. 1 501 games 6059 2月 5 2014 php_pdo_mysql_int.h
-rw-r--r--. 1 501 games 25445 2月 5 2014 php_pdo_mysql_sqlstate.h
drwxr-xr-x. 2 501 games 8192 2月 5 2014 tests
6.2 生成php扩展编译脚本
这里找不到phpize命令是因为没有配置php环境变量,我们可以直接用phpize命令全路径
[root@myhost mysql]# which phpize
/usr/bin/which: no phpize in (/usr/local/jdk1.8.0_131/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/mysql/bin:/root/bin)
[root@myhost mysql]# ll /usr/local/php-5.4.25/bin/
总用量 69788
-rwxr-xr-x. 1 root root 872 4月 14 16:53 pear
-rwxr-xr-x. 1 root root 893 4月 14 16:53 peardev
-rwxr-xr-x. 1 root root 806 4月 14 16:53 pecl
lrwxrwxrwx. 1 root root 35 4月 14 16:53 phar -> /usr/local/php-5.4.25/bin/phar.phar
-rwxr-xr-x. 1 root root 14834 4月 14 16:53 phar.phar
-rwxr-xr-x. 1 root root 35747388 4月 14 16:52 php
-rwxr-xr-x. 1 root root 35670315 4月 14 16:52 php-cgi
-rwxr-xr-x. 1 root root 3162 4月 14 16:52 php-config
-rwxr-xr-x. 1 root root 4559 4月 14 16:52 phpize #####
[root@myhost mysql]# pwd
/home/tools/php-5.4.25/ext/pdo_mysql
[root@myhost pdo_mysql]# /usr/local/php-5.4.25/bin/phpize
Configuring for:
PHP Api Version: 20100412
Zend Module Api No: 20100525
Zend Extension Api No: 220100525
[root@myhost pdo_mysql]# ll
总用量 1348
-rw-r--r--. 1 root root 79859 4月 14 19:15 acinclude.m4
-rw-r--r--. 1 root root 310183 4月 14 19:15 aclocal.m4
drwxr-xr-x. 2 root root 51 4月 14 19:15 autom4te.cache
drwxr-xr-x. 2 root root 79 4月 14 19:15 build
-rwxr-xr-x. 1 root root 44893 4月 14 19:15 config.guess
-rw-r--r--. 1 root root 1751 4月 14 19:15 config.h.in
-rwxr-xr-x. 1 501 games 5449 2月 5 2014 config.m4
-rwxr-xr-x. 1 root root 33399 4月 14 19:15 config.sub
-rwxr-xr-x. 1 root root 462258 4月 14 19:15 configure ##########
-rw-r--r--. 1 root root 4690 4月 14 19:15 configure.in
-rw-r--r--. 1 501 games 751 2月 5 2014 config.w32
-rwxr-xr-x. 1 501 games 93 2月 5 2014 CREDITS
-rw-r--r--. 1 501 games 643 2月 5 2014 get_error_codes.php
-rw-r--r--. 1 root root 0 4月 14 19:15 install-sh
-rw-r--r--. 1 root root 199728 4月 14 19:15 ltmain.sh
-rw-r--r--. 1 root root 5574 4月 14 19:15 Makefile.global
-rw-r--r--. 1 root root 0 4月 14 19:15 missing
-rw-r--r--. 1 root root 0 4月 14 19:15 mkinstalldirs
-rw-r--r--. 1 501 games 21938 2月 5 2014 mysql_driver.c
-rw-r--r--. 1 501 games 25092 2月 5 2014 mysql_statement.c
-rw-r--r--. 1 501 games 3159 2月 5 2014 package2.xml
-rw-r--r--. 1 501 games 7471 2月 5 2014 pdo_mysql.c
-rw-r--r--. 1 501 games 1606 2月 5 2014 php_pdo_mysql.h
-rw-r--r--. 1 501 games 6059 2月 5 2014 php_pdo_mysql_int.h
-rw-r--r--. 1 501 games 25445 2月 5 2014 php_pdo_mysql_sqlstate.h
-rw-r--r--. 1 root root 79262 4月 14 19:15 run-tests.php
drwxr-xr-x. 2 501 games 8192 2月 5 2014 tests
编译安装,php扩展 /usr/local/php/bin/phpize 发现没有 configure,需要先安装automake
yum install automake
6.3 编译安装php扩展之php_pdo_mysql
[root@myhost pdo_mysql]#
./configure \
--with-php-config=/usr/local/php-5.4.25/bin/php-config \
--with-pdo-mysql=/usr/local/mysql
[root@myhost pdo_mysql]# make && make install
----------------------------------------------------------------------
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
Build complete.
Don't forget to run 'make test'.
Installing shared extensions: /usr/local/php-5.4.25/lib/php/extensions/no-debug-zts-20100525/
6.4 修改php配置文件php.ini 并重启apache服务
[root@myhost mysql]# cd /usr/local/php-5.4.25/lib/
[root@myhost lib]# vi php.ini +868
添加以下几行:
extension=/usr/local/php-5.4.25/lib/php/extensions/no-debug-zts-20100525/pdo_mysql.so
extension=pdo_mysql.so
extension=php_mysql.dll
extension=php_mysqli.dll
extension=php_pdo_firebird.dll
extension=php_pdo_mysql.dll
[root@myhost lib]# chown -R www.www /usr/local/apache-2.4.43/
[root@myhost lib]# /usr/local/apache-2.4.43/bin/apachectl graceful