LAMP架构及安装配置

1. LAMP架构介绍

LAMP 是Linux Apache MySQL PHP的简写,其实就是把Apache, MySQL以及PHP安装在Linux系统上,组成一个环境来运行php的脚本语言。Apache是最常用的WEB服务软件,而MySQL是比较小型的数据库软件,这两个软件以及PHP都可以安装到windows的机器上。

1.1 LAMP=Linux+Apache(httpd--web服务)+MySQL(数据存储库)+PHP(脚本语言)

  • PHP网站:Google、淘宝、百度、51CTO博客、猿课论坛
  • 三个角色可以在一台机器,也可以分开(但是httpd和PHP要在一起)
  • 这2年流行的语言是python、JAVA、GO

1.2 LAMP架构图

httpd、PHP、MySQL三者如何工作 image

2. MySQL、MariaDB介绍

  • MySQL是一个关系型数据库,由mysql ab公司开发,mysql在2008年被sun公司收购(10亿刀),2009年sun公司被oracle公司收购(74亿刀)
  • MySQL官网https://www.mysql.com 最新版本5.7GA/8.0DMR
  • 从MySQL5.6开始变化比较大,5.7性能上有很大提升
  • Mariadb为MySQL的一个分支,官网https://mariadb.com/最新版本10.2
  • MariaDB主要由SkySQL公司(现更名为MariaDB公司)维护,SkySQL公司由MySQL原作者带领大部分原班人马创立.
  • Mariadb5.5版本对应MySQL的5.5,10.0对应MySQL5.6
  • Community 社区版本,Enterprise 企业版,GA(Generally Available)指通用版本,在生产环境中用的,DMR(Development Milestone Release)开发里程碑发布版,RC(Release Candidate)发行候选版本,Beta开放测x版本,Alpha内部测x版本

3. MySQL安装

  • MySQL的几个常用安装包:rpm、源码、二进制免编译
  • rpm包不能定制,不推荐;源码要编译,很慢,但是可以提高性能,暂时不学;先学别人编译好的免编译包

3.1 下载软件包

[root@localhost ~]# cd /usr/local/src   //进入目录下,以后会默认把该目录作为软件包的下载存放目录
[root@localhost src]# wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz    //下载包到该目录下

3.2 初始化Mysql安装文件

[root@localhost src]# tar zxvf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz    //解压免编译包
-  mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql   //移动目录/usr/local/,并重命名为MySQL
# 确认/usr/local/mysql没有创建,不然下面的命令会变成移动,而不是重命名
-  useradd mysql    创建MySQL用户
-  mkdir /data/      创建MySQL的数据存放目录
-  ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql  格式化mysql文件

