LAMP环境搭建详解

Posted l-dongf

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LAMP环境搭建详解相关的知识,希望对你有一定的参考价值。

一、LAMP简介

技术分享图片

LAM(M)P:

  • L: linux
  • A: apache (httpd)
  • M: mysql, mariadb
  • M:memcached
  • P: php, perl, python

WEB资源类型:

  • 静态资源:原始形式与响应内容一致,在客户端浏览器执行
  • 动态资源:原始形式通常为程序文件,需要在服务器端执行之后,将执行结果返回给客户端

Web相关语言:

  • 客户端技术: htmljavascript
  • 服务器端技术:php, jsp,python,asp

CGI:Common Gateway Interface 通用网关接口;可以让一个客户端,从网页浏览器通过http服务器向执行在网络服务器上的程序传输数据;CGI描述了客户端和服务器程序之间传输的一种标准

二、PHP

1、php简介

php:通用服务器端脚本编程语言,主要用于web开发实现动态web页面,也是最早实现将脚本嵌入HTML源码文档中的服务器端脚本语言之一。同时,php还提供了一个命令行接口,因此其也可以在大多数系统上作为一个独立的shell来使用

Zend Engine:PHP脚本语言的解释器,它最早是由以色列理工学院(Technion)的学生Andi Gutmans和Zeev Suraski所开发,Zend也正是此二人名字的合称。后来两人联合创立了Zend Technologies公司。Zend Engine的出现将PHP代码的处理过程分成了两个阶段:首先是分析PHP代码并将其转换为称作Zend opcode的二进制格式(类似Java的字节码),并将其存储于内存中;第二阶段是使用Zend Engine去执行这些转换后的Opcode。

Opcode:一种PHP脚本编译后的中间语言,类似于Java的ByteCode,或者.NET的MSL。PHP执行PHP脚本代码一般会经过如下4个步骤:扫描-->分析-->编译-->执行

  1. Scanning 词法分析,将PHP代码转换为语言片段(Tokens);
  2. Parsing 语义分析,将Tokens转换成简单而有意义的表达式;
  3. Compilation 将表达式编译成Opocdes;
  4. Execution 顺次执行Opcodes,每次一条,从而实现PHP脚本的功能。

2、php配置

