linux运维之LAMP(apache+mariadb+php)搭建
         LAMP 是Linux Apache MySQL(mariadb) PHP的简写,其实就是把Apache, MySQL以及PHP安装在Linux系统上,组成一个环境来运行php的脚本语言,其中mariadb为mysql的一个分支。

搭建环境:

          系统: CentOS6.6-x86_64
          httpd: httpd-2.4.9
          mariadb: mariadb-5.5.43
          php: php-5.4.26

 注: 安装顺序为 httpd-->mairadb-->php,安装编译前先安装开发环境组件:
       "Develoment Tools"和 "Server Platform Development"

[root@localhost ~]# yum groupinstall "Development Tools" "Server Platform Development" -y


---------------------------------------------------------------------------------------------------------

1.apache(httpd)
 apache:Web服务器,主要提供网上信息浏览服务,使用应用层http协议


编译安装:
         依赖包:
                   httpd-2.4.12.tar.bz2
                   pcre-deve
                   apr-1.5.0.tar.bz2:apr(Apache Portable Runtime)
                   apr-util-1.5.3.tar.bz2

     注: CentOS6.6系统默认安装httpd2.2版本,如果以防和2.4冲突,可以自行移除2.2版本后再编译安装2.4,这里不做移除。
 
 移除命令为:

[root@localhost ~]# yum remove httpd


     首先安装pcre-deve包:

[root@localhost ~]# yum install pcre-devel -y

      编译安装apr包:

[root@localhost ~]# ls #下载源码包到服务器
anaconda-ks.cfg         httpd-2.4.10.tar.bz2                   Public
apr-1.5.0.tar.bz2       install.log                            Templates
apr-util-1.5.3.tar.bz2  install.log.syslog                     Videos
Desktop                 Music                                  wordpress-4.3.1-zh_CN.zip
Documents               phpMyAdmin-4.4.14.1-all-languages.zip
Downloads               Pictures
[root@localhost ~]# tar xf apr-1.5.0.tar.bz2 #解压
[root@localhost ~]# cd apr-1.5.0
[root@localhost apr-1.5.0]# ls #查看源码包内容
apr-config.in  buildconf         dso         libapr.rc     NOTICE         support
apr.dep        build.conf        emacs-mode  LICENSE       NWGNUmakefile  tables
apr.dsp        build-outputs.mk  encoding    locks         passwd         test
apr.dsw        CHANGES           file_io     Makefile.in   poll           threadproc
apr.mak        CMakeLists.txt    helpers     Makefile.win  random         time
apr.pc.in      config.layout     include     memory        README         tools
apr.spec       configure         libapr.dep  misc          README.cmake   user
atomic         configure.in      libapr.dsp  mmap          shmem
build          docs              libapr.mak  network_io    strings
[root@localhost apr-1.5.0]# ./configure --prefix=/usr/local/apr #编译并指定一个安装路径
[root@localhost apr-1.5.0]# make && make install #编译安装

      完成后编译安装apr-util

[root@localhost ~]# tar -xf apr-util-1.5.3.tar.bz2 #解压
[root@localhost ~]# cd apr-util-1.5.3
[root@localhost apr-util-1.5.3]# ls #查看源码包内容
aprutil.dep     buildconf         dbd                libaprutil.dsp  NWGNUmakefile
aprutil.dsp     build.conf        dbm                libaprutil.mak  README
aprutil.dsw     build-outputs.mk  docs               libaprutil.rc   README.cmake
aprutil.mak     CHANGES           encoding           LICENSE         renames_pending
apr-util.pc.in  CMakeLists.txt    export_vars.sh.in  Makefile.in     strmatch
apr-util.spec   config.layout     hooks              Makefile.win    test
apu-config.in   configure         include            memcache        uri
buckets         configure.in      ldap               misc            xlate
build           crypto            libaprutil.dep     NOTICE          xml
[root@localhost apr-util-1.5.3]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr #编译指定路径并针对apr的路径
[root@localhost apr-1.5.0]# make && make install #编译安装

      完成后编译安装httpd包:

