脚本安装LNMP(详细解读)

文章目录

  • 脚本安装nginx
  • 脚本安装mysql
  • 脚本安装php
  • 完整脚本

过程为详细解读,完整脚本会放在博客最后哦 ^ _^

安装包下载链接:

链接:https://pan.baidu.com/s/1K2ChbNgh0Bpm0yxzYizMow
提取码:chi7

脚本安装nginx

nginx安装版本:1.12.2

nginx_package="nginx-1.12.2.tar.gz"

安装环境依赖

yum -y install gcc gcc-c++ make pcre* zlib-devel

创建运行用户、组

useradd -M -s /sbin/nologin nginx

进程用户,使用-s参数指定shell为nologin

编译安装

tar -xf $nginx_package
#进入安装目录
cd ${nginx_package%.tar.*}
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
make && make install 

指定安装目录为/usr/local/nginx

指定用户和组分别为nginx和nginx

添加http_stub_status_module模块,该模块主要用于站点的访问统计

创建软链接

ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

创建软链接到/usr/local/sbin,这样系统就能直接使用nginx命令

nginx安装完成

脚本安装mysql

mysql版本:5.7.20

使用的包是mysql-boost-5.7.20.tar.gz

设置变量

mysql_package="mysql-boost-5.7.20.tar.gz"
install_dir="/usr/local/mysql"
cnf_file_dir="/etc/"
rootpasswd="#EDC4rfv"
# 配置文件
my_cnf="
[client]
port = 3306
default-character-set=utf8
socket = $install_dir/mysql.sock

[mysql]
port = 3306
default-character-set=utf8
socket = $install_dir/mysql.sock

[mysqld]
users = mysql
basedir = $install_dir
datadir = $install_dir/data
port = 3306
character_set_server=utf8
pid-file = $install_dir/mysqld.pid
socket = $install_dir/mysql.sock
server-id = 1

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERR0R_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
"

这里设置了变量my_cnf存放配置文件的内容,用它输出到新的配置文件中

安装环境依赖

yum -y install \
ncurses \
ncurses-devel \
binson \
gcc gcc-c++ \
make \
pcre* zlib-devel \
cmake &>/dev/null

创建运行用户、组

useradd -s /sbin/nologin mysql

编译安装

tar -xf mysql-boost-5.7.20.tar.gz
cd $(ls -F | grep mysql*/)
cmake \
-DCMAKE_INSTALL_PREFIX=$install_dir \
-DMYSQL_UNIX_ADDR=$install_dir/mysql.sock \
-DSYSCONFDIR=$cnf_file_path \
-DSYSTEMD_PID_DIR=$install_dir \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=$install_dir/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1
make && make install

解压源码包,到源码目录下使用cmake(需要手动下载)

修改mysql配置

cd $cnf_file_dir
# 备份原文件
mv my.cnf my.cnf.bak
touch my.cnf
# 写入内容
echo $my_cnf > my.cnf
# 数据库目录权限调整
chown -R mysql:mysql $install_dir
chown -R mysql:mysql my.cnf

将安装目录和配置文件的属主和属组都设置为mysql、mysql

设置软链接

