脚本安装lamp

脚本安装lamp

1. 提供软件包

[root@SYL7 lamp]# ls files/
apr-1.7.0.tar.gz       httpd-vhosts.conf
apr-util-1.6.1.tar.gz  mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
httpd-2.4.54.tar.gz    php-7.4.30.tar.gz
[root@SYL7 lamp]# pwd
/root/lamp
[root@SYL7 lamp]# 

2. 先编写Apache和mysql脚本

[root@SYL7 lamp]# cat install.sh 
#!/bin/bash

if [ $UID -ne 0 ];then
    echo "Execute the script as an administrator."
    exit
fi

#定义变量
read -p "请输入apache安装目录的绝对路径(default: /usr/local/apache): " apache_install_dir
read -p "请输入mysql安装目录的绝对路径(default: /usr/local/mysql): " mysql_install_dir
read -p "请输入数据存放目录的绝对路径(default: /opt/data): " datadir
read -p "请输入要为数据库设置的密码(default: 123456): " passwd

script_path=$(pwd)
port=3306

if [ -z $datadir ];then
    apache_install_dir=/usr/local/apache
fi

if [ -z $datadir ];then
    mysql_install_dir=/usr/local/mysql
fi

if [ -z $datadir ];then
    datadir=/opt/data
fi

if [ -z $passwd ];then
    passwd=123456
fi

#安装Apache
#创建用户
id apache &> /dev/null
if [ $? -ne 0 ];then
    useradd -r -M -s /sbin/nologin apache
else
    echo "用户已存在"
fi

#安装依赖包
echo -e "\033[32mInstalling dependency packages \033[\0m\033[5;32m......\033[0m"
yum -y groups mark install "Development Tools" 
sleep 2
yum -y install openssl-devel pcre-devel expat-devel libtool make gcc gcc-c++ ncurses-devel openssl cmake mariadb-devel ncurses-compat-libs &> /dev/null &&\
sleep 5

