欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

LNMP一键安装脚本(执行后只需等待即可)

程序员文章站 2022-04-08 22:36:59
LNMP网站架构一键安装脚本...

前言

此脚本是根据LNMP网站架构部署(非常详细,全文1.1w字,列举了从NGINX到PHP源码部署的全过程)博客编写而成。

脚本及软件包分享

百度云盘分享:

链接:LNMP_install.tar.gz
提取码:pxdy

将包打开后,共6个文件,分别是boost包(MySQL依赖库),libmcrypt(PHP依赖包),lnmp_install.script.sh(LNMP网站架构安装脚本),MySQL源码包,Nginx源码包,PHP源码包

ls
boost_1_59_0.tar.bz2  libmcrypt-2.5.8.tar.gz  lnmp_install.script.sh  mysql-5.7.26.tar.gz  nginx-1.9.5.tar.gz  php-5.6.40.tar.gz

脚本内容

#!/bin/bash
#描述:LNMP网站架构部署脚本
echo    "说明:将镜像导入系统,脚本解压到/root/目录,启动执行脚本,接下来只需要等待就可以了。"
echo    "执行流程:搭建yum源-->安装Nginx-->安装MySQL-->安装PHP-->LNMP架构配置-->退出脚本"
echo    -e "\033[31m 1.搭建yum源 \033[0m"
echo    -e "1.网络yum源"
echo    -e "2.本地yum源"
    read -p "请输入你的操作(1-2):" yum