[root@localhost ~]# tar -xf httpd-2.4.10.tar.bz2 
[root@localhost ~]# cd httpd-2.4.10
[root@localhost httpd-2.4.10]# ls
ABOUT_APACHE     BuildBin.dsp    emacs-style     LICENSE        README.cmake
acinclude.m4     buildconf       httpd.dsp       Makefile.in    README.platforms
Apache-apr2.dsw  CHANGES         httpd.spec      Makefile.win   ROADMAP
Apache.dsw       CMakeLists.txt  include         modules        server
apache_probes.d  config.layout   INSTALL         NOTICE         srclib
ap.d             configure       InstallBin.dsp  NWGNUmakefile  support
build            configure.in    LAYOUT          os             test
BuildAll.dsp     docs            libhttpd.dsp    README         VERSIONING
[root@localhost ~]#./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-modules=most --enable-mpms-shared=all --with-mpm=event #指定安装目录,配置文件目录,模块等等
[root@localhost httpd-2.4.10]# make && make install#编译安装


 注: 指定配置文件目录为/etc/httpd24,其中24是为了不与自带httpd2.2冲突而设置。

 安装完成后看一下安装目录内容:

[root@localhost ~]# tree -d /usr/local/apache/
/usr/local/apache/
├── bin    #启动和关闭脚本
├── build   
├── cgi-bin  #cgi程序文件存放目录
├── error   #服务器端错误时返回给客户端的错误页面
│   └── include
├── htdocs   #web页面所在的目录
├── icons   #httpd图标文件
│   └── small
├── include     #头文件
├── logs    #日志文件
├── man   #man手册
│   ├── man1
│   └── man8
├── manual     #配置手册
│   ├── developer
│   ├── faq
│   ├── howto
│   ├── p_w_picpaths
│   ├── misc
│   ├── mod
│   ├── platform
│   ├── programs
│   ├── rewrite
│   ├── ssl
│   ├── style
│   │   ├── css
│   │   ├── lang
│   │   ├── latex
│   │   ├── scripts
│   │   └── xsl
│   │       └── util
│   └── vhosts  #虚拟主机
└── modules     #httpd模块
[root@localhost ~]# tree /etc/httpd24/
/etc/httpd24/
├── extra                         #以下为扩展的配置文件
│?? ├── httpd-autoindex.conf      
│?? ├── httpd-dav.conf
│?? ├── httpd-default.conf     
│?? ├── httpd-info.conf
│?? ├── httpd-languages.conf
│?? ├── httpd-manual.conf
│?? ├── httpd-mpm.conf
│?? ├── httpd-multilang-errordoc.conf
│?? ├── httpd-ssl.conf
│?? ├── httpd-userdir.conf
│?? ├── httpd-vhosts.conf
│?? └── proxy-html.conf
├── httpd.conf        #主配置文件,包括添加模
├── magic
├── mime.types
└── original
    ├── extra
    │?? ├── httpd-autoindex.conf
    │?? ├── httpd-dav.conf
    │?? ├── httpd-default.conf
    │?? ├── httpd-info.conf
    │?? ├── httpd-languages.conf
    │?? ├── httpd-manual.conf
    │?? ├── httpd-mpm.conf
    │?? ├── httpd-multilang-errordoc.conf
    │?? ├── httpd-ssl.conf
    │?? ├── httpd-userdir.conf
    │?? ├── httpd-vhosts.conf
    │?? └── proxy-html.conf
    └── httpd.conf

     编写服务脚本,由于编译安装不会自动生成服务脚本,这里我们拷贝httpd目录到httpd24编辑:

[root@localhost httpd-2.4.10]# cd /etc/rc.d/init.d/
[root@localhost init.d]# cp httpd httpd24
[root@localhost init.d]# vim httpd24

wKiom1YCcFOQNtcKAANZxRGFZW0354.jpg

     添加到开机启动表并开机自启动:

