LAMP:
- L指的是Linux
- A是Apache
- M为Mysql或者Mariadb
- P为Php,Pyton或者perl
这里我们使用Linux,Apache,Mariadb和php来搭建。先来看下原理
![Uploading Paste_Image_676571.png . . .]LAMP:
- L指的是Linux
- A是Apache
- M为Mysql或者Mariadb
- P为Php,Pyton或者perl
这里我们使用Linux,Apache,Mariadb和php来搭建。先来看下原理:
开始为客户端通过http协议发起请求,这个请求可能是动态的,也可能是静态的。
web服务器通过发起请求的后缀来判断,如果是静态的资源就由web服务器自行处理,到磁盘上找到数据后构建相应报文,然后发送给客户端。
如果是动态这时web服务器会将请求转发给php。这里分为两种情况: - 一种为php以模块的方式集成在httpd中,httpd通过cgi协议和php模块连接。这中间的过程就是httpd服务针对每个有客户端请求生成一个进程,然后进程生成一个cgi子进程,子进程负责和后边的php模块的连接,php模块执行程序,如果需要用到数据,就会作为客户端通过mysql协议连接mysql数据库服务端,获取数据并处理,处理完成后的数据成为静态的资源,再通过httpd服务传给客户端浏览器。
- 另一种情况如果是php会安装一个名为php-fpm的单独的服务,httpd转发请求时服务就相当于是php-fpm的客户端,通过FastCgi协议和php-fpm服务端连接。之后的步骤就和上面一样了。
编译安装LAMP
这里使用第一种php模块的方式来实现编译安装LAMP
软件环境如下:
centos7.3
apr-1.5.2.tar.bz2
apr-util-1.5.4.tar.bz2
httpd-2.4.27.tar.bz2
mariadb-10.2.7-linux-x86_64.tar.gz
php-7.1.7.tar.bz2
mkdir /root/src
创建目录
下载环境需要的源码或者二进制包到/root/src目录下
httpd编译安装
- 安装依赖的工具包组
yum -y groupinstall "Development tools"
yum -y install pcre-devel openssl-devel
2.解包并将arp的内容放入httpd目录中
[root@localhost src]# cd /roo/src
[root@localhost src]# tar xf apr-1.5.2.tar.bz2
[root@localhost src]# tar xf apr-util-1.5.4.tar.bz2
[root@localhost src]# tar xf httpd-2.4.27.tar.bz2
[root@localhost src]# mv apr-1.5.2 httpd-2.4.27/srclib/apr
[root@localhost src]# mv apr-util-1.5.4 httpd-2.4.27/srclib/apr-util
3.编译安装httpd
[root@localhost httpd-2.4.27]# mkdir -p /app
[root@localhost httpd-2.4.27]# cd httpd-2.4.27/
[root@localhost httpd-2.4.27]# ./configure --prefix=/app/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
[root@localhost httpd-2.4.27]# make && make install
4.设置环境变量
[root@localhost httpd-2.4.27]# vim /etc/profile.d/app.sh
添加以下内容
export PATH=$PATH:/app/httpd24/bin/
[root@localhost httpd-2.4.27]# . /etc/profile.d/app.sh #执行设置
5.启动httpd
[root@localhost httpd-2.4.27]# apachectl
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
出现这种提示就证明启动成功了,可以使用ss -tnl
来查看80端口是否打开
二进制安装mariadb
1.先将系统自带的mariadb组件卸载,要不会冲突
[root@localhost httpd-2.4.27]# yum -y remove "mariadb*"
2.创建用户
useradd -r mysql -s /sbin/nologin -d /app/mysqldb -m
3.解包并设置路径
[root@localhost src]# cd /usr/src
[root@localhost src]# tar xf mariadb-10.2.7-linux-x86_64.tar.gz -C /usr/local/
[root@localhost src]# cd /usr/local/
[root@localhost local]# ln -s mariadb-10.2.7-linux-x86_64/ mysql
[root@localhost local]# chown root.mysql mysql/*
4.设置配置文件
[root@localhost local]# mkdir /etc/mysql
[root@localhost local]# cp /usr/local/mysql/support-files/my-huge.cnf /etc/mysql/my.cnf
[root@localhost local]# vim /etc/mysql/my.cnf
里边在[mysqld]下边增加这几行
datadir = /app/mysqldb
innodb_file_per_table = on
skip_name_resolve = on
5.初始化数据库
[root@localhost mysql]# cd /usr/local/mysql/
[root@localhost mysql]# /usr/local/mysql/scripts/mysql_install_db
[root@localhost mysql]# chown -R mysql.mysql /app/mysqldb
6.设置启动脚本
[root@localhost mysql]# cp -a support-files/mysql.server /etc/init.d/mysqld
[root@localhost mysql]# chkconfig --add mysqld
[root@localhost mysql]# chkconfig mysqld on
[root@localhost mysql]# service mysqld start
7.设置环境变量
[root@localhost mysql]# vim /etc/profile.d/app.sh
将内容修改为
export PATH=$PATH:/app/httpd24/bin/:/usr/local/mysql/bin
[root@localhost mysql]# . /etc/profile.d/app.sh
8.初始化用户管理
[root@localhost mysql]# cd /usr/local/mysql/
[root@localhost mysql]# mysql_secure_installation
[root@localhost mysql]# mysql -uroot -p
MariaDB [(none)]> create database blogdb;
MariaDB [(none)]> grant all on blogdb.* to wpuser@'192.168.8.%' identified by "magedu";
编译安装PHP
1.安装依赖软件
[root@localhost mysql]# yum -y install libxml2-devel bzip2-devel libmcrypt-devel
2.编译安装
[root@localhost mysql]# cd /root/src/
[root@localhost src]# tar xf php-7.1.7.tar.bz2
[root@localhost src]# cd php-7.1.7/
[root@localhost php-7.1.7]# ./configure --prefix=/app/php --enable-mysqlnd --with-mysqli=mysqlnd --with-openssl --enable-mbstring --with-png-dir --with-jpeg-dir --with-freetype-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --with-apxs2=/app/httpd24/bin/apxs --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2
[root@localhost php-7.1.7]# make && make install
3.修改配置文件
[root@localhost php-7.1.7]# cp php.ini-production /etc/php.ini
[root@localhost php-7.1.7]# vim /app/httpd24/conf/httpd.conf
进去后找到 AddType开头的两行,将下边内容追加到后边
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
找到 DirectoryIndex,将index.php写入
<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>
4.重启apache服务并测试
[root@localhost php-7.1.7]# vim /app/httpd24/htdocs/index.php
添加以下内容
<?php
$mysqli=new mysqli("127.0.0.1","root","magedu");
if(mysqli_connect_errno()){
echo "连接数据库失败!";
$mysqli=null;
exit;
}
echo "连接数据库成功!";
$mysqli->close();
?>
[root@localhost php-7.1.7]# apachectl restart
访问本机ip地址,出现连接数据库成功字样,表示实验成功
脚本
下面为一键编译安装脚本,使用之前需要先将对应的安装包下载到/root/src目录中(这个目录不存在,需要先创建),没有开发工具的也要装上,命令为`yum -y groupinstall "development tools",然后在执行脚本,执行完成后再浏览器输入本机ip,出现连接数据库成功的字样,就表示执行成功。
yum -y install pcre-devel openssl-devel > /dev/null
cd /root/src
tar xf apr-1.5.2.tar.bz2
tar xf apr-util-1.5.4.tar.bz2
tar xf httpd-2.4.27.tar.bz2
mv apr-1.5.2 httpd-2.4.27/srclib/apr
mv apr-util-1.5.4 httpd-2.4.27/srclib/apr-util
mkdir -p /app
cd httpd-2.4.27/
./configure --prefix=/app/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 &> /dev/null
if [ $? -ne 0 ];then
echo './configure 失败'
fi
make &> /dev/null
if [ $? -ne 0 ];then
echo 'make 失败'
fi
make install &> /dev/null
if [ $? -ne 0 ];then
echo 'make install 失败'
fi
echo 'export PATH=$PATH:/app/httpd24/bin/' > /etc/profile.d/app.sh
. /etc/profile.d/app.sh
apachectl &> /dev/null
#mariadb
yum -y remove "mariadb*" &> /dev/null
useradd -r mysql -s /sbin/nologin -d /app/mysqldb -m
cd /root/src
tar xf mariadb-10.2.7-linux-x86_64.tar.gz -C /usr/local/
cd /usr/local
ln -s mariadb-10.2.7-linux-x86_64/ mysql
chown -R root.mysql mysql/
mkdir /etc/mysql
cp /usr/local/mysql/support-files/my-huge.cnf /etc/mysql/my.cnf
sed -i '/\[mysqld\]/a datadir = /app/mysqldb\ninnodb_file_per_table = on\nskip_name_resolve = on' /etc/mysql/my.cnf
cd /usr/local/mysql/
/usr/local/mysql/scripts/mysql_install_db &> /dev/null
if [ $? -ne 0 ];then
echo "初始化数据库失败"
fi
chown -R mysql.mysql /app/mysqldb/
cp -a support-files/mysql.server /etc/init.d/mysqld
chkconfig mysqld on
service mysqld start &> /dev/null
if [ $? -ne 0 ];then
echo "mysql 启动失败"
fi
echo 'export PATH=$PATH:/app/httpd24/bin/:/usr/local/mysql/bin' > /etc/profile.d/app.sh
. /etc/profile.d/app.sh
#php
yum -y install libxml2-devel bzip2-devel libmcrypt-devel > /dev/null
#if [ $? -ne 0 ];then
#echo "安装php依赖工具包失败"
#fi
cd /root/src/
tar xf php-7.1.7.tar.bz2
cd php-7.1.7/
./configure --prefix=/app/php --enable-mysqlnd --with-mysqli=mysqlnd --with-openssl --enable-mbstring --with-png-dir --with-jpeg-dir --with-freetype-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --with-apxs2=/app/httpd24/bin/apxs --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 &> /dev/null
if [ $? -ne 0 ];then
echo './configure 失败'
fi
make &> /dev/null
if [ $? -ne 0 ];then
echo 'make 失败'
fi
make install &>/dev/null
if [ $? -ne 0 ];then
echo 'make install 失败'
fi
cp php.ini-production /etc/php.ini
sed -i 's/DirectoryIndex index.html/DirectoryIndex index.php index.html/' /app/httpd24/conf/httpd.conf
echo 'AddType application/x-httpd-php .php' >> /app/httpd24/conf/httpd.conf
echo 'AddType application/x-httpd-php-source .phps' >> /app/httpd24/conf/httpd.conf
cat > /app/httpd24/htdocs/index.php <<EOF
<?php
\$mysqli=new mysqli("127.0.0.1","root","");
if(mysqli_connect_errno()){
echo "连接数据库失败!";
\$mysqli=null;
exit;
}
echo "连接数据库成功!";
\$mysqli->close();
?>
EOF
apachectl restart
`