LAMPLinux+ Apache+ MySQL+PHP的简称。使用源码安装,由于php需要依赖apachemysql,所以php是最后安装,而mysqlapache谁先安装都可以。

 

安装平台:CentOS6.7 X86_64

MySql版本:mysql-5.6.27
MySql安装目录:/usr/local/mysql
MySql数据库存放目录:/data/mysql
MySql的用户和组:mysql:mysql
MySql源码存放目录:/usr/local/src

 

安装前的准备:

1)、关闭selinux

         vim/etc/selinux/config

SELINUX=enforcing     ##加#注释
SELINUX=disabled      ##增加


保存退出,然后重启系统,如果不重启,可临时关闭使用命令:

setenforce 0

 

2)、添加好防火墙规则到/etc/sysconfig/iptables

# vim /etc/sysconfig/iptables

-A INPUT -m state --state NEW -m tcp -p tcp--dport 3306 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp--dport 80 -j ACCEPT

 

保存退出,重启服务:

/etc/init.d/iptables restart



1、安装mysql

 

1)、下载mysql,这里使用的是免编译的安装包,下载地址:

ftp://ftp.jaist.ac.jp/pub/mysql/Downloads/MySQL-5.6/mysql-5.6.27-linux-glibc2.5-x86_64.tar.gz

这是mysql官网提供的一个镜像站点。

 

首先进入/usr/local/src目录,使用wget命令下载

# wget ftp://ftp.jaist.ac.jp/pub/mysql/Downloads/MySQL-5.6/mysql-5.6.27-linux-glibc2.5-x86_64.tar.gz

 

2)、解压下载的mysql安装包,然后把解压的文件移动到/usr/local/mysql(移动重命名)

# tar zxvfmysql-5.6.27-linux-glibc2.5-x86_64.tar.gz     
# mv mysql-5.6.27-linux-glibc2.5-x86_64 /usr/local/mysql


3)、创建用户mysql数据库的用户和用户组mysql,并且这个用户是无需登录系统。

# groupadd mysql                     ##创建mysql组
# useradd -s/sbin/nologin -g mysql mysql    ##创建mysql用户,不允许登录


4)、创建用于存放mysql数据库的目录,并且把所有者和所属组设置为mysql

# mkdir -p /data/mysql               ##创建存放mysql数据库的目录
# chown -R mysql:mysql/data/mysql/      ##修改属主和数组


5)、初始化数据库,在mysql的安装目录下/usr/local/mysql

# cd /usr/local/mysql/                  ##进入mysql安装目录
#./scripts/mysql_install_db --user=mysql --datadir=/data/mysql/   ##初始化数据库


 

--user=mysql              ##指定数据库的所属主

--datadir=/data/mysql/        ##指定数据库的数据库文件的存放目录

 

安装过程中的错误:

Installing MySQL systemtables..../bin/mysqld: error while loading shared libraries: libaio.so.1:cannot open shared object file: No such file or directory

 

提示缺少了shared libraries: libaio.so,那就安装不上,如下:

# yum install libaio

然后继续执行上面的安装命令。如果看到了两个ok ,则表明mysql已经安装成功了。


6)、拷贝配置文件my.cnf

# pwd
/usr/local/mysql
# cpsupport-files/my-default.cnf /etc/my.cnf  ##拷贝配置文件
cp:是否覆盖"/etc/my.cnf"? y



7)、拷贝启动脚本并修改其属主和数组。

# cp support-files/mysql.server /etc/init.d/mysqld     ##拷贝启动文件
# chmod 755/etc/init.d/mysqld                 ##修改权限,增加执行权限


 

8)、修改启动脚本/etc/init.d/mysqld,并把启动脚本加入到系统服务,设置开机启动。

# vim/etc/init.d/mysqld

# 找到:
basedir=
datadir=
# 修改为:
basedir=/usr/local/mysql                      ##mysql数据库的安装目录
datadir=/data/mysql                          ##mysql数据库的存放目录


# chkconfig --addmysqld         ##把mysqld添加到系统服务
# chkconfig mysqld on          ##设置为开机启动


 

9)、启动mysql数据库

# /etc/init.d/mysqldstart            
StartingMySQL............................................. SUCCESS!

 