case $yum in
1)
yum install -y wget &> /dev/null
rm -rf /etc/yum.repos.d/*
wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/Centos-7.repo &>/dev/null
if [ $? -eq 0 ];then
    echo    "yum源文件下载成功!!"
else
    echo    "yum源文件下载失败,请检查网络配置!!"
    exit 1
fi
yum clean all &>/dev/null
yum makecache &>/dev/null
echo    "阿里yum源搭建完成!!!"
;;
2)
rm -rf /etc/yum.repos.d/*
cat >> /etc/yum.repos.d/yum.repo << eof
[yum]
name=yum
baseurl=file:///mnt
gpgcheck=0
enabled=1
eof
yum clean all &>/dev/null
yum makecache &>/dev/null
echo    "本地yum源搭建完成!!"
mount /dev/cdrom /mnt/
cd /mnt/Packages/
if [ $? -eq 0 ]; then
    echo    "已将镜像挂载到系统!!"
    cd /root/
else
    echo    "镜像挂载失败,请查看是否将镜像导入系统!!"
    exit 2
fi
;;
*)
    echo    "请输入1或者2操作,输入错误,正在退出..."
    exit    3
esac


echo    -e "\033[34m 2.安装Nginx \033[0m"
sleep 3
echo    "安装Nginx依赖包"
nginx_gz=nginx-1.9.5.tar.gz
nginx=nginx-1.9.5
cd /root
yum install -y gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel  pcre pcre-devel make automake   &>/dev/null
echo    "创建Nginx运行用户"
groupadd www
useradd -g www www -s /sbin/nologin
tar xf $nginx_gz
cd $nginx
./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre --with-http_ssl_module --with-http_gzip_static_module --user=www --group=www &>/dev/null
if [ $? -eq 0 ];then
    echo    "Nginx预编译完成,开始安装"
else
    echo    "Nginx预编译失败,请检查相关依赖包是否安装"
    exit 4
fi
make && make install &>/dev/null
/usr/local/nginx/sbin/nginx
netstat -anput | grep nginx &>/dev/null
if [ $? -eq 0 ];then
    echo    "Nginx启动成功"
else
    echo    "Nginx启动失败"
    exit 5
fi


echo    -e "\033[35m 3.安装MySQL \033[0m"
sleep 3
cd /root
boost_bz2=boost_1_59_0.tar.bz2
mysql_gz=mysql-5.7.26.tar.gz
mysql=mysql-5.7.26
echo    "卸载原有MySQL及相关依赖"
yum -y remove mysql* mariadb* &>/dev/null
echo    "创建MySQL运行用户"
groupadd mysql
useradd -M -s /sbin/nologin -r -g mysql mysql
mkdir -p /data/mysql/{data,log}
chown -R mysql:mysql /data/mysql/
echo    "安装MySQL依赖包"
yum install -y cmake make gcc gcc-c++ bison ncurses ncurses-devel bzip2 &>/dev/null
tar jxf $boost_bz2 -C /root
tar xf $mysql_gz
cd $mysql
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql/data -DSYSCONFDIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/root/boost_1_59_0 &>/dev/null
if [ $? -eq 0 ];then
    echo    "MySQL预编译完成,正在安装"
else
    echo    "MySQL预编译失败,请检查依赖包是否全部安装"
    exit 6
fi
make && make install &>/dev/null
chown -R mysql:mysql /usr/local/mysql/
cat >>/etc/my.cnf<< eof
[mysqld]
basedir=/usr/local/mysql
datadir=/data/mysql/data
port=3306
socket=/usr/local/mysql/mysql.sock
symbolic-links=0
character-set-server=utf8
log-error=/data/mysql/log/mysqld.log
pid-file=/usr/local/mysql/mysqld.pid
eof
cp support-files/mysql.server /etc/init.d/mysql.server
chmod +x /etc/init.d/mysql.server
/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/data
/etc/init.d/mysql.server start
netstat -anplt | grep mysql
if [ $? -eq 0 ];then
    echo    "MySQL启动成功"
else
    echo    "MySQL启动失败"
    exit 7
fi
ln -s /usr/local/mysql/bin/* /usr/local/bin/

echo    -e "\033[36m 4.安装PHP \033[0m"
sleep 3
libmcrypt_gz=libmcrypt-2.5.8.tar.gz
libmcrypt=libmcrypt-2.5.8
php_gz=php-5.6.40.tar.gz
php=php-5.6.40
cd /root
echo    "安装PHP依赖包"
yum -y install gcc autoconf  freetype gd libpng libpng-devel libjpeg libxml2 libxml2-devel zlib curl curl-devel freetype-devel libjpeg-devel bzip2 bzip2-devel openssl openssl-devel
tar xf $libmcrypt_gz
cd $libmcrypt
./configure --prefix=/usr/local/libmcrypt && make && make install &>/dev/null
cd /root
tar xf $php_gz
cd $php
./configure --prefix=/usr/local/php5.6 --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --enable-mbstring --with-curl --with-gd --enable-fpm --with-config-file-path --with-openssl --enable-fpm --enable-sockets  --enable-sysvshm --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml  --with-mhash  --with-mcrypt=/usr/local/libmcrypt --with-config-file-path=/usr/local/php5.6/etc --with-config-file-scan-dir=/etc/php.d --with-bz2  &>/dev/null
make && make install &>/dev/null
cp php.ini-production /usr/local/php5.6/php.ini
cp /usr/local/php5.6/etc/php-fpm.conf.default /usr/local/php5.6/etc/php-fpm.conf
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
/etc/init.d/php-fpm start
netstat -anput | grep php-fpm
if [ $? -eq 0 ];then
    echo    "PHP启动成功"
else
    echo    "PHP启动失败"
    exit 8
fi
echo    -e "\033[33m 5.LNMP架构配置 \033[0m"
sleep 3
echo    "修改Nginx配置文件"
rm -rf /usr/local/nginx/conf/nginx.conf
cat >> /usr/local/nginx/conf/nginx.conf << eof
user  www;
worker_processes  2;
error_log  logs/error.log;
pid        logs/nginx.pid;
events {
        use epoll;
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  logs/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
    #gzip  on;
    server {
        listen       80;
        server_name  localhost;
        charset utf-8;
        location / {
            root   html;
            index  index.php index.html index.htm;
        }
        location ~ \.php$ {
            root  html;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi.conf;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
eof
cat >> /usr/local/nginx/html/index.php << eof
<?php
        phpinfo();
?>
eof
cat >> /usr/local/nginx/html/mysql.php << eof
<?php
$link=mysql_connect('127.0.0.1','lnmp','123456');
if ($link)echo "connection success......";
mysql_close();
?>
eof
echo    "在MySQL中创建测试用户"
mysql -uroot -e "grant all on *.* to 'lnmp'@'%' identified by '123456';"
mysql -uroot -e "flush privileges;"
echo    "重启服务(NML)"
/usr/local/nginx/sbin/nginx -s reload
/etc/init.d/mysql.server restart
/etc/init.d/php-fpm restart
cd /root
echo    -e "\033[37m 6.退出脚本 \033[0m"

测试

执行完成后,直接在浏览器中输入服务器地址,即可进行测试
LNMP一键安装脚本(执行后只需等待即可)
输入服务器地址/mysql.php,对网站是否能连接到后端MySQL进行测试
LNMP一键安装脚本(执行后只需等待即可)

若连接MySQL出现报错现象,那有可能就是mysql.php文件有错误
LNMP一键安装脚本(执行后只需等待即可)
产生报错后,将以下内容直接复制到mysql.php,然后刷新即可

<?php
$link=mysql_connect('127.0.0.1','lnmp','123456');
if ($link)echo "connection success......";
mysql_close();
?>

LNMP一键安装脚本(执行后只需等待即可)
整个网站架构就ok了。

本文地址:https://blog.csdn.net/qq_42534026/article/details/107427750