LNMP 源码部署脚本

Posted 锦衣admin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LNMP 源码部署脚本相关的知识,希望对你有一定的参考价值。

LNMP 部署脚本

部署版本:

nginxmysqlphp
nginx-1.14.2.tar.gzmysql-5.6.33.tar.gzphp-7.2.12.tar.gz

MySQL数据库密码:123

Nginx 和 php 的子进程运行用户为www

LNMP 部署脚本:

#!/bin/bash
# Desc:LNMP 源码部署脚本

# 安装MySQL源码
mysql_install () 
cd /root/soft
# 创建MMySQL软件运行用户
[ -f /etc/my.cnf ] && mv /etc/my.cnf /etc/my.cnf.bak
if ! (id mysql &>/dev/null);then
	useradd -r -s /sbin/nologin -M mysql
fi

# 解压、安装依赖、及编译安装
if [ -n "$mysql" ];then
    mkdir ./$mysql 2>/dev/null && tar xf $mysql.tar.gz -C ./$mysql --strip-components 1 2>/dev/null
    yum -y install cmake ncurses-devel openssl-devel gcc-c++ libaio-devel &> /dev/null

cd $mysql/
cmake \\
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \\
-DMYSQL_DATADIR=/usr/local/mysql/data \\
-DSYSCONFDIR=/etc \\
-DENABLED_LOCAL_INFILE=1 \\
-DWITH_PARTITION_STORAGE_ENGINE=1 \\
-DEXTRA_CHARSETS=all \\
-DDEFAULT_CHARSET=utf8mb4 \\
-DDEFAULT_COLLATION=utf8mb4_general_ci \\
-DWITH_SSL=bundled
make && make install
fi

# 初始化MySQL数据库
if [ -d /usr/local/mysql ];then
   cd /usr/local/mysql
	# 拷贝配置文件   
   cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf

	# 授权并初始化MySQL
   chown -R mysql:mysql /usr/local/mysql
   /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
   # 拷贝服务启动脚本,添加环境变量,添加开机启动项
   cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
   echo 'export PATH=$PATH:/usr/local/mysql/bin' >> /etc/profile
	source /etc/profile
	service mysqld start
	chkconfig mysqld on	
fi
# 对MySQL进行安全配置,删除匿名用户,数据库密码设为123,利用expect自动交互套件执行
if ! (rpm -q expect &> /dev/null);then
yum -y install expect
fi

/usr/bin/expect <<-EOF
set timeout 20
spawn mysql_secure_installation
expect 
 "enter for none"  send "\\r"; exp_continue 
 " password:"  send "123\\r"; exp_continue  
 "Y/n"   send "Y\\r"; exp_continue 
 "Cleaning up"  send "\\r"

expect eof
EOF



# 安装Nginx
nginx_install () 
cd /root/soft

# 安装依赖,及创建软件运行用户
yum -y install pcre-devel zlib-devel openssl-devel
if ! (id www &> /dev/null);then
	useradd -s /sbin/nologin -M www
fi


# 编译安装源码
if [ -n "$nginx" ];then
	mkdir ./$nginx 2>/dev/null && tar xf $nginx.tar.gz -C ./$nginx --strip-components 1 2>/dev/null
	cd $nginx/
	./configure  --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module
	make && make install
fi

# 编写nginx服务启动脚本
cat >> /usr/lib/systemd/system/nginx.service <<EOF
[Unit]
Description=nginx - high performance web server
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

# 重载配置并启动脚本 
systemctl daemon-reload
systemctl start nginx 
systemctl enable nginx


# PHP 安装
php_install () 
cd /root/soft

# 安装依赖
yum -y install libxml2-devel libjpeg-devel libpng-devel freetype-devel curl-devel openssl-devel

# 编译安装
if [ -n "$php" ];then
	mkdir ./$php 2>/dev/null && tar xf $php.tar.gz -C ./$php --strip-components 1 2>/dev/null
	cd $php/
./configure --prefix=/usr/local/php \\
--with-config-file-path=/usr/local/php/etc \\
--enable-fpm \\
--with-fpm-user=www \\
--with-fpm-group=www \\
--with-mysqli=mysqlnd \\
--with-pdo-mysql=mysqlnd \\
--with-iconv-dir \\
--with-freetype-dir \\
--with-jpeg-dir \\
--with-png-dir \\
--with-zlib \\
--with-libxml-dir \\
--enable-xml \\
--disable-rpath \\
--enable-bcmath \\
--enable-shmop \\
--enable-sysvsem \\
--enable-inline-optimization \\
--with-curl \\
--enable-mbregex \\
--enable-mbstring \\
--enable-ftp \\
--with-gd \\
--with-openssl \\
--with-mhash \\
--enable-pcntl \\
--enable-sockets \\
--with-xmlrpc \\
--with-libzip \\
--enable-soap \\
--without-pear \\
--with-gettext \\
--disable-fileinfo \\
--enable-maintainer-zts
make && make install
fi

# 让配置文件生效
if [ -d /usr/local/php ];then
	# 开启php-fpm相关的配置文件
	cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
	cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
	# 将php默认配置文件复制到安装目录/etc 下,并更名为php.ini
	cp /root/soft/$php/php.ini-development /usr/local/php/etc/php.ini
fi

# 复制php脚本服务文件到/usr/lib/systemd/system/ 目录下
cp /root/soft/$php/sapi/fpm/php-fpm.service /usr/lib/systemd/system/
systemctl daemon-reload
systemctl start php-fpm
systemctl enable php-fpm


main () 
read -p "请输入安装的MySQL版本,如:mysql-5.6.33:" mysql
read -p "请输入安装的nginx版本,如:nginx-1.14.2:" nginx
read -p "请输入安装的php版本,如:php-7.2.12:" php

mysql_install
nginx_install
php_install

main

以上是关于LNMP 源码部署脚本的主要内容,如果未能解决你的问题,请参考以下文章

LNMP源码编译安装及一键部署LNMP架构

一键安装lnmp脚本

lnmp部署

源码编译安装LNMP及论坛部署

CentOS6.8系统部署lnmp环境

lnmp源码安装-脚本执行