为了验证,我们可以使用psnetstat查看启动的进程和开启的端口号。

# ps aux |grepmysql|grep -v grep
root     3541  0.0  0.1 106192 1524 pts/2    S    12:38  0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql--pid-file=/data/mysql/balichvm.org.pid
mysql    3656 19.1 44.7 1011064 452936 pts/2 Sl   12:38   0:45 /usr/local/mysql/bin/mysqld--basedir=/usr/local/mysql --datadir=/data/mysql--plugin-dir=/usr/local/mysql/lib/plugin --user=mysql--log-error=/data/mysql/balichvm.org.err--pid-file=/data/mysql/balichvm.org.pid
 
# netstat -lnp |grepmysql|grep -v grep
tcp       0      0 :::3306                     :::*                        LISTEN      3656/mysqld        
unix 2      [ ACC ]     STREAM    LISTENING     32648  3656/mysqld         /tmp/mysql.sock

 

10)、将mysql的安装目录加入到环境变量,这是为了方便使用(如果不加,要登录mysql需要写全路径)

 

# vim /etc/profile            #编辑环境变量配置文件

export PATH=$PATH:/usr/local/mysql/bin

          

# source /etc/profile        ##
刷新一次环境变量


 

11)、登录mysql、修改mysql密码

登录mysql的方法可以使通过ip/端口或者是socket的方式登录。

ip/端口的方式:mysql –Hhostname–Pport –uuser –ppasswd
socket的方式:mysql –S socketdir–uuser –ppasswd

 

修改mysql的密码:

# mysqladmin -urootpassword 'fgjh123.'    ##设置密码为fgjh123.

 

mysql的完成安装。

 

 

2、安装Apache

 

Apache的版本:httpd-2.2.31

Apache的安装目录:/usr/local/apache22

 

1)、下载Apache软件包,下载存放的目录:/usr/local/src  然后解压

# wget http://www.us.apache.org/dist//httpd/httpd-2.2.31.tar.gz 
# tar zxvfhttpd-2.2.31.tar.gz

2)、创建Apache的安装目录/usr/local/apache22,并且配置编译参数:

# mkdir/usr/local/apache22
# cd httpd-2.2.31
# ./configure--prefix=/usr/local/apache22 --with-included-apr --enable-so--enable-deflate=shared \
--enable-expires=shared --enable-rewrite=shared--with-pcre

配置完成后,使用echo $? 检查上一条命令是否有错误,没有会返回0.  

# echo $?           #这是查看上一条命令是否有错误的。

1

这里有错误提示:checking whether to enable mod_deflate... configure: error:mod_deflate has been requested but can not be built due to prerequisitefailures

那就重新安装

# yum install zlib-devel

然后再次配置。

 

3)、编译(make)和安装(makeinstall

# make &make install       ##
编译和安装


 

4)、Apache服务的启动、关闭、重启。

Apache的启动、关闭,重启。Apache的服务控制脚本存放在安装目录下:/usr/local/apache22/bin/apachectl

要启动Apache服务:/usr/local/apache22/bin/apachectl -k start
要停止Apache服务:/usr/local/apache22/bin/apachectl -k stop
要重启Apache服务:/usr/local/apache22/bin/apachectl -k restart

 

5)、查看Apache服务是否启动,首页是否正常打开

# netstat -lnp|grep -v grep|grep httpd
tcp       0      0 :::80                       :::*                        LISTEN      64605/httpd        

##使用curl命令测试打开
# curl -Ilocalhost       
HTTP/1.1 200 OK
Date: Thu, 29 Oct 2015 07:16:34 GMT
Server: Apache/2.2.31 (Unix)
Last-Modified: Sat, 20 Nov 2004 20:16:24GMT
ETag: "c481c-2c-3e9564c23b600"
Accept-Ranges: bytes
Content-Length: 44
Content-Type: text/html


 

Apache安装完成。

 

 

3、安装PHP

php版本:php-5.4.45

php安装目录:/usr/local/php

 

1)、下载和解压,如果是从php官网下载,速度很慢,可以从国内的镜像站点,如搜狐:mirrors.sohu.com

# wget http://mirrors.sohu.com/php/php-5.4.45.tar.bz2 
# tar jxvfphp-5.4.45.tar.bz2

  

 

