实现LAMP
实现LAMP
1.LAMP工作原理
LAMP
是一个强大的Web应用程序平台,其中L是指linux
系统;A是指apache
也就是http;M一般是mysql/mariadb
数据库;P一般是php, perl, python
其中之一。
工作过程:
1》当客户端请求的是静态资源时,web服务器会直接把静态资源返回客户端;
2》当客户端请求的是动态资源时,httpd的php模块会进行相应的动态资源运算,如果此过程还需要数据库的数据作为运算参数时,php会连接mysql取得数据然后进行运算,运算的结果转为静态资源并由web服务器返回到客户端。
2.使用yum安装实现LAMP
1)实现环境条件
1》需要两天主机,一台实现LAP,另一台实现M.
2》在两台主机上使用iptables -F
关闭防火墙,使用setenforce 0
来暂时设置selinux为permisive,或者配置/etc/selinux/config来设置selinux为disable或permisive。
2)所需安装的程序包
centos6需要:httpd、php、php-mysql、mysql、mysql-server、mysql-libs
centos7需要:httpd、php-fpm(or php)、php-mysql、mariadb、mariadb-server、mariadb-libs
3)centos6和7上实现
1>安装httpd并开启
centos6上配置为:
yum install httpd
chkconfig httpd on
chkconfig --list httpd
service httpd start
service httpd status
centos7上配置为:
yum install httpd
systemctl enable httpd
systemctl start httpd
systemctl status httpd
2>安装mysql并开启
centos6上配置为:
yum install mysql mysql-server mysql-libs
chkconfig mysqld on
chkconfig --list mysqld
service mysqld start
service mysqld status
/usr/bin/mysql_secure_installation
mysql -uroot -pxm1234
mysql>create user "shenxm"@'%' identified by 'xm1234';
centos7上配置为:
yum install mariadb mariadb-libs mariadb-server
systemctl enable mariadb
systemctl start mariadb
systemctl status mariadb
/usr/bin/mysql_secure_installation
mysql -uroot -pxm1234
MariaDB [(none)]>create user "shenxm"@'%' identified by 'xm1234';
3>安装php相关包
centos6上配置为:
yum install php php-mysql
vim /etc/httpd/conf/httpd.conf
在文件尾部加两行
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
修改或添加下面内容
<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>
service httpd restart
centos7上配置为:
下面模式二选一
1)modules:
yum install php php-mysql
vim /etc/httpd/conf/httpd.conf
在文件尾部加两行
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
添加修改下面行
<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>
systemctl restart httpd
2)fastcgi:
yum install php-fpm php-mysql
vim /etc/httpd/conf/httpd.conf
在文件尾部加四行
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/app/httpd24/htdocs/$1
修改下面行
<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>
systemctl restart httpd
systemctl start php-fpm
4>测试配置的LAMP是否成功
vim /var/www/html/index.php
<html><body><h1>It works!</h1></body></html>
<?php
echo date("Y/m/d h:i:s");
$mysqli=new mysqli("192.168.213.252","wpuser","xm1234");
if(mysqli_connect_errno()){
echo "连接数据库失败!";
$mysqli=null;
exit;
}
echo "连接数据库成功!";
$mysqli->close();
phpinfo();
?>
3.脚本实现源码编译安装LAMP
这是将编译安装实现LAMP的操作写成脚本,通过脚本来实现快速实现。
1)实现环境条件
1》因为这是一个脚本,所以用一台主机来直接实现LAMPM。当然,也可以把脚本分成几个模块,就如同分为centos6和7一样,可以把每个功能分别独立设立为模块,然后需要那个就调用那个应用来配合完成,这样修改或排错的时候更清晰。
2》在主机上使用iptables -F
关闭防火墙,使用setenforce 0
来暂时设置selinux为permisive,或者配置/etc/selinux/config来设置selinux为disable或permisive。
2)所需的软件包
软件包的版本可以根据需要来进行选择。且需要先创建个/app的目录,把所有软件包都放到这个目录下。
centos7
:
apr-1.6.2.tar.gz
httpd-2.4.27.tar.bz2
php-7.1.10.tar.xz
apr-util-1.6.0.tar.gz
mariadb-10.2.8-linux-x86_64.tar.gz
centos6
:
apr-1.6.2.tar.gz
httpd-2.4.27.tar.bz2
php-5.6.31.tar.xz
xcache-3.2.0.tar.bz2
apr-util-1.6.0.tar.gz
mariadb-5.5.57-linux-x86_64.tar.gz
3)编写脚本
#!/bin/bash
#-------------------------------------------------
#Filename:lamp-script.sh
#Description:
#Revision:2.0
#Date:
#-------------------------------------------------
creat_lamp_on_centos6 ()
{
####1.编译httpd2.4
yum groupinstall "development tools" -y
yum install openssl-devel pcre-devel expat-devel -y
cd /app
tar xvf apr-1.6.2.tar.gz
tar xvf apr-util-1.6.0.tar.gz
tar xvf httpd-2.4.27.tar.bz2
cp -r /app/apr-1.6.2 /app/httpd-2.4.27/srclib/apr
cp -r /app/apr-util-1.6.0 /app/httpd-2.4.27/srclib/apr-util
cd httpd-2.4.27/
./configure --prefix=/app/httpd24 --enable-so --enable-ssl --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork
make && make install
cat > /etc/profile.d/lamp.sh <<eof
PATH=/app/httpd24/bin/:$PATH
eof
bash /etc/profile.d/lamp.sh
yum install httpd -y
cp /etc/rc.d/init.d/httpd /etc/init.d/httpd24
sed -i 's@apachectl=/usr/sbin/apachectl@apachectl=/app/httpd24/bin/apachectl@ ;s@httpd=${HTTPD-/usr/sbin/httpd}@httpd=${HTTPD-/app/httpd24/bin/httpd}@ ; s@pidfile=${PIDFILE-/var/run/httpd/httpd.pid}@pidfile=${PIDFILE-/app/httpd24/logs/httpd.pid}@ ; s@lockfile=${LOCKFILE-/var/lock/subsys/httpd}@lockfile=${LOCKFILE-/var/lock/subsys/httpd24}@' /etc/init.d/httpd
chkconfig --add httpd24
chkconfig httpd24 on
service httpd24 start
####2.二进制安装mariadb
####可以在另台主机上进行安装,这里是在本地安装
cd /app
tar xvf /app/mariadb-5.5.57-linux-x86_64.tar.gz -C /usr/local/
cd /usr/local/
ln -s mariadb-5.5.57-linux-x86_64/ mysql
useradd -r -m -d /app/mysqldb -s /sbin/nologin mysql
cd mysql/
./scripts/mysql_install_db --datadir=/app/mysqldb --user=mysql
mkdir /etc/mysql
cp support-files/my-large.cnf /etc/mysql/my.cnf
sed -i '27a\datadir = /app/mysqldb \ninnodb_file_per_table = ON \nskip_name_resolve = ON' /etc/mysql/my.cnf
cp support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
touch /var/log/mysqld.log
chown mysql /var/log/mysqld.log
service mysqld start
cat >/etc/profile.d/lamp.sh <<end
PATH=/app/httpd24/bin/:/usr/local/mysql/bin/:$PATH
end
bash /etc/profile.d/lamp.sh
/usr/local/mysql/bin/mysqladmin -u root password 'xm1234'
cat > /app/mysql-file.txt <<eof
create database wpdb;
grant all on wpdb.* to shenxm@'%' identified by 'xm1234';
eof
mysql -uroot -pxm1234< /app/mysql-file.txt
###3.源码编译php
yum install libxml2-devel bzip2-devel libmcrypt-devel -y #(epel源)
cd /app
tar xvf php-5.6.31.tar.xz
cd /app/php-5.6.31
./configure --prefix=/app/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --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
make && make install
cat >/etc/profile.d/lamp.sh <<END
PATH=/app/php/bin:/app/httpd24/bin/:/usr/local/mysql/bin/:$PATH
END
bash /etc/profile.d/lamp.sh
cp php.ini-production /etc/php.ini
sed -i '255s@DirectoryIndex index.html@DirectoryIndex index.php index.html@' /app/httpd24/conf/httpd.conf
cat >>/app/httpd24/conf/httpd.conf <<DEF
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
DEF
service httpd24 restart
###4.测试
cat > /app/httpd24/htdocs/index.php <<EDD
<html><body><h1>It works!</h1></body></html>
<?php
echo date("Y/m/d h:i:s");
\$mysqli=new mysqli("localhost","shenxm","xm1234");
if(mysqli_connect_errno()){
echo "连接数据库失败!";
\$mysqli=null;
exit;
}
echo "连接数据库成功!";
\$mysqli->close();
\$conn = mysql_connect('localhost','root','centos');
if ($conn)
echo "OK";
else
echo "Failure";
mysql_close();
phpinfo();
?>
EDD
}
creat_lamp_on_centos7 ()
{
####1.源码编译安装Httpd2.4
yum groupinstall "development tools" -y
yum install openssl-devel expat-devel pcre-devel -y
cd /app
tar xvf apr-1.6.2.tar.gz
tar xvf apr-util-1.6.0.tar.gz
tar xvf httpd-2.4.27.tar.bz2
cp -r apr-1.6.2 httpd-2.4.27/srclib/apr
cp -r apr-util-1.6.0 httpd-2.4.27/srclib/apr-util
cd httpd-2.4.27/
./configure --prefix=/app/httpd24 --sysconfdir=/etc/httpd24 --enable-so --enable-ssl --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork
make && make install
cat > /etc/profile.d/lamp.sh <<END
PATH=/app/httpd24/bin/:$PATH
END
bash /etc/profile.d/lamp.sh
apachectl
####2.二进制安装mariadb
####可以在另台主机上进行安装,这里是在本地安装
cd /app
tar xvf mariadb-10.2.8-linux-x86_64.tar.gz -C /usr/local/
cd /usr/local
ln -s mariadb-10.2.8-linux-x86_64/ mysql
useradd -r -m -d /app/mysqldb -s /sbin/nologin mysql
cd mysql/
scripts/mysql_install_db --datadir=/app/mysqldb --user=mysql
mkdir /etc/mysql
cp support-files/my-large.cnf /etc/mysql/my.cnf
sed -i '27a\datadir = /app/mysqldb \ninnodb_file_per_table = ON \nskip_name_resolve = ON' /etc/mysql/my.cnf
cp support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
mkdir /var/log/mariadb
chown mysql /var/log/mariadb/
systemctl start mysqld
cat > /etc/profile.d/lamp.sh <<EOF
PATH=/app/httpd24/bin/:/usr/local/mysql/bin/:$PATH
EOF
bash /etc/profile.d/lamp.sh
/usr/local/mysql/bin/mysqladmin -u root password 'xm1234'
cat > /app/mysql-file.txt <<eof
create database wpdb;
grant all on wpdb.* to shenxm@'%' identified by 'xm1234';
eof
mysql -uroot -pxm1234< /app/mysql-file.txt
####3.源码编译安装Php
yum install libxml2-devel bzip2-devel libmcrypt-devel -y #(epel源)
cd /app
tar xvf php-7.1.10.tar.xz
cd /app/php-7.1.10/
./configure --prefix=/app/php --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 --with-apxs2=/app/httpd24/bin/apxs --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --enable-maintainer-zts --disable-fileinfo
make && make install
cp php.ini-production /etc/php.ini
cat >>/etc/httpd24/httpd.conf <<DEF
<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
DEF
apachectl restart
####4.测试
cat > /app/httpd24/htdocs/index.php <<DDF
<html><body><h1> LAMP</h1></body></html>
<?php
\$mysqli=new mysqli("localhost","shenxm","xm1234");
if(mysqli_connect_errno()){
echo "连接数据库失败!";
\$mysqli=null;
exit;
}
echo "连接数据库成功!";
\$mysqli->close();
phpinfo();
?>
DDF
}
a=`cat /etc/centos-release|egrep -o "[0-9]"|head -1`
if [ ${a} -eq 6 ] ;then
creat_lamp_on_centos6
elif [ ${a} -eq 7 ] ;then
creat_lamp_on_centos7
fi
unset a
上述若有那些错误,欢迎观看的各位提出,谢谢~