配置文件:/etc/php.ini, /etc/php.d/*.ini

/etc/php.ini配置文件格式:

[foo]:Section Header
directive = value
注释符:较新的版本中,已经完全使用;进行注释
#:纯粹的注释信息
;:用于注释可启用的directive

max_execution_time= 30 最长执行时间30s 
memory_limit 128M 生产不够,可调大 
display_errors off  调试使用,不要打开,否则可能暴露重要信息 
display_startup_errors off  建议关闭 
post_max_size 8M   最大上传数据大小,生产可能临时要调大,比下面项要大 
upload_max_filesize  2M  最大上传文件,生产可能要调大 
max_file_uploads = 20 同时上传最多文件数 
date.timezone =Asia/Shanghai  指定时区 
short_open_tag=on 开启短标签,如<? phpinfo();?> 

参考官方文档:

php.ini的核心配置选项文档:http://php.net/manual/zh/ini.core.php

php.ini配置选项列表:http://php.net/manual/zh/ini.list.php

3、php加速器

? 基于PHP的特殊扩展机制如opcode缓存扩展也可以将opcode缓存于php的共享内存中,从而可以让同一段代码的后续重复执行时跳过编译阶段以提高性能。这些加速器并非真正提高了opcode的运行速度,而仅是通过分析opcode后并将它们重新排列以达到快速执行的目的

常见的php加速器有:

  • APC (Alternative PHP Cache) :遵循PHP License的开源框架,PHP opcode缓存加速器,目前的版本不适用于PHP 5.4;项目地址http://pecl.php.net/package/APC
  • eAccelerator:源于Turck MMCache,早期的版本包含了一个PHP encoder和PHP loader,目前encoder已经不在支持。项目地址 http://eaccelerator.net/
  • XCache:快速而且稳定的PHP opcode缓存,经过严格测试且被大量用于生产环境。项目地址:http://xcache.lighttpd.net/,收录EPEL源
  • Zend Optimizer和Zend Guard Loader:Zend Optimizer并非一个opcode加速器,它是由Zend Technologies为PHP5.2及以前的版本提供的一个免费、闭源的PHP扩展,其能够运行由Zend Guard生成的密的PHP代码或模糊代码。 而Zend Guard Loader则是专为PHP5.3提供的类似于Zend Optimizer功能的扩展。项目地址http://www.zend.com/en/products/guard/runtime-decoders
  • 5、NuSphere PhpExpress:NuSphere的一款开源PHP加速器,它支持装载通过NuSphere PHP Encoder编码的PHP程序文件,并能够实现对常规PHP文件的执行加速。项目地址http://www.nusphere.com/products/phpexpress.htm

三、部署LAMP

1、Modules工作方式部署

? 1)安装

~]# yum install httpd php php-mysql mariadb-server -y
~]# systemctl start httpd mariadb

? 2)测试PHP连接数据库

~]# vim /var/www/html/index.php
<?php
try {
    $user='root';
    $pass='';
    $dbh = new PDO('mysql:host=localhost;dbname=mysql', $user, $pass);
    foreach($dbh->query('SELECT user,host from user') as $row) {
        print_r($row);
    }
    $dbh = null;
}
catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}
?>
~]# curl 192.168.0.7  #如果返回的是数据库的查询信息则说明成功
~]# rm -f /var/www/html/index.php

? 3)配置数据库

~]# mysql_secure_installation 
Enter current password for root (enter for none):
Set root password? [Y/n] Y
New password: ******
Re-enter new password: ******
Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] n
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y
 ... Success!

? 4)编译安装php-xcache

~]# yum groupinstall "Development Tools" -y
~]# yum install php-devel -y
~]# wget http://xcache.lighttpd.net/pub/Releases/2.0.1/xcache-2.0.1.tar.gz
~]# tar xf xcache-2.0.1.tar.gz
~]# cd xcache-2.0.1/
xcache-2.0.1]# phpize  #生成configure脚本
xcache-2.0.1]# ./configure --enable-xcache --with-php-config=/usr/bin/php-config
xcache-2.0.1]# make && make install
xcache-2.0.1]# cp xcache.ini /etc/php.d/
xcache-2.0.1]# systemctl restart httpd

2、FastCGI工作方式部署

/etc/php-fpm.d/www.conf :PHP配置文件

listen = 127.0.0.1:9000 监听端口
listen.allowed_clients = 127.0.0.1 允许客户端的IP
pm.max_children = 50 最多链接数
pm.start_servers = 5 初识启动多少个
pm.min_spare_servers = 5 最小保留空闲进程
pm.max_spare_servers = 35 最大空闲进程
pm.max_requests = 500 最大请求数

参考文档:

? php.ini核心配置的详细说明: http://php.net/manual/zh/ini.core.php
Php.ini配置选项列表: http://php.net/manual/zh/ini.list.php

[[email protected] ~]# yum install httpd php-fpm php-mysql mariadb-server -y
[[email protected] ~]# systemctl enable httpd php-fpm mariadb
[[email protected] ~]# httpd -M |grep fcgi  #确保fcgi模块启用
 proxy_fcgi_module (shared)
[[email protected] ~]# vim /etc/httpd/conf.d/fcgi.conf  #加入以下配置
DirectoryIndex index.php
ProxyRequests Off  #关闭正向代理
ProxyPassMatch ^/(.*.php)$  fcgi://127.0.0.1:9000/var/www/html/$1 #将请求php资源的请求发送给PHP进程
[[email protected] ~]# systemctl start httpd php-fpm mariadb

3、编译安装LAMP

环境:

linux:CentOS7.5

Apache:apr-1.6.3、apr-util-1.6.1、httpd-2.4.33

MariaDB:mariadb-10.2.15

PHP:php-7.1.18

? 1)检查环境并准备安装包

[[email protected] src]# yum -y groupinstall "Development Tools"
[[email protected] src]# yum -y install openssl-devel expat-devel pcre-devel libxml2-devel  bzip2-devel libmcrypt-devel bison-devel boost-devel cmake gnutls-devel libaio-devel libarchive-devel libcurl-devel libevent-devel ncurses-devel  #注意:需要配置epel源
[[email protected] src]# wget http://mirror.bit.edu.cn/apache//apr/apr-1.6.3.tar.gz
[[email protected] src]# wget http://mirror.bit.edu.cn/apache//apr/apr-util-1.6.1.tar.gz
[[email protected] src]# wget http://mirrors.shu.edu.cn/apache//httpd/httpd-2.4.33.tar.gz
[[email protected] src]# wget http://sfo1.mirrors.digitalocean.com/mariadb//mariadb-10.2.15/source/mariadb-10.2.15.tar.gz
[[email protected] src]# wget http://mirrors.sohu.com/php/php-7.1.18.tar.gz

? 2)编译安装Apache,并配置

[[email protected] src]# useradd -r -s /sbin/nologin apache
[[email protected] src]# tar xf apr-1.6.3.tar.gz 
[[email protected] src]# tar xf apr-util-1.6.1.tar.gz 
[[email protected] src]# tar xf httpd-2.4.33.tar.gz 
[[email protected] src]# cp -a apr-util-1.6.1 httpd-2.4.33/srclib/apr-util
[[email protected] src]# cp -a apr-1.6.3 httpd-2.4.33/srclib/apr
[[email protected] src]# cd httpd-2.4.33
[[email protected] httpd-2.4.33]# ./configure --prefix=/lamp/application/httpd24 > --enable-so > --enable-ssl > --enable-cgi > --enable-rewrite > --with-zlib > --with-pcre  > --with-included-apr > --enable-modules=most > --enable-mpms-shared=all > --with-mpm=prefork
[[email protected] httpd-2.4.33]# make -j 4 && make install
[[email protected] httpd-2.4.33]# vim /lamp/application/httpd24/conf/httpd.conf  #修改httpd配置文件
ServerName www.example.com:80
User apache
Group apache
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so  #启用cfgi模块
LoadModule proxy_module modules/mod_proxy.so  #启用代理模块
<IfModule dir_module>
    DirectoryIndex index.php index.html  #加入index.php
</IfModule>
-----> 插入以下内容 <-----
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
ProxyRequests Off
ProxyPassMatch  ^/(.*.php)$ fcgi://127.0.0.1:9000/lamp/application/httpd24/htdocs/$1  #注意此处路径
[[email protected] src]# echo 'PATH=/lamp/application/httpd24/bin/:$PATH' > /etc/profile.d/httpd.sh
[[email protected] src]# . /etc/profile.d/httpd.sh
[[email protected] src]# apachectl start
[[email protected] src]# curl 127.0.0.1/index.html
It works! #看到It works!表示工作正常

? 3)编译安装MariaDB,并配置

[[email protected] src]# useradd -r -s /sbin/nologin -d /lamp/data/mysqldb mysql
[[email protected] src]# mkdir -p /lamp/data/mysqldb
[[email protected] src]# chown -R mysql.mysql /lamp/data/mysqldb/
[[email protected] src]# tar xf mariadb-10.2.15.tar.gz 
[[email protected] mariadb-10.2.15]# cd mariadb-10.2.15/
[[email protected] mariadb-10.2.15]# cmake . -DCMAKE_INSTALL_PREFIX=/lamp/application/mysql > -DMYSQL_DATADIR=/lamp/data/mysqldb/ > -DMYSQL_UNIX_ADDR=/lamp/data/mysqldb/mysql.sock > -DSYSCONFDIR=/etc > -DMYSQL_USER=mysql > -DWITH_INNOBASE_STORAGE_ENGINE=1 > -DWITH_ARCHIVE_STORAGE_ENGINE=1 > -DWITH_BLACKHOLE_STORAGE_ENGINE=1 > -DWITH_PARTITION_STORAGE_ENGINE=1  > -DWITHOUT_MROONGA_STORAGE_ENGINE=1 > -DWITH_DEBUG=0 > -DWITH_READLINE=1 > -DWITH_SSL=system > -DWITH_ZLIB=system > -DWITH_LIBWRAP=0 > -DENABLED_LOCAL_INFILE=1  > -DDEFAULT_CHARSET=utf8 > -DDEFAULT_COLLATION=utf8_general_ci
[[email protected] mariadb-10.2.15]# make -j4 && make install
[[email protected] mariadb-10.2.15]# echo 'export PATH=/lamp/application/mysql/bin:$PATH' >/etc/profile.d/mysql.sh
[[email protected] mariadb-10.2.15]# . /etc/profile.d/mysql.sh
[[email protected] mariadb-10.2.15]# cd /lamp/application/mysql/
[[email protected] mysql]# scripts/mysql_install_db --datadir=/lamp/data/mysqldb/ --user=mysql --basedir=/lamp/application/mysql/
[[email protected] mysql]# cp support-files/my-huge.cnf /etc/my.cnf
[[email protected] mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[[email protected] mysql]# chkconfig --add mysqld
[[email protected] mysql]# chkconfig mysqld on
[[email protected] mysql]# service mysqld start
[[email protected] mysql]# mysql
Server version: 10.2.15-MariaDB-log Source distribution
MariaDB [(none)]>  #成功连接mysql

? 4)编译安装php,并配置

[[email protected] src]# tar xf php-7.1.18.tar.gz 
[[email protected] src]# cd php-7.1.18/
[[email protected] php-7.1.18]# ./configure --prefix=/lamp/application/php7 > --enable-mysqlnd > --with-mysqli=mysqlnd > --with-openssl > --with-pdo-mysql=mysqlnd > --enable-mbstring  > --with-freetype-dir > --with-jpeg-dir > --with-png-dir > --with-zlib > --with-libxml-dir=/usr > --enable-xml > --enable-sockets > --enable-fpm  > --with-config-file-path=/etc > --with-config-file-scan-dir=/etc/php.d > --enable-maintainer-zts > --disable-fileinfo
[[email protected] php-7.1.18]# make -j 4 && make install
[[email protected] php-7.1.18]# cp php.ini-production /etc/php.ini
[[email protected] php-7.1.18]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[[email protected] php-7.1.18]# chmod +x /etc/init.d/php-fpm
[[email protected] php-7.1.18]# chkconfig --add php-fpm
[[email protected] php-7.1.18]# chkconfig php-fpm on
[[email protected] php-7.1.18]# cd /lamp/application/php7/
[[email protected] php7]# cp etc/php-fpm.conf.default etc/php-fpm.conf
[[email protected] php7]# cp etc/php-fpm.d/www.conf.default etc/php-fpm.d/www.conf
[[email protected] php-fpm.d]# vim etc/php-fpm.d/www.conf
user = apache
group = apache
[[email protected] php7]# echo 'PATH=/lamp/application/php7/bin:/lamp/application/php7/sbin:$PATH' > /etc/profile.d/php-fpm.sh
[[email protected] php7]# . /etc/profile.d/php-fpm.sh
[[email protected] php7]# service php-fpm start
[[email protected] php7]# ss -tnl |grep 9000
LISTEN     0      128    127.0.0.1:9000  #9000端口已监听

? 5)测试lamp

[[email protected] ~]# vim /lamp/application/httpd24/htdocs/test.php
<?php
$dsn='mysql:host=127.0.0.1;dbname=mysql';
$username='root';
$passwd='';
$dbh=new PDO($dsn,$username,$passwd);
var_dump($dbh);
?>
[[email protected] ~]# curl 127.0.0.1/test.php
object(PDO)#1 (0) {}  #如果返回PDO则说明成功
[[email protected] ~]# rm -f /lamp/application/httpd24/htdocs/test.php

四、部署常见LAMP应用

  • PhpMyAdmin是一个以PHP为基础,以Web-Base方式架构在网站主机上的MySQL的数据库管理工具,让管理者可用Web接口管理MySQL数据库。
  • WordPress是一种使用PHP语言开发的博客平台,用户可以在支持PHP和MySQL数据库的服务器上架设属于自己的网站。也可把 WordPress当作一个内容管理系统(CMS)来使用。
  • PHPWind:2003年发布了PHPWind的前身版本ofstar,并发展成为包含BBS、CMS、博客、SNS等一系列程序的通用型建站软件, 于2008年加入阿里巴巴集团。
  • Crossday Discuz! Board(简称 Discuz!)是一套通用的社区论坛软件系统。自2001年6月面世以来,是全球成熟度最高、覆盖率最大的论坛软件系统之一。2010年8月23日,与腾讯达成收购协议。
  • ECShop是一款B2C独立网店系统,适合企业及个人快速构建个性化网上商店。系统是基于PHP语言及MYSQL数据库构架开发的跨平台开源程序。2006年6月,ECShop推出第一个版本1.0。

1、布署phpMyadmin

~]# wget https://files.phpmyadmin.net/phpMyAdmin/4.0.10.20/phpMyAdmin-4.0.10.20-all-languages.zip
~]# unzip phpMyAdmin-4.0.10.20-all-languages.zip
~]# mv phpMyAdmin-4.0.10.20-all-languages /var/www/html/pma
~]# cd /var/www/html/pma
pma]# cp config.sample.inc.php config.inc.php       
pma]# yum install php-mbstring -y
pma]# systemctl restart httpd

访问:http://192.168.0.7/pma/按照提示输入用户名密码(数据库的管理用户)等

2、布署WordPress

[[email protected] ~]# mysql -uroot -p
MariaDB [(none)]> CREATE DATABASE wpdb;
MariaDB [(none)]> GRANT ALL ON wpdb.* TO [email protected]'192.168.0.%' IDENTIFIED BY 'wppass';
[[email protected] ~]# wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz
[[email protected] ~]# tar xf wordpress-4.9.4-zh_CN.tar.gz -C /var/www/html/
[[email protected] ~]# chown -R apache:apache /var/www/html/wordpress/

访问:http://192.168.0.7/wordpress/,按提示填入相应信息

五、继3.3编译安装LAMP——实现多虚拟主机的应用部署

? 1)配置虚拟主机

[[email protected] ~]# cd /lamp/application/httpd24/conf/
[[email protected] conf]# vim httpd.conf
#ProxyPassMatch  ^/(.*.php)$ fcgi://127.0.0.1:9000/lamp/application/httpd24/htdocs/$1  #将此行注释掉
Include conf/extra/httpd-vhosts.conf  #将此行启用
[[email protected] conf]# vim extra/httpd-vhosts.conf
<VirtualHost *:80>
    DocumentRoot "/lamp/data/www/wordpress"
    ServerName blog.dongfei.com
    ErrorLog "logs/blog.dongfei.com-error_log"
    CustomLog "logs/blog.dongfei.com-access_log" common
    <Directory "/lamp/data/www/wordpress">
        Require all granted
    </Directory>
    ProxyPassMatch  ^/(.*.php)$ fcgi://127.0.0.1:9000/lamp/data/www/wordpress/$1
</VirtualHost>
<VirtualHost *:80>
    DocumentRoot "/lamp/data/www/discuz"
    ServerName bbs.dongfei.com
    ErrorLog "logs/bbs.dongfei.com-error_log"
    CustomLog "logs/bbs.dongfei.com-access_log" common
    <Directory "/lamp/data/www/discuz">
        Require all granted
    </Directory>
    ProxyPassMatch  ^/(.*.php)$ fcgi://127.0.0.1:9000/lamp/data/www/discuz/$1
</VirtualHost>
[[email protected] ~]# mkdir -p /lamp/data/www/{wordpress,discuz}
[[email protected] ~]# apachectl restart

? 2)配置数据库

MariaDB [(none)]> CREATE DATABASE discuzdb;
MariaDB [(none)]> GRANT ALL ON discuzdb.* TO [email protected]'192.168.0.%' IDENTIFIED BY 'dpass';
MariaDB [(none)]> CREATE DATABASE wordpressdb;
MariaDB [(none)]> GRANT ALL ON wordpressdb.* TO [email protected]'192.168.0.%' IDENTIFIED BY 'wpass';

? 3)部署Discuz

[[email protected] src]# wget http://download.comsenz.com/DiscuzX/3.3/Discuz_X3.3_SC_UTF8.zip
[[email protected] src]# unzip Discuz_X3.3_SC_UTF8.zip
[[email protected] src]# mv upload/* /lamp/data/www/discuz/
[[email protected] src]# setfacl -R -m u:apache:rwx /lamp/data/www/discuz/

访问:http://bbs.dongfei.com,按照提示填入相应信息

? 4)部署WordPress

[[email protected] src]# wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz
[[email protected] src]# tar xf wordpress-4.9.4-zh_CN.tar.gz
[[email protected] src]# mv wordpress/* /lamp/data/www/wordpress/
[[email protected] src]# setfacl -R -m u:apache:rwx /lamp/data/www/wordpress/

访问:http://blog.dongfei.com,按照提示填入相应信息

六、一键搭建LAMP脚本

1、LAMP-yum.sh

#!/bin/bash
# -----------------------------------------
# Filename:     lamp_yum.sh
# Revision:     1.0v
# Date:         2018-06-23
# Author:       LiDongfei
# Email:        [email protected]
# Website:      www.cnblogs.com/L-dongf
# Description:  This is the lamp_yum script
# -----------------------------------------
# Copyright:    @2018 Dongfei
# License:      GPL

# kernel: 3.10.0-862.el7.x86_64
# linux: CentOS 7.5
# Apache: 2.4.6
# MariaDB: 5.5.56
# PHP: 5.4.16
# xcache: 2.0.1
# Way of working: Modules

check() {
        if [ $? -eq 0 ]; then
                echo -e "33[32mOK!33[0m"
        else
                echo -e "33[31mError!33[0m"
                exit
        fi
}

# install software
yum install -y httpd php php-mysql mariadb-server
check

# start service
systemctl start httpd mariadb
check

# make and make install php-xcache
yum groupinstall "Development Tools" -y
check
yum install php-devel -y
check

mkdir /root/src
cd /root/src
wget http://xcache.lighttpd.net/pub/Releases/2.0.1/xcache-2.0.1.tar.gz
check
tar xf xcache-2.0.1.tar.gz
cd xcache-2.0.1/
phpize
./configure --enable-xcache --with-php-config=/usr/bin/php-config
make && make install
cp -n xcache.ini /etc/php.d/
systemctl restart httpd

# output info log
echo -e "33[32mInstall LAMP Completed!33[0m"

2、LAMP-make.sh

软件安装目录:/lamp/application/

数据库库目录:/lamp/data/mysqldb

#!/bin/bash
# -----------------------------------------
# Filename:     lamp_make.sh
# Revision:     1.0v
# Date:         2018-06-23
# Author:       LiDongfei
# Email:        [email protected]
# Website:      www.cnblogs.com/L-dongf
# Description:  This is the lamp_make script
# -----------------------------------------
# Copyright:    @2018 Dongfei
# License:      GPL

trap 'exit' 2
clear
echo '# Kernel: 3.10.0-862.el7.x86_64'
echo '# Linux: CentOS 7.5'
echo '# Apr: 1.6.3'
echo '# Apr-util: 1.6.1'
echo '# Apache: 2.4.33'
echo '# MariaDB: 10.2.15'
echo '# PHP: 7.1.18'
echo '# Way of working: FastCGI'
read -p "Press any key to start...or Press Ctrl+C to cancel" tmpvar

cpus=`cat /proc/cpuinfo |grep processor |wc -l`

check() {
        if [ $? -eq 0 ]; then
                echo -e "33[32mOK!33[0m"
        else
                echo -e "33[31mError!33[0m"
                exit
        fi
}

# check yum package
yum -y groupinstall "Development Tools"
check
yum -y install openssl-devel expat-devel pcre-devel libxml2-devel  bzip2-devel bison-devel boost-devel cmake gnutls-devel libaio-devel libarchive-devel libcurl-devel libevent-devel ncurses-devel
check
yum -y install libmcrypt-devel
check

# Get source code
mkdir /root/src
cd /root/src
wget http://mirror.bit.edu.cn/apache//apr/apr-1.6.3.tar.gz
check
wget http://mirror.bit.edu.cn/apache//apr/apr-util-1.6.1.tar.gz
check
wget http://mirrors.shu.edu.cn/apache//httpd/httpd-2.4.33.tar.gz
check
wget http://sfo1.mirrors.digitalocean.com/mariadb//mariadb-10.2.15/source/mariadb-10.2.15.tar.gz
check
#wget http://mirrors.sohu.com/php/php-7.1.18.tar.gz
wget http://php.net/get/php-7.1.18.tar.gz/from/this/mirror -O php-7.1.18.tar.gz
check

# ============================ Apache ============================
# make and make install httpd, configure httpd.conf
! id apache &>/dev/null && useradd -r -s /sbin/nologin apache
cd /root/src/
tar xf apr-1.6.3.tar.gz
tar xf apr-util-1.6.1.tar.gz
tar xf httpd-2.4.33.tar.gz
cp -a apr-util-1.6.1 httpd-2.4.33/srclib/apr-util
cp -a apr-1.6.3 httpd-2.4.33/srclib/apr
check
# 1)make httpd
cd httpd-2.4.33
./configure --prefix=/lamp/application/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre  --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork
make -j $cpus && make install
check
# 2)configure httpd
httpd_conf_file='/lamp/application/httpd24/conf/httpd.conf'
sed -i.bak -r '/ServerName www.example.com/s/#(.*)/1/' $httpd_conf_file
sed -i.bak -r '/^(User|Group)/s/(.* ).*/1apache/' $httpd_conf_file
sed -i.bak -r '/proxy_fcgi_module/s/#(.*)/1/' $httpd_conf_file
sed -i.bak -r '/proxy_module/s/#(.*)/1/' $httpd_conf_file
sed -i.bak -r 's/( *DirectoryIndex )index.html/1index.php index.html/' $httpd_conf_file
cat >> $httpd_conf_file  <<EOF
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
ProxyRequests Off
ProxyPassMatch  ^/(.*.php)$ fcgi://127.0.0.1:9000/lamp/application/httpd24/htdocs/$1
EOF
echo 'PATH=/lamp/application/httpd24/bin/:$PATH' > /etc/profile.d/httpd.sh
/lamp/application/httpd24/bin/apachectl start
check
echo "configure httpd is OK!"