2)、创建php安装目录:/usr/local/php;配置编译参数:

# mkdir /usr/local/php                   
# cd php-5.4.45
# ./configure \
--prefix=/usr/local/php \
--with-apxs2=/usr/local/apache22/bin/apxs \
--with-config-file-path=/usr/local/php/etc \
--with-mysql=/usr/local/mysql \
--with-libxml-dir \
--with-gd \
--with-jpeg-dir \
--with-freetype-dir \
--with-iconv-dir \
--with-zlib-dir \
--with-bz2 \
--with-openssl \
--enable-soap \
--enable-gd-native-ttf \
--enable-mbstring \
--enable-sockets \
--enable-exif \
--with-curl \
--enable-json \
--disable-ipv6

使用$?检查是否有错误,如果没有就执行编译和安装。如有错误,基本都是确实了相关的开发库支持,只需要安装提示的错误补上即可。

 

最后有:Thank you for using PHP. 的提示。

 

3)、编译(make)和安装(make install

# make &&make install


 

4)、拷贝php.ini配置文件到/usr/local/php/etc/目录下

# cp php.ini-production /usr/local/php/etc/php.ini

 

使用/usr/local/php/bin/php -i |head 的相关信息,其实这是phpinfo()函数查看的一样的。

#/usr/local/php/bin/php -i |head
phpinfo()
PHP Version => 5.4.45
 
System => Linux balichvm.org2.6.32-573.el6.x86_64 #1 SMP Thu Jul 23 15:44:03 UTC 2015 x86_64
Build Date => Oct 29 2015 17:42:38
Configure Command =>  './configure' '--prefix=/usr/local/php' '--with-apxs2=/usr/local/apache22/bin/apxs''--with-config-file-path=/usr/local/php/etc' '--with-mysql=/usr/local/mysql''--with-libxml-dir' '--with-gd' '--with-jpeg-dir' '--with-freetype-dir''--with-iconv-dir' '--with-zlib-dir' '--with-bz2' '--with-openssl''--enable-soap' '--enable-gd-native-ttf' '--enable-mbstring' '--enable-sockets''--enable-exif' '--disable-ipv6' '--with-curl' '--enable-json'
Server API => Command Line Interface
Virtual Directory Support => disabled
Configuration File (php.ini) Path =>/usr/local/php/etc
Loaded Configuration File => (none)

 

mysqlapachephp、都安装了,需要将他们结合起来共同工作。

 

4、配置ApachePHP组合

Mysqld 的主配置文件:/etc/my.cnf

Apache的主配置文件:/usr/local/apache22/conf/httpd.conf

Apache的默认网站根目录:/usr/local/apache22/htdocs/

php的主配置文件:/usr/local/php/etc/php.ini   

 

1)、修改Apache的主配置文件

# vim/usr/local/apache22/conf/httpd.conf

 

#找到:AddType application/x-gzip .gz .tgz
 
#在下一行增加内容:AddType application/x-httpd-php .php
 
#找到:

   DirectoryIndex index.html

 
#修改为:

   DirectoryIndex index.html index.htm index.php

 保存退出。

 

2)、编写用于测试php的程序,比如phpinfo(),这文件放在Apache的默认网站的根目录下。

# vim/usr/local/apache22/htdocs/info.php

 


~

保存,退出。

 

3)、检查Apache的配置文件是否有错,然后重启Apache服务。

#/usr/local/apache22/bin/apachectl –t    ##检查配置文件是否正确
httpd: Could not reliably determine theserver's fully qualified domain name, using balichvm.org for ServerName
Syntax OK                          ##配置文件没有问题。



# 这里有一个警告,如果不想它出现,可以修改Apache的主配置文件里面的ServerName
# 找到:
#ServerName www.example.com:80
 
# 这里有#号,默认是注释,那就在下面添加如下:
ServerName localhost:80

 

保存退出,在检查,就会正常了。

# /usr/local/apache22/bin/apachectl-t
Syntax OK
#/usr/local/apache22/bin/apachectl restart        ##重启Apache服务


 

在浏览器上输入http://serverip/info.php serverip是服务器的ip地址)可以看到php的配置信息。