[root@localhost init.d]# chkconfig --add httpd24 #
[root@localhost init.d]# chkconfig httpd24 on  
[root@localhost init.d]# chkconfig --list httpd24 
httpd24         0:off 1:off 2:on 3:on 4:on 5:on 6:off

     接下来添加PATH变量脚本,不然启动时为httpd2.2版本

[root@localhost ~]# vim /etc/profile.d/httpd.sh #添加脚本

 #内容为:
 export PATH=/usr/local/apache/bin:$PATH  #添加apache执行变量

[root@localhost ~]# . /etc/profile.d/httpd.sh #重新载入

 重启服务,80端口处于监听状态

[root@localhost ~]# ss -tnl
State      Recv-Q Send-Q             Local Address:Port               Peer Address:Port 
LISTEN     0      128                           :::22                           :::*     
LISTEN     0      128                            *:22                            *:*     
LISTEN     0      128                    127.0.0.1:631                           *:*     
LISTEN     0      128                          ::1:631                          :::*     
LISTEN     0      100                          ::1:25                           :::*     
LISTEN     0      100                    127.0.0.1:25                            *:*     
LISTEN     0      128                           :::40538                        :::*     
LISTEN     0      128                            *:57219                         *:*     
LISTEN     0      128                           :::111                          :::*     
LISTEN     0      128                            *:111                           *:*     
LISTEN     0      128                           :::80  #已经监听                      :::
[root@localhost ~]# httpd -M #查看是否为event模式,event是否已经启动

 wKioL1YCcqCTWbp-AAKB3x9bSR0888.jpg

大家看到这里默认启动的模块不是很多,可以根据自己需要自行添加模块位于:

[root@localhost ~]# cd /etc/httpd24/
[root@localhost httpd24]# ls
extra  httpd.conf  magic  mime.types  original
[root@localhost httpd24]# vim httpd.conf  #里边模块基本保持默认即可

wKioL1YCczCCAYqHAAPqaa4sycY537.jpg 这样httpd2.4就搭建完成

    

    2.编译安装maraidb-5.5.43

         (1)首先解压缩到指定目录并创建数据目录,设定权限
         (2)设置配置文件
         (3)设置服务脚本文件

[root@localhost ~]# tar xf mariadb-5.5.43-linux-x86_64.tar.gz -C /usr/local/
  #解压到/usr/local/目录
[root@localhost ~]# mkdir -pv /mydata/data  #创建一个数据存放目录
mkdir: created directory `/mydata'
mkdir: created directory `/mydata/data'
[root@localhost ~]# useradd -r mysql       #添加一个管理用户mysql
[root@localhost ~]# chown -R mysql.mysql /mydata/data/  #指定目录权限
[root@localhost local]# ln -sv mariadb-5.5.43-linux-x86_64 mysql #创建软连接指向mariadb数据目录
`mysql' -> `mariadb-5.5.43-linux-x86_64'
[root@localhost local]# cd mysql/   #通过软链接mysql可以看到mariadb的数据文件
[root@localhost mysql]# ls
bin      COPYING.LESSER  EXCEPTIONS-CLIENT  INSTALL-BINARY  man         README   share      support-files
COPYING  data            include            lib             mysql-test  scripts  sql-bench
[root@localhost mysql]# chown -R root.mysql ./*   #修改目录mysql里所有文件权限
[root@localhost mysql]# ll
total 220
drwxr-xr-x.  2 root mysql   4096 Sep 22 08:03 bin
-rw-r--r--.  1 root mysql  17987 Apr 29 14:55 COPYING
-rw-r--r--.  1 root mysql  26545 Apr 29 14:55 COPYING.LESSER
drwxr-xr-x.  3 root mysql   4096 Sep 22 08:03 data
-rw-r--r--.  1 root mysql   8245 Apr 29 14:55 EXCEPTIONS-CLIENT
drwxr-xr-x.  3 root mysql   4096 Sep 22 08:03 include
-rw-r--r--.  1 root mysql   8694 Apr 29 14:55 INSTALL-BINARY
drwxr-xr-x.  3 root mysql   4096 Sep 22 08:03 lib
drwxr-xr-x.  4 root mysql   4096 Sep 22 08:03 man
drwxr-xr-x. 11 root mysql   4096 Sep 22 08:03 mysql-test
-rw-r--r--.  1 root mysql 108813 Apr 29 14:55 README
drwxr-xr-x.  2 root mysql   4096 Sep 22 08:03 scripts
drwxr-xr-x. 27 root mysql   4096 Sep 22 08:03 share
drwxr-xr-x.  4 root mysql   4096 Sep 22 08:03 sql-bench
drwxr-xr-x.  3 root mysql   4096 Sep 22 08:03 support-files