# ============================ MariaDB-server ============================
# make and make install MariaDB-server, configure mysqld
# 1)make and install
dbdir='/lamp/data/mysqldb/'
insdir='/lamp/application/mysql/'
! id mysql &>/dev/null && useradd -r -s /sbin/nologin -d $dbdir mysql
mkdir -p $dbdir
chown -R mysql.mysql $dbdir
cd /root/src/
tar xf mariadb-10.2.15.tar.gz
cd mariadb-10.2.15/
cmake . -DCMAKE_INSTALL_PREFIX=$insdir -DMYSQL_DATADIR=$dbdir -DMYSQL_UNIX_ADDR=${dbdir}mysql.sock -DSYSCONFDIR=/etc -DMYSQL_USER=mysql -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1  -DWITHOUT_MROONGA_STORAGE_ENGINE=1 -DWITH_DEBUG=0 -DWITH_READLINE=1 -DWITH_SSL=system -DWITH_ZLIB=system -DWITH_LIBWRAP=0 -DENABLED_LOCAL_INFILE=1  -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci
check
make -j $cpus && make install
check
echo 'export PATH=/lamp/application/mysql/bin:$PATH' >/etc/profile.d/mysql.sh
# 2)configure mysql
cd $insdir
scripts/mysql_install_db --datadir=$dbdir --user=mysql --basedir=$insdir
check
cp -f support-files/my-huge.cnf /etc/my.cnf
cp -f support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
service mysqld start
check
${insdir}bin/mysql -e "DELETE FROM mysql.user WHERE user=''"
check

