一键安装LNMP脚本

lnmp脚本:

#!/bin/bash

#********************************************************************
# File Name: lnmp.sh
# Version: V1.0
# Author: dahuangji
# Email: 
# Created Time : 2022-01-28 14:55:17
# Description:
#********************************************************************

. /etc/init.d/functions &>/dev/null
set -eo pipefail

RED='\E[31;2m'
GREEN='\E[32;1m'
BLUE='\E[34;1m'
END='\E[0m'

ngx_vs=nginx-1.18.0.tar.gz
mysql_vs=mariadb-10.5.13-linux-systemd-x86_64.tar.gz
php_vs=php-7.4.27.tar.gz

down() {
  m_name=$(echo $mysql_vs|sed -nr 's/(.*)-lin.*/\1/p')
  cd /opt/
  wget http://nginx.org/download/${ngx_vs}
  wget https://mirrors.aliyun.com/mariadb/${m_name}/bintar-linux-systemd-x86_64/${mysql_vs}
  wget --no-check-certificate https://www.php.net/distributions/${php_vs}
}

install_ngx() {
  n_name=$(echo  $ngx_vs|sed -nr 's/(.*).ta.*/\1/p')
  cd /opt/
	if id nginx ;then 
		echo -e "$GREEN 已有nginx用户 $END"
	else
		useradd -r nginx
	fi
  yum install -y pcre-devel openssl-devel zlib-devel gd-devel
  tar xf  $ngx_vs -C /opt/
  cd $n_name
  ./configure --prefix=/opt/nginx \
    --user=nginx --group=nginx \
    --with-http_ssl_module \
    --with-http_v2_module \
    --with-http_stub_status_module \
    --with-threads \
    --with-file-aio \
    --with-http_gzip_static_module \
    --with-pcre --with-stream \
    --with-stream_realip_module \
    --with-stream_ssl_module \
    --with-http_image_filter_module=dynamic \
    --with-http_image_filter_module \
    --with-http_realip_module
  make && make install && echo "nginx is install"
  echo 'export PATH=/opt/nginx/sbin/:$PATH' > /etc/profile.d/nginx.sh ;chmod +x /etc/profile.d/nginx.sh
	/etc/profile.d/nginx.sh
}

install_mariadb() {
  m_name=$(echo $mysql_vs|sed -nr 's/(.*).ta.*/\1/p')
  cd /opt/
  yum install -y ncurses*
  tar xf $mysql_vs -C /opt/
  ln -s ${m_name} mariadb
  mkdir -p /data/mariadb/run
  if id mariadb ;then
    chown -R mariadb.mariadb mariadb
    chown -R mariadb.mariadb /data/mariadb
  else 
    useradd -r mariadb
    chown -R mariadb.mariadb mariadb
    chown -R mariadb.mariadb /data/mariadb
  fi
  ./mariadb/scripts/mysql_install_db  --user=mariadb --datadir=/data/mariadb  --basedir=/opt/mariadb/
  echo 'export PATH=/opt/mariadb/bin/:$PATH' > /etc/profile.d/mariadb.sh ;chmod +x /etc/profile.d/mariadb.sh
	/etc/profile.d/mariadb.sh
	cat > /etc/my.cnf <<-EOF
[mysqld]
server-id=1
log-bin
port=3306
basedir=/opt/mariadb
datadir=/data/mariadb
socket=/data/mariadb/run/mysql.sock
log-error=/data/mariadb/run/mysql.log
pid-file=/data/mariadb/run/mysql.pid
skip-name-resolve=1

[client]
socket=/data/mariadb/run/mysql.sock
EOF
}

install_php() {
  p_name=$(echo $php_vs|sed -nr 's/(.*).ta.*/\1/p')
  cd /opt/
  yum install -y sqlite-devel bzip2-devel libpng-devel libjpeg-turbo-devel freetype-devel gettext-devel libxml2-devel libmcrypt-devel
  yum install -y http://mirror.centos.org/centos/8/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm 
  tar xf $php_vs -C /opt/ 
	cd $p_name
  ./configure --prefix=/opt/php  --with-openssl  --enable-mbstring --with-freetype --with-jpeg --with-zlib  --enable-xml --enable-sockets --enable-fpm  --with-config-file-path=/opt/php --with-config-file-scan-dir=/opt/php/ --with-bz2 --enable-gd --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-maintainer-zts --disable-fileinfo --enable-bcmath --with-gettext
	make && make install
  echo "export PATH=/opt/php/bin:$PATH" > /etc/profile.d/php.sh ; chmod +x /etc/profile.d/php.sh
	/etc/profile.d/php.sh
  cp php.ini-production /opt/php/etc/php.ini
  cd /opt/php/etc/
  mv php-fpm.conf.default php-fpm.conf
  mv php-fpm.d/www.conf.default php-fpm.d/www.conf
}

service_conf() {
  #nginx.service config
  cat > /etc/systemd/system/nginx.service <<-EOF
[Unit]
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
ExecStart=/opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /opt/nginx/logs/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /opt/nginx/logs/nginx.pid)"

[Install]
WantedBy=multi-user.target
EOF

  #mariadb.service config
  m_name=$(echo $mysql_vs|sed -nr 's/(.*).ta.*/\1/p')
  cat > /etc/systemd/system/mariadb.service <<-EOF
[Unit]
After=network.target

[Install]
WantedBy=multi-user.target

[Service]
Type=notify
ExecStart=/opt/mariadb/bin/mariadbd --defaults-file=/etc/my.cnf
KillSignal=SIGTERM
SendSIGKILL=no
Restart=on-abort
RestartSec=5s
TimeoutStopSec=900
User=mariadb
Group=mariadb
LimitNOFILE=65535
EOF

  #php.service config
  cp  /opt/$p_name/sapi/fpm/php-fpm.service /etc/systemd/system/php-fpm.service
  sed -ri '/ExecStart/s#(.*e) .*#\1 -c /opt/php/etc/php.ini -y /opt/php/etc/php-fpm.conf#' /etc/systemd/system/php-fpm.service
  systemctl daemon-reload
  systemctl enable --now nginx mariadb php-fpm
}
rm_file() {
  n_name=$(echo  $ngx_vs|sed -nr 's/(.*).ta.*/\1/p')
  p_name=$(echo $php_vs|sed -nr 's/(.*).ta.*/\1/p')
  for i in $ngx_vs $mysql_vs $php_vs $n_name $p_name ;do
    find /opt/ -iname $i -exec rm -rf {} \;
  done
}

down
install_ngx
install_mariadb
install_php
service_conf
rm_file
posted @ 2022-01-28 19:58  suyanhj  阅读(122)  评论(0)    收藏  举报