LAMP(apache/httpd+mysql+php)环境/架构 一键安装脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LAMP(apache/httpd+mysql+php)环境/架构 一键安装脚本相关的知识,希望对你有一定的参考价值。

          该脚本只在centos 上测试过,测试OK,其他发行版自行测试


#!/bin/bash


#Purpose        :一键安装lamp(apache + mysql + php)

#Writer                :www.linuser.com

#Date                :2015-10-08


#定义软件下载目录:

SoftDir=‘/usr/local/src‘


#定义编译函数:

function MAKE() {

        if [ `echo $?` -eq 0 ];then

                        make 

                else

                        exit

        fi

}


#定义安装函数:

function MAKE_INS() {

        if [ `echo $?` -eq 0 ];then

                        make install 

                else

                        exit

        fi

}


#第一部分:系统基础环境的配置:


#判断当前用户是否为root:

if [ `whoami` = "root" ];then 

                echo "root"

        else 

                echo "Please use root"

                exit

fi


#判断当前网络是否畅通:

NET=`ping -c 4 www.baidu.com |grep received |awk ‘{print $6}‘|sed -e ‘s/%//‘`

if [ $NET -ge 2 ];then

        echo "Please check your network"

        exit

fi


#配置PS 环境:

if [ -z `egrep -i ps1 /etc/profile |awk ‘{print $1}‘` ];then

        echo ‘PS1="\[\e[33;1m\][\[email protected]\[\e[31;1m\]\h \t \# \w]\\$\[\e[m\]"‘ >> /etc/profile 

                source /etc/profile

fi


#查看系统编码是否为en_US.UTF-8

lang=`grep -i lang /etc/sysconfig/i18n |awk -F ‘"‘ ‘{print $2}‘`

if [ $lang != ‘en_US.UTF-8‘ ];then

        sed -i "s/$lang/en_US.UTF-8/" i18n

fi


#做本机的hosts 解析:

ETH=`egrep -i device /etc/sysconfig/network-scripts/ifcfg-eth0 |awk -F ‘=‘ ‘{print $2}‘`

if  [  -z `egrep -i $(echo $HOSTNAME) /etc/hosts |awk ‘{print $2}‘` ];then

        echo $(ip ad |grep "global $ETH" |awk -F "/" ‘{print $1}‘ |awk ‘{print $2}‘  && echo $HOSTNAME) >> /etc/hosts

fi

#关闭selinux:

sed -i ‘s/SELINUX=enforcing/SELINUX=disabled/‘ /etc/selinux/config


#关闭反向解析:

sed -i ‘s/^#UseDNS yes/UseDNS no/‘ /etc/selinux/config


#重启启动sshd 服务:

service sshd restart


#系统调优,将系统文件数和进程数调高至65535

cat <<EOF >> /etc/security/limits.conf

*                soft    nproc           65535

*                soft    nproc           65535

*                soft    nofile          65535

*                hard    nofile          65535

EOF

#或者使用sed 插入到指定的行

#sed -i "49i*                soft    nproc           65535\n*                soft    nproc           65535\n*                soft    nofile          65535\n*                hard    nofile          65535" /etc/security/limits.conf


sed -i ‘s/^root/#root/‘ /etc/security/limits.d/90-nproc.conf 

sed -i ‘s/1024/65535/‘ /etc/security/limits.d/90-nproc.conf 

sed -i "6i*          hard    nporc     65535" /etc/security/limits.d/90-nproc.conf 


#安装系统扩展源:http://dl.fedoraproject.org/pub/epel

if [ `awk ‘{print $3}‘ /etc/redhat-release |awk -F ‘.‘ ‘{print $1}‘` -eq 6 ];then

                rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm 

        elif [ `awk ‘{print $4}‘ /etc/redhat-release |awk -F ‘.‘ ‘{print $1}‘` -eq 7 ];then

                rpm -ivh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm 

fi


#安装系统运维包:

yum install -y lrzsz gcc gcc-c++  perl perl-devel bzip2 bzip2-devel make man vim tree unzip wget lua-devel lua-static patch libxml2 libxml2-devel libxslt libxslt-devel gd gd-devel ntp screen sysstat rsync lsof  gettext  gettext-devel autoconf automake libtool git openssl openssl-devel curl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel perl-Data-Dumper


#升级系统:

yum update -y 


#第二部分:安装LAMP


#================================mysql 的安装配置#================================

cd $SoftDir && wget -q http://www.linuser.com/share/soft/mysql-5.6.26-linux-glibc2.5-x86_64.tar.gz


#解压:

tar zxf mysql-5.6.26-linux-glibc2.5-x86_64.tar.gz


#移动解压目录病重命名Mysql:

mv mysql-5.6.26-linux-glibc2.5-x86_64 /usr/local/mysql


#添加mysql 系统用户:

if [ -z `grep mysql /etc/passwd` ] ;then

        useradd -s /sbin/nologin mysql

fi


#创建mysql 初始化数据目录:

if [ ! -d "/data/mysql" ] ;then

        mkdir -p /data/mysql

fi


#将mysql 初始化目录所属主和组修改为mysql:

chown -R mysql.mysql /data/mysql/


#初始化

cd $MysqlS && ./scripts/mysql_install_db --datadir=/data/mysql --user=mysql 


#备份系统默认的my.cnf 文件:

mv /etc/my.cnf{,.bak}


#复制mysql 的 主配置文件到etc 目录下病重命名为my.cnf

 \cp support-files/my-default.cnf /etc/my.cnf


#复制mysql 的启动脚本到/etc/init.d 目录下:

 \cp support-files/mysql.server /etc/init.d/mysqld


#赋予启动755 权限:

chmod 755 /etc/init.d/mysqld


#修改my.cnf 文件配置如下:

cat << EOF > /etc/my.cnf

[mysqld]

pid-file=/data/mysql/mysql.pid

log-error=/var/log/mysql.log

datadir = /data/mysql

basedir = /usr/local/mysql

character-set-server=utf8

port = 3306

socket = /tmp/mysql.sock

key_buffer_size = 256M

max_allowed_packet = 1M

table_open_cache = 256

sort_buffer_size = 1M

read_buffer_size = 1M

read_rnd_buffer_size = 4M

myisam_sort_buffer_size = 64M

thread_cache_size = 8

query_cache_size = 16M

thread_concurrency = 8

binlog_format=mixed

server-id = 1

slow-query-log = 1

slow-query-log-file = /data/mysql/mysql-slow.log

log-bin = mysql-bin

log-bin-index = mysql-bin.index

symbolic-links = 0

skip-name-resolve


[client]

port = 3306

socket = /tmp/mysql.sock

default-character-set=utf8


[mysqldump]

quick

max_allowed_packet = 16M


#[mysqld_safe]


[mysql]

no-auto-rehash


[myisamchk]

key_buffer_size = 128M

sort_buffer_size = 128M

read_buffer = 2M

write_buffer = 2M


[mysqlhotcopy]

interactive-timeout

EOF


#启动mysql:

service mysqld start


if [ `echo $?` -eq 0 ] ;then

        echo "Mysql started"

else

        exit

fi


#将Mysqld 设置为随机启动:

chkconfig --add mysqld && chkconfig mysqld on


#================================apr 、apr-util 、libmcrypt 及pcre ================================


cd $SoftDir


wget -q http://www.linuser.com/share/soft/apr-1.5.2.tar.gz


#解压:

tar zxf apr-1.5.2.tar.gz


#进入解压目录:

cd apr-1.5.2 && ./configure --prefix=/usr/local/apr


#编译及安装

MAKE && MAKE_INS


cd $SoftDir


wget -q http://www.linuser.com/share/soft/apr-util-1.5.4.tar.gz


tar zxf apr-util-1.5.4.tar.gz


cd ./apr-util-1.5.4  && ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr


#编译及安装

MAKE && MAKE_INS


cd $SoftDir 


wget -q http://www.linuser.com/share/soft/libmcrypt-2.5.7.tar.gz


tar zxf libmcrypt-2.5.7.tar.gz


cd libmcrypt-2.5.7 && ./configure --prefix=/usr/local/libmcrypt 


#编译及安装

MAKE && MAKE_INS


cd $SoftDir


wget -q http://www.linuser.com/share/soft/pcre-8.37.tar.gz


tar zxf pcre-8.37.tar.gz


cd pcre-8.37  && ./configure --prefix=/usr/local/pcre 


#编译及安装

MAKE && MAKE_INS


cd $SoftDir