[root@localhost src]# ls
mysql-5.6.35-linux-glibc2.5-x86_64  mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
[root@localhost src]# mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql
[root@localhost src]# cd !$
cd /usr/local/mysql
[root@localhost mysql]# ls
bin  COPYING  data  docs  include  lib  man  mysql-test  README  scripts  share  sql-bench  support-files
[root@localhost mysql]# useradd mysql     //添加mysql的专门用户
[root@localhost mysql]# mkdir -p /data/mysql     //创建datadir,数据库的文件就存在这里
[root@localhost mysql]# chown -R mysql:mysql /data/mysql   //更改权限,合理分配权限
[root@localhost mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql   //初始化
[root@localhost mysql]# echo $?
0
# 查询上一条编译命令是否成功,返回0表示成功,返回1表示失败

3.2.1 初始化过程中遇到的错误

错误1:

FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db:
Data::Dumper

解决办法:

# yum -y install autoconf            //安装缺少的autoconf

错误2:

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

解决办法:

# yum install -y libaio-devel.x86_64   //安装缺少的libaio包,一般只用装devel库文件就好了

3.3 配置Mysql

# 拷贝配置文件
[root@localhost mysql]# cp support-files/my-default.cnf  /etc/my.cnf
cp:是否覆盖"/etc/my.cnf"? y
# 拷贝启动脚本文件并修改其属性
[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@localhost mysql]# chmod 755 /etc/init.d/mysqld
# 修改启动脚本
[root@localhost mysql]# vim /etc/init.d/mysqld
# 需要修改的地方,basedir和datadir
basedir=/usr/local/mysql
datadir=/data/mysql

# 把启动脚本添加到开机启动项,并设定开机启动
[root@localhost mysql]# chkconfig --add mysqld   //将mysqld服务加入开机服务
[root@localhost mysql]# chkconfig  --list       //查看是否加入开机服务成功   

# 命令启动mysql服务
[root@localhost mysql]# service mysqld start
Starting MySQL.Logging to '/data/mysql/localhost.localdomain.err'.
 SUCCESS!

# 查看mysql进程是否启动和它的端口
ps aux | grep mysql
netstat -lntp      mysql默认端口3306

# 查看mysql无法正常启动的错误日志
[root@localhost mysql]# cat /data/mysql/localhost.localdomain.err  //通常是/data/mysql/主机名.err文件

3.3.1 命令行启动mysql方法

  • 假如没办法将脚本放入到开机服务,或者没有启动脚本模板拷贝,要启动mysql服务,用命令行 /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --user=mysql --datadir=/data/mysql & ps

  • 用服务命令启动mysql,可以用/etc/init.d/mysqld stop命令关闭,但是用上面命令行的方式开启的, 就不行了。那该怎么停止呢?

  • 只能用killall mysql关闭,不能用kill mysql关闭,因为kill会直接终止进程,而killall会等正 在读写的数据完成后再结束,如果后面工作遇到killall没反应,是因为数据量很大,那就一直等,不要急。

  • killall命令centos7默认没安装,安装命令如下

# yum install -y psmisc

4. Apache(httpd)安装

4.1 下载源码包

  • apache官网地址: http://www.apache.org/dyn/closer.cgi
  • Apache是一个基金会的名字,httpd才是我们要安装的软件包,早期它的名字就叫apache,现在主流版本是2.4
  • 2.2和2.4的安装方法不同,涉及到依赖的APR通用函数库版本不同,我们的CentOS7自带的APR包是2.2的,而2.4的需要下载自己编译
[root@localhost mysql]# cd /usr/local/s
[root@host src]# wget http://mirrors.cnnic.cn/apache/httpd/httpd-2.4.32.tar.gz      //httpd的二进制包
[root@host src]# wget http://mirrors.hust.edu.cn/apache/apr/apr-1.6.3.tar.gz        //APR库源码包
[root@host src]# wget http://mirrors.hust.edu.cn/apache/apr/apr-util-1.6.1.tar.gz  //APR-util库源码包

4.2 解压缩

[root@localhost src]# tar zxvf httpd-2.4.32.tar.gz
[root@localhost src]# tar zxvf apr-1.6.3.tar.gz 
[root@localhost src]# tar zxvf apr-util-1.6.1.tar.gz

4.3 安装apr

[root@localhost src]# cd /usr/local/src/apr-1.6.3
[root@localhost apr-1.6.3]# ./configure --prefix=/usr/local/apr
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
Configuring APR library
Platform: x86_64-pc-linux-gnu
checking for working mkdir -p... yes
APR Version: 1.6.3
checking for chosen layout... apr
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/usr/local/src/apr-1.6.3':
configure: error: no acceptable C compiler found in $PATH     // !报错!
See `config.log' for more details

# 编译出现报错,解决办法是安装gcc套件
[root@localhost apr-1.6.3]# yum install -y gcc

# 再次编译出现报错,错误代码如下:
... ...
config.status: executing libtool commands
rm: cannot remove 'libtoolT': No such file or directory
config.status: executing default commands

# 解决办法是编辑 configure文件,查找 $RM "$cfgfile" 这个地方,用#注释掉

[root@localhost apr-1.6.3]# vim configure   
/$RM "$cfgfile        //查找这个,并用#注释掉

[root@localhost apr-1.6.3]# make && make install  //编译
[root@localhost apr-1.6.3]# echo $?
0

4.4 安装APR-util

[root@localhost apr-1.6.3]# cd /usr/local/src/apr-util-1.6.1
[root@localhost apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr   //初始化
[root@localhost apr-util-1.6.1]# echo $?
0
[root@localhost apr-util-1.6.1]# make && make install   //编译
... ... # 报错
xml/apr_xml.c:35:19: 致命错误:expat.h:没有那个文件或目录
 #include <expat.h>
# 解决方案:安装expat库
[root@localhost apr-util-1.6.1]# yum install expat-devel
[root@localhost apr-util-1.6.1]# make && make install   //再编译
[root@localhost apr-util-1.6.1]# echo $?
0

4.5 安装httpd

[root@localhost apr-util-1.6.1]# cd /usr/local/httpd-2.4.32
[root@localhost httpd-2.4.32]# ./configure --prefix=/usr/local/apache2.4 --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-so --enable-mods-shared=most
####
--enable-so \       //表示开启支持动态扩展模块,PHP还有Appache都支持以模块的形式存在
--enable-mods-shared=most  //表示开启大多数模块
####
# 编译报错
...  ...
checking for pcre-config... false
configure: error: pcre-config for libpcre not found. PCRE is required and available from http://pcre.org/

# 解决方案,安装pcre库
[root@localhost httpd-2.4.32]# yum install -y pcre-devel.x86_64

# 再初始化
[root@localhost httpd-2.4.32]# ./configure --prefix=/usr/local/apache2.4 --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-so --enable-mods-shared=most
[root@localhost httpd-2.4.32]# echo $?
0

# 编译
[root@localhost httpd-2.4.32]# make && make install
[root@localhost httpd-2.4.32]# echo $?
0

# 如果编译的过程中,出现如下报错:
……
#报错,也可能不出现,但是后面出现了的话,注意是因为apr包被破坏的原因
collect2: error: ld returned 1 exit status
make[2]: *** [htpasswd] Error 1
make[2]: Leaving directory `/usr/local/src/httpd-2.4.27/support'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/usr/local/src/httpd-2.4.27/support'
# 解决方案:是因为没有apr包,可能是第一次初始化的时候,出过一次错,把apr包弄乱了,回头重新安装apr包和apr-util包

# 启动apache
[root@localhost httpd-2.4.32]# /usr/local/apache2.4/bin/apachectl start    //下面有提示,但并不是错误的
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

[root@localhost httpd-2.4.32]# ps aux | grep httpd
root     63343  0.0  0.1  70904  2212 ?        Ss   01:10   0:00 /usr/local/apache2.4/bin/httpd -k start
daemon   63344  0.0  0.1 359868  2212 ?        Sl   01:10   0:00 /usr/local/apache2.4/bin/httpd -k start
daemon   63345  0.0  0.1 359868  2212 ?        Sl   01:10   0:00 /usr/local/apache2.4/bin/httpd -k start
daemon   63346  0.0  0.1 359868  2212 ?        Sl   01:10   0:00 /usr/local/apache2.4/bin/httpd -k start
root     63429  0.0  0.0 112676   984 pts/0    R+   01:10   0:00 grep --color=auto httpd

[root@localhost httpd-2.4.32]# netstat -lntp     //查看端口,httpd默认监听的端口是80端口
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      852/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1069/master         
tcp6       0      0 :::80                   :::*                    LISTEN      63343/httpd         
tcp6       0      0 :::22                   :::*                    LISTEN      852/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      1069/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      1884/mysqld

5. 编译安装PHP

把PHP放到最后安装是因为PHP编译的时候需要指定apache和mysql的路径。而apache和mysql的安装顺序就无所谓了。

目前PHP的主流版本是 5.6 和 7.1

5.1 下载PHP 5.6

[root@localhost httpd-2.4.32]# cd /usr/local/src
[root@localhost src]# wget http://cn2.php.net/distributions/php-5.6.30.tar.bz2
[root@localhost src]# tar jxf php-5.6.30.tar.bz2
tar (child): bzip2: Cannot exec: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now   //说明没有安装(bzip2)这个软件包。解决方法:安装(bzip2)软件包
[root@localhost src]# yum -y install bzip2
[root@localhost src]# tar jxf php-5.6.30.tar.bz2

5.2 配置编译参数、编译并安装

[root@localhost src]# cd php-5.6.30
# 初始化 (LAMP,M=mysql)
[root@localhost php-5.6.30]# ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2.4/bin/apxs --with-config-file-path=/usr/local/php/etc  --with-mysql=/usr/local/mysql --with-pdo-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-bz2 --with-openssl --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-mbstring --enable-sockets --enable-exif
——————
# 初始化 (LAMP,M=mariadb)
[root@localhost php-5.6.30]# ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2.4/bin/apxs --with-config-file-path=/usr/local/php/etc  --with-mysql=/usr/local/mariadb --with-pdo-mysql=/usr/local/mariadb --with-mysqli=/usr/local/mariadb/bin/mysql_config --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-bz2 --with-openssl --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-mbstring --enable-sockets --enable-exif
——————

# 解释
# --prefix 安装目录
#--with-apxs2 apache的工具,能让我们不用人工干涉它,帮忙把扩展模块放到apache的modules目录里,并且在它的配置文件里加上一行mod-modules,自动的给你配置上这个模块,一般可以给你加上so文件,但是不会自动加载配置模块,这就是为什么要后安装PHP
#--with-mysql --with-pdo-mysql --with-mysqli 定义给mysql通信的相关函数或者驱动,让php能和mysql交换数据
#--with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-bz2 --with-openssl --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-mbstring --enable-sockets --enable-exif 这些都是一些必要的参数

#在编译的过程中,会出现一些错误,往往是因为缺少相关的库文件

# 报错
1. configure: error: xml2-config not found. Please check your libxml2 installation.
yum install libxml2-devel -y

2. configure: error: Cannot find OpenSSL's <evp.h>
yum install -y openssl openssl-devel

3. configure: error: Please reinstall the BZip2 distribution
yum install -y bzip2 bzip2-devel

4. configure: error: jpeglib.h not found.
yum install -y libjpeg-devel

5. configure: error: png.h not found.
yum install -y libpng-devel

6. configure: error: freetype-config not found.
yum install -y freetype-devel

7. configure: error: mcrypt.h not found. Please reinstall libmcrypt.
(centos源不能安装libmcrypt-devel,由于版权的原因,没有自带mcrypt的包rpm -qa |grep limcrypt limcrypt-devel,此源为rethot社区版的源)
安装第三方yum源
wget http://www.atomicorp.com/installers/atomic
sh ./atomic
yum install php-mcrypt libmcrypt libmcrypt-devel

# 编译
[root@localhost php-5.6.30]# echo $?
0
[root@localhost php-5.6.30]# make && make install
[root@localhost php-5.6.30]# echo $?
0

5.3 拷贝php配置文件

# 把参考配置文件拷贝到定义的路径 
[root@localhost php-5.6.30]# cp php.ini-production /usr/local/php/etc/php.ini 

# 也可以自己找一下, /usr/local/php/bin/php -i | less 查看一些php的参数
# php.ini-production 使用于生产环境的配置文件,还有适用于开发和实验环境的配置文件php.ini-development

5.4 查看

[root@localhost php-5.6.30]# ls /usr/local/php/    //php核心文件
bin etc include lib php
[root@localhost php-5.6.30]# ls /usr/local/php/bin/       //php核心的二进制文件在这个文件夹里面
pear peardev pecl phar phar.phar php php-cgi php-config phpize
[root@localhost php-5.6.30]# du -sh /usr/local/php/bin/php     
36M	/usr/local/php/bin/php
[root@localhost php-5.6.30]# du -sh /usr/local/apache2.4/modules/libphp5.so   //PHP和Apache结合的模块
37M	/usr/local/apache2.4/modules/libphp5.so

# 查看php加载的模块(静态)
[root@localhost php-5.6.30]# /usr/local/php/bin/php -m 

# 查看php加载的模块(静+动态)
[root@localhost php-5.6.30]# /usr/local/php/bin/php -M

# apache配置文件
[root@localhost php-5.6.30]#vi  /usr/local/apache2.4/conf/httpd.conf

5.5 安装PHP7

和安装PHP5类似的步骤

cd /usr/local/src/
wget http://cn2.php.net/distributions/php-7.1.6.tar.bz2
tar zxf php-7.1.6.tar.bz2
cd php-7.1.6
./configure --prefix=/usr/local/php7 --with-apxs2=/usr/local/apache2.4/bin/apxs --with-config-file-path=/usr/local/php7/etc --with-pdo-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-bz2 --with-openssl --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-mbstring --enable-sockets --enable-exif
make && make install
ls /usr/local/apache2.4/modules/libphp7.so
cp php.ini-production /usr/local/php7/etc/php.ini

猜你喜欢

转载自my.oschina.net/zhouyuntai/blog/1647058