接着初始化数据库:

[root@localhost mysql]# scripts/mysql_install_db --datadir=/mydata/data/ --user=mysql

wKioL1YCdKqwwAM4AAM_-39KJkw927.jpg

接下来把mysql里的配置文件拷贝到/etc/mysql/my.cnf(创建)里并编辑:

[root@localhost mysql]# mkdir /etc/mysql
[root@localhost mysql]# cp support-files/my-large.cnf /etc/mysql/my.cnf
[root@localhost mysql]# vim /etc/mysql/my.cnf

wKiom1YCdg2CCttwAAJjgyur8kA651.jpg配置文件设置完成

    接下来设置服务脚本:

[root@localhost mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld #拷贝服务脚本到init.d目录下名为mysqld

      为了使用mysql的安装符合系统规范,并将其开发组件导出给系统使用,这里还需要进行如下步骤:

     输出mysql的man手册至man命令的查找路径:编辑/etc/man.config,添加下行:
            MANPATH /usr/local/mysql/man

     输出msyql的头文件至系统头文件路径/usr/include,通过软链接实现:

[root@localhost ~]# ln -sv /usr/local/mysql/include/ /usr/include/mysql
`/usr/include/mysql' -> `/usr/local/mysql/include/'

     输出mysql的库文件给系统查找路径:

[root@localhost ~]# echo '/usr/local/mysql/lib/' > /etc/ld.so.conf.d/mysql.conf

配置好后让系统重新载入系统库

[root@localhost ~]# ldconfig
[root@localhost mysql]# chkconfig --add mysqld  #加入开机启动表中
[root@localhost mysql]# service mysqld start    #启动服务
Starting MySQL..                                           [  OK  ]
[root@localhost mysql]# ss -tnl  #查看到3306端口以及监听
State       Recv-Q Send-Q                           Local Address:Port                             Peer Address:Port 
LISTEN      0      128                                         :::22                                         :::*     
LISTEN      0      128                                          *:22                                          *:*     
LISTEN      0      128                                  127.0.0.1:631                                         *:*     
LISTEN      0      128                                        ::1:631                                        :::*     
LISTEN      0      100                                        ::1:25                                         :::*     
LISTEN      0      100                                  127.0.0.1:25                                          *:*     
LISTEN      0      128                                         :::40538                                      :::*     
LISTEN      0      128                                          *:57219                                       *:*     
LISTEN      0      50                                           *:3306  #端口以及监听                                      *:*     
LISTEN      0      128                                         :::111                                        :::*     
LISTEN      0      128                                          *:111                                         *:*     
LISTEN      0      128                                         :::80                                         :::*

接着我们连接数据库:

wKioL1YCdyaSZrIbAAFISmbT2Ys116.jpg

能连接到数据库说明数据库正常


    3.编译安装php-5.4.26

             依赖安装包:

                     bzip2-devel   libmcrypt-devel(epel源,本地光盘没有)   libxml2-devel

    首先下载源码包到本地目录:

[root@localhost ~]# ls  #下载好源码包
anaconda-ks.cfg         Downloads                           php-5.4.26.tar.bz2 #php包
apr-1.5.0               httpd-2.4.10                        phpMyAdmin-4.4.14.1-all-languages.zip
apr-1.5.0.tar.bz2       httpd-2.4.10.tar.bz2                Pictures
apr-util-1.5.3          install.log                         Public
apr-util-1.5.3.tar.bz2  install.log.syslog                  Templates
Desktop                 mariadb-5.5.43-linux-x86_64.tar.gz  Videos
Documents               Music                               wordpress-4.3.1-zh_CN.zip

    解压并编译:

[root@localhost ~]# tar xf php-5.4.26.tar.bz2 
[root@localhost ~]# cd php-5.4.26
[root@localhost ~]#./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --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/php.d --with-bz2  --enable-maintainer-zts
#with-openssl: 自动寻找本机安装的openssl
#mysql_config: 对mysql数据库进行交互的另外一个接口
#enable-mbstring: 多字节字符串支持
#freetype-dir: 支持的多字体类型
#jpeg-dir: 支持jpeg格式图片
#png-dir: png图片
#zlib: 压缩库
#libxml-dir: 处理xml格式文档
#enable-xml: 支持启用xml
#enable-sockets: 基于套接字通信
#apxs2: 用来把php编译成模块
#mcrypt: 支持加解密库
#config-file: php配置文件路径
#config-file-scan: 详细配置目录,到php.d查看
#bz2:支持bz2格式加密
#maintainer-zts: 编译成zts模块,worker和event模块必须加,prefork则不需要

     说明:
      (1)这里为了支持apache的worker或event这两个MPM,编译时使用了--enable-maintainer-zts选项。
      (2)如果使用PHP5.3以上版本,为了链接MySQL数据库,可以指定mysqlnd,这样在本机就不需要先安装MySQL或MySQL开发包了。mysqlnd从php 5.3开始可用,可以编译时绑定到它(而不用和具体的MySQL客户端库绑定形成依赖),但从PHP 5.4开始它就是默认设置了。

    

    编译完成后,默认httpd是不支持php的,这里需要为php提供配置文件

 [root@localhost php-5.4.26]# cp php.ini-production /etc/php.ini

     编辑apache配置文件httpd.conf以让apache支持php:

[root@localhost php-5.4.26]# cd /etc/httpd24/
[root@localhost httpd24]# ls
extra  httpd.conf  httpd.conf.bak  magic  mime.types  original
[root@localhost httpd24]# vim httpd.conf

 找到AddType选项,添加这两项:

#AddType application/x-httpd-php  .php           #Add前边没有#号。
#AddType application/x-httpd-php-source  .phps

wKioL1YCeD-TL_whAAJ3wFT9OuY315.jpg

     接着定位至DrectoryIndex index.html,修改为DirectoryIndex  index.php  index.html

wKiom1YCeLbz_GI6AAEmR6QPad8345.jpg

    而后重启httpd,或让其重新载入配置文件即可,使用httpd -M 可以查看php模块已加载

[root@localhost ~]# httpd -M | tail -5
 status_module (shared)
 autoindex_module (shared)
 dir_module (shared)
 alias_module (shared)
 php5_module (shared)      #可以看到php模块已经加载

    编译安装后默认首页文件保存于/usr/local/apache/htdocs里:

[root@localhost ~]# cd /usr/local/apache/
[root@localhost apache]# ls
bin  build  cgi-bin  error  htdocs  icons  include  logs  man  manual  modules
[root@localhost apache]# cd htdocs/
[root@localhost htdocs]# ls         #默认为html,改为php
index.html
[root@localhost htdocs]# mv index.html index.php
[root@localhost htdocs]# vim index.php   #编辑一下内容,看测试效果

wKiom1YCeYSzkooiAACINy11fvU601.jpg

      接下来在windows浏览器里输入本机IP地址就可以看到测试页:

  wKioL1YCecnQQjm_AAMPf82qRiA087.jpg


    php首页测试没有问题,这样php就安装完成

    关闭数据库测试一下:

[root@localhost htdocs]# service mysqld stop
Shutting down MySQL..                                      [  OK  ]

wKiom1YCef-BtSiTAAHA1zxZaxU657.jpg


测试没有问题

[root@localhost htdocs]# service mysqld start #重新开启mysql服务


到此,LAMP正式搭建完成。。。。