ln -s $install_dir/bin/* /usr/local/sbin/

初始化数据库

cd $install_dir/bin
mysqld \
--initialize-insecure \
--user=mysql \
--basedir=$install_dir \ 
--datadir=$install_dir/data

设置mysqld为systemd服务

cp $install_dir/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
# 开启mysqld服务
systemctl start mysqld

设置mysql用户和密码

mysql_secure_installation

进入交互,初始化mysql的root用户密码和数据库状态

mysql安装完成

脚本安装php

php安装版本为7.1.10

设置变量

php_package="php-7.1.10.tar.bz2"
install_dir="/usr/local/php" 
mysql_install_dir="/usr/local/mysql"
timezone="Asia/Shanghai"

环境依赖安装

yum -y install \
libjpeg libjpeg-devel \
libpng libpng-devel  \
freetype freetype-devel  \
libxml2 libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel 

编译安装

tar xf $php_package
cd ${php_package%.tar.*}
./configure \
--prefix=$install_dir \
--with-mysql-sock=$mysql_install_dir/mysql.sock \
-with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-fpm \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip
make && make install 

解压源码包到当前目录,进入源码目录进行编译

安装目录为$install_dir指定的目录/usr/local/php

指定mysql.sock文件的路径为$mysql_install_dir/mysql.sock

修改php配置文件

cp php.ini-development  $install_dir/lib/php.ini
sed -i -e "/mysqli.default_socket/s,=.*,= $mysql_install_dir\/mysql.sock," -e "/;date.timezone/cdate.timezone = $timezone," $install_dir/lib/php.ini

复制源码目录下的php.ini-development到安装目录下的lib/,并命名为php.ini

修改配置文件中mysqli.default_socket的值为mysql.sock的路径

修改date.timezone的值为$timezone

配置fpm的配置文件

cd $install_dir/etc/
cp php-fpm.conf.default php-fpm.conf
cd ./php-fpm.d/
cp www.conf.default www.conf
cd ..
sed -i "s/^;pid/pid/"  php-fpm.conf

将php-fpm.conf中pid开头的一行去掉注释

启动fpm

$install_dir/sbin/php-fpm -c $install_dir/lib/php.ini

创建php的软链接

ln -s $install_dir/bin/* /usr/local/bin/

php安装完成

完整脚本

安装nginx

#!/bin/bash
nginx_package="nginx-1.12.2.tar.gz"
# 安装环境依赖
yum -y install gcc gcc-c++ make pcre* zlib-devel
# 创建运行用户、组
useradd -M -s /sbin/nologin nginx
# 编译安装
tar -xf $nginx_package
cd ${nginx_package%.tar.*}
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
make && make install 
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

安装mysql

#!/bin/bash
mysql_package="mysql-boost-5.7.20.tar.gz"
install_dir="/usr/local/mysql"
cnf_file_dir="/etc/"
rootpasswd="#EDC4rfv"
my_cnf="
[client]
port = 3306
default-character-set=utf8
socket = $install_dir/mysql.sock

[mysql]
port = 3306
default-character-set=utf8
socket = $install_dir/mysql.sock

[mysqld]
users = mysql
basedir = $install_dir
datadir = $install_dir/data
port = 3306
character_set_server=utf8
pid-file = $install_dir/mysqld.pid
socket = $install_dir/mysql.sock
server-id = 1

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERR0R_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
"

# 安装环境
echo "安装依赖环境..."
yum -y install \
ncurses \
ncurses-devel \
binson \
gcc gcc-c++ \
make \
pcre* zlib-devel \
cmake &>/dev/null
# 创建运行用户、组
useradd -s /sbin/nologin mysql
# 编译安装
echo "编译安装..."
tar -xf mysql-boost-5.7.20.tar.gz
cd $(ls -F | grep mysql*/)
cmake \
-DCMAKE_INSTALL_PREFIX=$install_dir \
-DMYSQL_UNIX_ADDR=$install_dir/mysql.sock \
-DSYSCONFDIR=$cnf_file_path \
-DSYSTEMD_PID_DIR=$install_dir \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=$install_dir/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1
make && make install
# 修改配置文件
echo "修改配置文件..."
cd $cnf_file_dir
# 备份原文件
mv my.cnf my.cnf.bak
touch my.cnf
# 写入内容
echo $my_cnf > my.cnf
# 数据库目录权限调整
chown -R mysql:mysql $install_dir
chown -R mysql:mysql my.cnf
# 设置软链接
ln -s $install_dir/bin/* /usr/local/sbin/
# 初始化数据库
echo "初始化数据库..."
cd $install_dir/bin
mysqld \
--initialize-insecure \
--user=mysql \
--basedir=$install_dir \ 
--datadir=$install_dir/data
# 设置为systemd服务
cp $install_dir/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
# 开启mysqld服务
echo "开启mysqld..."
systemctl start mysqld
# 设置用户和密码
mysqladmin -u root -p password $rootpasswd

安装php

#!/bin/bash
php_package="php-7.1.10.tar.bz2"
install_dir="/usr/local/php" 
mysql_install_dir="/usr/local/mysql"
timezone="Asia/Shanghai"

echo "安装环境..."
yum -y install \
libjpeg libjpeg-devel \
libpng libpng-devel  \
freetype freetype-devel  \
libxml2 libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel 

echo "编译安装..."
tar xf $php_package
cd ${php_package%.tar.*}
./configure \
--prefix=$install_dir \
--with-mysql-sock=$mysql_install_dir/mysql.sock \
-with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-fpm \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip
make && make install 
cp php.ini-development  $install_dir/lib/php.ini
sed -i -e "/mysqli.default_socket/s,=.*,= $mysql_install_dir\/mysql.sock," -e "/;date.timezone/cdate.timezone = $timezone," $install_dir/lib/php.ini
cd $install_dir/etc/
cp php-fpm.conf.default php-fpm.conf
cd ./php-fpm.d/
cp www.conf.default www.conf
cd ..
sed -i "s/^;pid/pid/"  php-fpm.conf
# 启动fpm
$install_dir/sbin/php-fpm -c $install_dir/lib/php.ini
ln -s $install_dir/bin/* /usr/local/bin/

你可能感兴趣的:(web架构,shell,shell,linux,nginx,mysql,php)