#解压软件包
rm -rf /usr/src/*
tar xf files/apr-1.7.0.tar.gz -C /usr/src
tar xf files/apr-util-1.6.1.tar.gz -C /usr/src
tar xf files/httpd-2.4.54.tar.gz -C /usr/src/

#编译安装apr
cd /usr/src/apr-1.7.0
if [ ! -d /usr/local/apr ];then
    sed -i '/$RM "$cfgfile"/d' configure
    ./configure --prefix=/usr/local/apr && \
    make && make install
else
    ls /usr/local
    echo "apr 编译安装完成"
    sleep 2
fi

#编译安装apr-util
cd ../apr-util-1.6.1/
if [ ! -d /usr/local/apr-util ];then
    ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && \
    make && make install
else
    ls /usr/local/
    echo "apr-util 编译安装完成"
    sleep 2
fi

#编译安装httpd
cd ../httpd-2.4.54/
if [ ! -d $apache_install_dir ];then
    ./configure --prefix=$apache_install_dir \
            --enable-so \
            --enable-ssl \
            --enable-cgi \
            --enable-rewrite \
            --with-zlib \
            --with-pcre \
            --with-apr=/usr/local/apr \
            --with-apr-util=/usr/local/apr-util/ \
            --enable-modules=most \
            --enable-mpms-shared=all \
            --with-mpm=prefork && \
    make && make install
else
    ls $apache_install_dir
    sleep 2
    echo "httpd 安装完成"
fi

#查看是否有环境变量,man文档,头文件并且设置
ls $apache_install_dir

echo "export PATH=$apache_install_dir/bin:\$PATH" > /etc/profile.d/apache.sh
ls /etc/profile.d/apache.sh

ln -s $apache_install_dir/include /usr/include/apache &> /dev/null

grep "$apache_install_dir/man" /etc/man_db.conf &> /dev/null
if [ $? -ne 0 ];then
    sed -i "/^MANDATORY.*share\/man/a MANDATORY_MANPATH\t\t\t$apache_install_dir/man" /etc/man_db.conf
fi

#编写service file
cat > /usr/lib/systemd/system/httpd.service <<EOF
[Unit]
Description=httpd server daemon
After=network.target

[Service]
Type=forking
ExecStart=$apache_install_dir/bin/apachectl start
ExecStop=$apache_install_dir/bin/apachectl stop
ExecReload=/bin/kill -HUP \$MAINPID

[Install]
WantedBy=multi-user.target
EOF

#加载文件
systemctl daemon-reload

#设置开机自启
systemctl enable --now httpd
ss -antl
echo -e "\033[32m Apache installation Complete \033[\0m"
sleep 5



#安装mysql
#创建mysql用户
id mysql &> /dev/null
if [ $? -ne 0 ];then
    useradd -r -M -s /sbin/nologin mysql
fi

export PATH=${mysql_install_dir}/bin:$PATH

#解压软件包,并修改目录及其属主
if [ ! -d $mysql_install_dir ];then
    echo "Decompressing software package..."
    tar xf ${script_path}/files/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local 
    sleep 2
    cd /usr/local
    mv mysql-5.7.38-linux-glibc2.12-x86_64 mysql
else
    echo "mysql 以修改."
fi

#修改mysql属主
chown -R mysql.mysql $mysql_install_dir

#设置环境变量
echo "export PATH=$mysql_install_dir/bin:\$PATH" > /etc/profile.d/mysql.sh

#头文件
ln -s $mysql_install_dir/include /usr/include/mysql &> /dev/null

#man文档
grep "$mysql_install_dir/man" /etc/man_db.conf &> /dev/null
if [ $? -ne 0 ];then
    sed -i "/^MANDATORY.*share\/man/a MANDATORY_MANPATH\t\t\t$mysql_install_dir/man" /etc/man_db.conf
fi

#lib
echo "$mysql_install_dir/lib" > /etc/ld.so.conf.d/mysql.conf
ldconfig

#判断目录存在
if [ ! -d $datadir ];then
    mkdir -p $datadir
fi
#修改目录属性
chown -R mysql.mysql $datadir
echo "init mysql ......"

#初始化mysql
${mysql_install_dir}/bin/mysqld --initialize --user mysql --datadir $datadir &> /tmp/mysqlpasswd

#生成配置mysql文件
cat > /etc/my.cnf <<EOF
[mysqld] 
basedir = $mysql_install_dir
datadir = $datadir
socket = /tmp/mysql.sock 
port = 3306 
pid-file = $datadir/mysql.pid 
user = mysql 
skip-name-resolve 
sql-mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
EOF

if [ ! -f /etc/init.d/mysqld ];then
    cp ${mysql_install_dir}/support-files/mysql.server /etc/init.d/mysqld
    sed -i "/^basedir=/c basedir=$mysql_install_dir" /etc/init.d/mysqld
    sed -i "/^datadir=/c datadir=$datadir" /etc/init.d/mysqld
fi

chmod +x /etc/init.d/mysqld
service mysqld start
chkconfig --add mysqld
sleep 2
ss -antl
password=$(grep 'password' /tmp/mysqlpasswd |awk '{print $NF}')
${mysql_install_dir}/bin/mysql -uroot -p$password --connect-expired-password -e "set password = password('$passwd');"
echo "你的密码为: $passwd"
sleep 5

3. 编写PHP脚本

#定义php变量
php_install_dir=/usr/local/php7

#安装PHP
if [ ! -d /usr/src/php-7.4.30 ];then
    echo "Decompressing software package..."
    tar xf ${script_path}/files/php-7.4.30.tar.gz -C /usr/src
else
    ls /usr/src/php-7.4.30
    sleep
    echo "php已解压"
fi

if [ ! -d php-7.4.30 ];then
    cd /usr/src/php-7.4.30
    ./configure --prefix=$php_install_dir  \
        --with-config-file-path=/etc \
        --enable-fpm \
        --enable-inline-optimization \
        --disable-debug \
        --disable-rpath \
        --enable-shared \
        --enable-soap \
        --with-openssl \
        --enable-bcmath \
        --with-iconv \
        --with-bz2 \
        --enable-calendar \
        --with-curl \
        --enable-exif  \
        --enable-ftp \
        --enable-gd \
        --with-jpeg \
        --with-zlib-dir \
        --with-freetype \
        --with-gettext \
        --enable-json \
        --enable-mbstring \
        --enable-pdo \
        --with-mysqli=mysqlnd \
        --with-pdo-mysql=mysqlnd \
        --with-readline \
        --enable-shmop \
        --enable-simplexml \
        --enable-sockets \
        --with-zip \
        --enable-mysqlnd-compression-support \
        --with-pear \
        --enable-pcntl \
        --enable-posix && \
    make && make install
else
    ls $php_install_dir
    sleep 2
    echo "PHP编译安装完成."
fi

#设置环境变量
echo "export PATH=${php_install_dir}/bin:${php_install_dir}/:sbin:\$PATH" > /etc/profile.d/php7.sh

#lib文件
echo "${php_install_dir}/lib" > /etc/ld.so.conf.d/php.conf
ldconfig
sleep 2

#头文件
ln -s ${php_install_dir}/include /usr/include/php &> /dev/null

#配置php-fpm
echo "$script_path"
\cp php.ini-production /etc/php.ini
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/rc.d/init.d/php-fpm
ls -l /etc/rc.d/init.d/
sleep 2
cd ${php_install_dir}/etc/
cp php-fpm.conf.default php-fpm.conf
cp php-fpm.d/www.conf.default php-fpm.d/www.conf
sleep 2

#启动PHP,并设置开机自启
service php-fpm start
chkconfig --add php-fpm
ss -antl
echo "PHP安装完成."
sleep 5

4. 配置lamp,并lamp脚本优化

[root@SYL7 lamp]# vim install.sh 
[root@SYL7 lamp]# cat install.sh 
#!/bin/bash

if [ $UID -ne 0 ];then
    echo "Execute the script as an administrator."
    exit
fi

#定义变量
read -p "请输入apache安装目录的绝对路径(default: /usr/local/apache): " apache_install_dir
read -p "请输入mysql安装目录的绝对路径(default: /usr/local/mysql): " mysql_install_dir
read -p "请输入数据存放目录的绝对路径(default: /opt/data): " datadir
read -p "请输入要为数据库设置的密码(default: 123456): " passwd

php_install_dir=/usr/local/php7



script_path=$(pwd)
port=3306

if [ -z $datadir ];then
    apache_install_dir=/usr/local/apache
fi

if [ -z $datadir ];then
    mysql_install_dir=/usr/local/mysql
fi

if [ -z $datadir ];then
    datadir=/opt/data
fi

if [ -z $passwd ];then
    passwd=123456
fi

#安装Apache
#创建用户
id apache &> /dev/null
if [ $? -ne 0 ];then
    useradd -r -M -s /sbin/nologin apache
else
    echo "用户已存在"
fi

#安装依赖包
echo -e "\033[32mInstalling dependency packages \033[\0m\033[5;32m......\033[0m"
yum -y groups mark install "Development Tools" &&\
yum -y install openssl-devel pcre-devel expat-devel libtool make gcc gcc-c++ ncurses-devel openssl cmake mariadb-devel ncurses-compat-libs http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm libxml2 libxml2-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php-mysqlnd sqlite-devel libzip-devel &> /dev/null &&\
sleep 5

#解压软件包
rm -rf /usr/src/*
tar xf files/apr-1.7.0.tar.gz -C /usr/src
tar xf files/apr-util-1.6.1.tar.gz -C /usr/src
tar xf files/httpd-2.4.54.tar.gz -C /usr/src/

#编译安装apr
cd /usr/src/apr-1.7.0
if [ ! -d /usr/local/apr ];then
    sed -i '/$RM "$cfgfile"/d' configure
    ./configure --prefix=/usr/local/apr && \
    make && make install
fi

#编译安装apr-util
cd ../apr-util-1.6.1/
if [ ! -d /usr/local/apr-util ];then
    ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && \
    make && make install
fi

#编译安装httpd
cd ../httpd-2.4.54/
if [ ! -d $apache_install_dir ];then
    ./configure --prefix=$apache_install_dir \
            --enable-so \
            --enable-ssl \
            --enable-cgi \
            --enable-rewrite \
            --with-zlib \
            --with-pcre \
            --with-apr=/usr/local/apr \
            --with-apr-util=/usr/local/apr-util/ \
            --enable-modules=most \
            --enable-mpms-shared=all \
            --with-mpm=prefork && \
    make && make install
fi

echo "export PATH=$apache_install_dir/bin:\$PATH" > /etc/profile.d/apache.sh

ln -s $apache_install_dir/include /usr/include/apache &> /dev/null

grep "$apache_install_dir/man" /etc/man_db.conf &> /dev/null
if [ $? -ne 0 ];then
    sed -i "/^MANDATORY.*share\/man/a MANDATORY_MANPATH\t\t\t$apache_install_dir/man" /etc/man_db.conf
fi

#编写service file
cat > /usr/lib/systemd/system/httpd.service <<EOF
[Unit]
Description=httpd server daemon
After=network.target

[Service]
Type=forking
ExecStart=$apache_install_dir/bin/apachectl start
ExecStop=$apache_install_dir/bin/apachectl stop
ExecReload=/bin/kill -HUP \$MAINPID

[Install]
WantedBy=multi-user.target
EOF

#加载文件
systemctl daemon-reload

#设置开机自启
systemctl enable --now httpd
ss -antl
echo -e "\033[32m Apache installation Complete \033[\0m"
sleep 5



#安装mysql
#创建mysql用户
id mysql &> /dev/null
if [ $? -ne 0 ];then
    useradd -r -M -s /sbin/nologin mysql
fi

export PATH=${mysql_install_dir}/bin:$PATH

#解压软件包,并修改目录及其属主
if [ ! -d $mysql_install_dir ];then
    echo "Decompressing software package..."
    tar xf ${script_path}/files/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local 
    sleep 2
    cd /usr/local
    mv mysql-5.7.38-linux-glibc2.12-x86_64 mysql
else
    echo "mysql 以修改."
fi

#修改mysql属主
chown -R mysql.mysql $mysql_install_dir

#设置环境变量
echo "export PATH=$mysql_install_dir/bin:\$PATH" > /etc/profile.d/mysql.sh

#头文件
ln -s $mysql_install_dir/include /usr/include/mysql &> /dev/null

#man文档
grep "$mysql_install_dir/man" /etc/man_db.conf &> /dev/null
if [ $? -ne 0 ];then
    sed -i "/^MANDATORY.*share\/man/a MANDATORY_MANPATH\t\t\t$mysql_install_dir/man" /etc/man_db.conf
fi

#lib
echo "$mysql_install_dir/lib" > /etc/ld.so.conf.d/mysql.conf
ldconfig

#判断目录存在
if [ ! -d $datadir ];then
    mkdir -p $datadir
fi
#修改目录属性
chown -R mysql.mysql $datadir
echo "init mysql ......"

#初始化mysql
${mysql_install_dir}/bin/mysqld --initialize --user mysql --datadir $datadir &> /tmp/mysqlpasswd

#生成配置mysql文件
cat > /etc/my.cnf <<EOF
[mysqld] 
basedir = $mysql_install_dir
datadir = $datadir
socket = /tmp/mysql.sock 
port = 3306 
pid-file = $datadir/mysql.pid 
user = mysql 
skip-name-resolve 
sql-mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
EOF

if [ ! -f /etc/init.d/mysqld ];then
    cp ${mysql_install_dir}/support-files/mysql.server /etc/init.d/mysqld
    sed -i "/^basedir=/c basedir=$mysql_install_dir" /etc/init.d/mysqld
    sed -i "/^datadir=/c datadir=$datadir" /etc/init.d/mysqld
fi

chmod +x /etc/init.d/mysqld
service mysqld start
chkconfig --add mysqld
sleep 2
ss -antl
password=$(grep 'password' /tmp/mysqlpasswd |awk '{print $NF}')
${mysql_install_dir}/bin/mysql -uroot -p$password --connect-expired-password -e "set password = password('$passwd');"
echo "你的密码为: $passwd"
sleep 5



#安装PHP
if [ ! -d /usr/src/php-7.4.30 ];then
    echo "Decompressing software package..."
    tar xf ${script_path}/files/php-7.4.30.tar.gz -C /usr/src
fi

cd /usr/src
if [ ! -d php-7.4.30 ];then
    cd php-7.4.30
    ./configure --prefix=$php_install_dir \
        --with-config-file-path=/etc \
        --enable-fpm \
        --enable-inline-optimization \
        --disable-debug \
        --disable-rpath \
        --enable-shared \
        --enable-soap \
        --with-openssl \
        --enable-bcmath \
        --with-iconv \
        --with-bz2 \
        --enable-calendar \
        --with-curl \
        --enable-exif  \
        --enable-ftp \
        --enable-gd \
        --with-jpeg \
        --with-zlib-dir \
        --with-freetype \
        --with-gettext \
        --enable-json \
        --enable-mbstring \
        --enable-pdo \
        --with-mysqli=mysqlnd \
        --with-pdo-mysql=mysqlnd \
        --with-readline \
        --enable-shmop \
        --enable-simplexml \
        --enable-sockets \
        --with-zip \
        --enable-mysqlnd-compression-support \
        --with-pear \
        --enable-pcntl \
        --enable-posix && \
    make && make install
fi

#设置环境变量
echo "export PATH=${php_install_dir}/bin:${php_install_dir}/:sbin:\$PATH" > /etc/profile.d/php7.sh

#lib文件
echo "${php_install_dir}/lib" > /etc/ld.so.conf.d/php.conf
ldconfig
sleep 2

#头文件
ln -s ${php_install_dir}/include /usr/include/php &> /dev/null

#配置php-fpm
echo "$script_path"
\cp php.ini-production /etc/php.ini
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/rc.d/init.d/php-fpm
ls -l /etc/rc.d/init.d/
cd ${php_install_dir}/etc/
cp php-fpm.conf.default php-fpm.conf
cp php-fpm.d/www.conf.default php-fpm.d/www.conf

#启动PHP,并设置开机自启
service php-fpm start
chkconfig --add php-fpm
ss -antl
echo "PHP安装完成."
sleep 5

#配置PHP界面
cat > ${apache_install_dir}/htdocs/index.php <<EOF
<?php
    phpinfo();
?>
EOF

#配置Apache
cat > ${apache_install_dir}/conf/extra/vhosts.conf <<EOF
<VirtualHost *:80>
    DocumentRoot "${apache_install_dir}/htdocs"
    ServerName www.example.com
    ErrorLog "logs/www.example.com-error_log"
    CustomLog "logs/www.example.com-access_log" common
    ProxyRequests Off
    ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000${apache_install_dir}/htdocs/\$1
    <Directory "${apache_install_dir}/htdocs">
        Options none
        AllowOverride none
        Require all granted
    </Directory>
</VirtualHost>
EOF

#配置Apache主配置文件
grep 'AddType application/x-httpd-php .php' ${apache_install_dir}/conf/httpd.conf &> /dev/null
if [ $? -ne 0 ];then
    sed -i '/\bAddType.*.gz .tgz/a \    AddType application/x-httpd-php .php' ${apache_install_dir}/conf/httpd.conf
fi

grep 'AddType application/x-httpd-php-source .phps' ${apache_install_dir}/conf/httpd.conf &> /dev/null
if [ $? -ne 0 ];then
    sed -i '/\bAddType.*.php/a \    AddType application/x-httpd-php-source .phps' ${apache_install_dir}/conf/httpd.conf
fi

sed -i '/index.html/c \    DirectoryIndex index.php index.html' ${apache_install_dir}/conf/httpd.conf

#启动Apache相关的模块
sed -i '/proxy_module/s/#//g' ${apache_install_dir}/conf/httpd.conf
sed -i '/proxy_fcgi_module/s/#//g' ${apache_install_dir}/conf/httpd.conf

#使httpd主配置文件包含vhosts文件
grep 'Include conf/extra/vhosts.conf' ${apache_install_dir}/conf/httpd.conf &> /dev/null
if [ $? -ne 0 ];then
    echo 'Include conf/extra/vhosts.conf' >> ${apache_install_dir}/conf/httpd.conf
fi

#设置权限
chown -R apache.apache $apache_install_dir
ls -l $apache_install_dir

#重启httpd,mysql和php
systemctl restart httpd
service mysqld restart
service php-fpm restart
sleep 2
ss -antl
[root@SYL7 lamp]# 

5. 验证

[root@SYL7 lamp]# bash
[root@SYL7 lamp]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.38 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> quit
Bye
[root@SYL7 lamp]# 

6. 查看

  • 在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值