# ============================ PHP ============================
# make and make install PHP, configure php.ini
# 1)make and install
cd /root/src/
tar xf php-7.1.18.tar.gz
cd php-7.1.18/
./configure --prefix=/lamp/application/php7 --enable-mysqlnd --with-mysqli=mysqlnd --with-openssl --with-pdo-mysql=mysqlnd --enable-mbstring  --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --enable-fpm  --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --enable-maintainer-zts --disable-fileinfo
check
make -j $cpus && make install
check
# 2)configure php
cp php.ini-production /etc/php.ini
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
chkconfig --add php-fpm
chkconfig php-fpm on
cd /lamp/application/php7/
cp etc/php-fpm.conf.default etc/php-fpm.conf
cp etc/php-fpm.d/www.conf.default etc/php-fpm.d/www.conf
sed -i.bak -r '/^(user|group)/s/(.* ).*/1apache/' etc/php-fpm.d/www.conf
echo 'PATH=/lamp/application/php7/bin:/lamp/application/php7/sbin:$PATH' > /etc/profile.d/php-fpm.sh
service php-fpm start
check
ss -tnl |grep -q 9000 && echo "install PHP complete!"

# test lamp
cat >> /lamp/application/httpd24/htdocs/test.php <<EOF
<?php
$dsn='mysql:host=127.0.0.1;dbname=mysql';
$username='root';
$passwd='';
$dbh=new PDO($dsn,$username,$passwd);
var_dump($dbh);
?>
EOF
curl 127.0.0.1/test.php |grep -q PDO && echo -e "33[32mLAMP Test Completed, Is Working!33[0m" || echo -e "33[31mSorry, Test failed, Please check!33[0m"

以上是关于LAMP环境搭建详解的主要内容,如果未能解决你的问题,请参考以下文章

lamp源码搭建详解及wordpress搭建

LAMP 系统服务搭建过程详解

LAMP 平台搭建详解

LAMP两种模式

图文详解阿里云下lamp+discuz搭建实例

图文详解阿里云下lamp+discuz搭建实例