wget -q http://www.linuser.com/share/soft/zlib-1.2.8.tar.gz


tar zxf zlib-1.2.8.tar.gz


cd zlib-1.2.8 && ./configure --prefix=/usr/local/zlib


#编译及安装:

MAKE && MAKE_INS


#================================apache 的安装配置================================

cd $SoftDir && wget -q http://www.linuser.com/share/soft/httpd-2.4.16.tar.gz


#解压apache:

tar zxf httpd-2.4.16.tar.gz


#进入解压目录:

cd httpd-2.4.16


#配置编译参数:

./configure  --prefix=/usr/local/apache2 -enable-so --enable-mods-shared=all --enable-deflate=shared --enable-expires=shared --enable-rewrite=shared --with-pcre=/usr/local/pcre/ --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-z=/usr/local/zlib 


#编译简装:

MAKE && MAKE_INS


#================================php 安装配置================================

cd $SoftDir && wget -q http://www.linuser.com/share/soft/php-5.5.30.tar.gz


#解压:

tar -zxf php-5.5.30.tar.gz


cd php-5.5.30 && ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs  --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/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=/usr/local/libmcrypt  --enable-soap  --enable-gd-native-ttf  --enable-mbstring --enable-sockets  --enable-exif  --disable-ipv6 --with-pear   --with-curl --with-openssl --with-mysqli --enable-mysqlnd --with-gettext  --enable-bcmath --enable-sockets 


#编译及安装

MAKE && MAKE_INS 


sed -i ‘s/DirectoryIndex index.html/DirectoryIndex index.html index.php/‘ /usr/local/apache2/conf/httpd.conf

sed -i ‘s/^#ServerName www.example.com:80/ServerName localhost:80/‘ /usr/local/apache2/conf/httpd.conf

sed -i "392i AddType application/x-httpd-php .php" /usr/local/apache2/conf/httpd.conf


#开启防火墙的80端口

iptables -I INPUT -p tcp --dport 80 -j ACCEPT && service iptables save


#测试apache 配置是否有误

/usr/local/apache2/bin/apachectl -t

if [ `echo $?` -ne 0 ];then

                exit

fi

#================================xcache 安装配置================================

cd $SoftDir && wget -q http://www.linuser.com/share/soft/xcache-3.2.0.tar.gz


#解压:

tar zxf xcache-3.2.0.tar.gz 


#进入解压目录,清除phpize

cd xcache-3.2.0 && /usr/local/php/bin/phpize --clean


#执行phpize,生产xcache 配置文件:

/usr/local/php/bin/phpize 


#配置编译参数:

./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config 


#编译及安装

MAKE && MAKE_INS


#复制PHP源码包中的php.ini-production 到 /usr/local/php/etc 目录下并重命名为php.ini

 \cp /usr/local/src/php-5.5.30/php.ini-production /usr/local/php/etc/php.ini


#将xcache 库文件加入到php.ini 文件中

echo "extemsiobn_dir=‘/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/‘" >> /usr/local/php/etc/php.ini

 

echo "extension=xcache.so" >> /usr/local/php/etc/php.ini


#启动Apache:

/usr/local/apache2/bin/apachectl -k restart


#重命名apache 默认的首页index.html:

mv /usr/local/apache2/htdocs/index.html{,.bak}


#新建php 信息页面,测试apache 解析php:

echo "<?php   echo phpinfo();  ?>" > /usr/local/apache2/htdocs/index.php


#将apache 加入到系统环境变量中:

echo "PATH=$PAHT:/usr/local/apache2/bin" >> /etc/profile

source /etc/profile


脚本执行:

  time sh /usr/local/sbin/lamp.sh >/var/log/install.log 2>&1

本文出自 “linux系统学习扩展” 博客,请务必保留此出处http://zhangxiaoxiong.blog.51cto.com/11657691/1786187

以上是关于LAMP(apache/httpd+mysql+php)环境/架构 一键安装脚本的主要内容,如果未能解决你的问题,请参考以下文章

LAMP架构介绍;MySQL安装(上中下)

LAMP 环境搭建实例

LAMP架构(LAMP介绍,mysql/mariaDB介绍,Mysql安装)

LAMP架构(LAMP介绍,mysql/mariaDB介绍,Mysql安装)

lamp介绍,mysql安装

基于LAMP的架构实现