Centos7一键编译安装LNMP脚本

#!/bin/bash
#具体版本号
NGINX_V=1.20.0
PHP_V=7.4.2
MYSQL_V=5.7.37
TMP_DIR=/tmp 
INSTALL_DIR=/usr/local
function install_nginx() {
#下载依赖
 yum -y install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel
 #安装配置
 #下载Nginx
 cd ${TMP_DIR}
 yum install -y wget && wget -c wget http://nginx.org/download/nginx-${NGINX_V}.tar.gz
 #解压源码
 tar -zxvf ${TMP_DIR}/nginx-${NGINX_V}.tar.gz
 mv nginx-${NGINX_V} nginx;cd nginx;
 #预编译配置
 ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module
 sleep 2s
 #编译安装
 make && make install
 #以服务启动
 cd /usr/lib/systemd/system;
 cat > nginx.service <<EOF
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
 
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target
EOF
systemctl restart firewalld;firewall-cmd --reload;
systemctl start nginx;systemctl enable nginx;
systemctl status nginx.service;
}
function install_php() {
 cd $TMP_DIR
 yum -y install gcc gcc-c++ make zlib zlib-devel pcre pcre-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers
 wget -c http://mirrors.sohu.com/php/php-${PHP_V}.tar.bz2 && 
 tar -jxvf php-${PHP_V}.tar.bz2 && cd php-${PHP_V} &&
 mv php-${PHP_V} $INSTALL_DIR/php
 cd $INSTALL_DIR/php;
 ./configure --prefix=$INSTALL_DIR/php --with-config-file-path=$INSTALL_DIR/php/etc
 sleep 1s;
 if [ $? -eq 0 ];then
 make ZEND_EXTRA_LIBS="-liconv" &&make install
 echo -e " 33[32m PHP服务安装成功! 33[0m"
 else
 echo -e " 33[32m PHP服务安装失败,请检测配置文件! 33[0m"
 exit
 fi
}
function install_mysql(){
 MYSQL_BASE=/usr/local/mysql
 cd $TMP_DIR
 file="mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz"
 if [ ! -f $file ]; then
 echo "File not found!"
 yum install -y wget && wget -c wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz;
 echo "下载完成,正在解压.......";
 tar -zxvf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
 mv mysql-5.7.37-linux-glibc2.12-x86_64 /usr/local/mysql
 cd /usr/local/mysql
 #exit 0
 fi
 echo "创建用户组"
 userdel mysql;
 groupadd mysql;
 useradd -r -g mysql mysql;
 mkdir -p /data/mysql;
 chown mysql:mysql -R /data/mysql;
cd /etc
echo "写入配置文件"
cat > my.cnf <<EOF
[mysqld]
bind-address=0.0.0.0 #绑定地址运行远程连接
port=3306 #Mysql开放的端口
user=mysql #数据库登录用户
basedir=/usr/local/mysql #Mysql安装的绝对路径
datadir=/data/mysql #Mysql数据存放的绝对路径
socket=/tmp/mysql.sock #套接字文件
log-error=/data/mysql/mysql.err #mysql生成的错误日志存放的路径
pid-file=/data/mysql/mysql.pid #为mysqld程序指定一个存放进程ID的文件
character_set_server=utf8mb4 #数据库字符编码
symbolic-links=0 #是否开启链接符号
explicit_defaults_for_timestamp=true #数据库timestamp类型的列自动更新
EOF
echo "初始化Mysql"
cd /usr/local/mysql/bin/
./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/data/mysql/ --user=mysql --initialize
sleep 2s
ehco "启动mysql"
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
service mysql start
service mysql status
ln -s /usr/local/mysql/bin/mysql /usr/bin
echo "获取mysql初始密码"
PASSWORD=`cat /data/mysql/mysql.err |grep "temporary password"|awk -F"root@localhost: " '{print $2}'`
echo "修改mysql密码"
$MYSQL_BASE/bin/mysql --socket=/tmp/mysql.sock --connect-expired-password -uroot -p${PASSWORD} -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'root@.com';"
$MYSQL_BASE/bin/mysql --socket=/tmp/mysql.sock -uroot -proot@.com -e "FLUSH PRIVILEGES;"
$MYSQL_BASE/bin/mysql --socket=/tmp/mysql.sock -uroot -proot@.com -e "USE mysql;"
$MYSQL_BASE/bin/mysql --socket=/tmp/mysql.sock -uroot -proot@.com -e "UPDATE user SET host = '%' WHERE user = 'root';"
$MYSQL_BASE/bin/mysql --socket=/tmp/mysql.sock -uroot -proot@.com -e "FLUSH PRIVILEGES;"
$MYSQL_BASE/bin/mysql --socket=/tmp/mysql.sock -uroot -proot@.com -e "exit;"
echo "重启数据库"
service mysql restart;
service mysql status;
echo "以服务项启动"
cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld;
chmod +x /etc/init.d/mysqld;
chkconfig --add mysqld;
chkconfig --list;
firewall-cmd --zone=public --add-port=3306/tcp --permanent;
firewall-cmd --reload;
firewall-cmd --list-all;
}
function install_info(){
 echo "=========> Nginx信息 <========="
 echo " Nginx : $NGINX_V "
 echo " 安装目录: /usr/local/ "
 
 echo "=========> MYSQL信息 <========="
 echo " 数据库版本 : 5.7.37 "
 echo " 数据库密码 : root@.com "
 echo " 数据库端口 : 3306 "
 echo " BASEDIR目录: /usr/local/mysql "
 echo " DATADIR目录: /data/mysql "
 echo "==========> PHP信息 <=========="
 echo " PHP版本 : $PHP_V "
 echo " 安装目录 : /usr/local/ "
}
function main(){
 install_nginx
 install_mysql
 install_php
 install_info
}
main
作者:KentBryce原文地址:https://segmentfault.com/a/1190000041408517

%s 个评论

要回复文章请先登录注册