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

使用shell脚本一键部署LNMP架构的方法

程序员文章站 2022-06-17 08:15:22
lnmp架构介绍lnmp:linux系统下nginx+mysql+php这种网站服务器架构。nginx是一个高性能的http和反向代理服务器,也是一个imap/pop3/smtp代理服务器。mysql...

lnmp架构介绍

lnmp:linux系统下nginx+mysql+php这种网站服务器架构。nginx是一个高性能的http和反向代理服务器,也是一个imap/pop3/smtp代理服务器。mysql是一个小型关系型数据库管理系统。php是一种在服务器端执行的嵌入html文档的脚本语言。

原理:浏览器发送http request请求到服务器(nginx),服务器响应并处理web请求,将一些静态资源(css,图片,视频等)保存服务器上,然后将php脚本通过接口传输协议(网关协议)php-fcgi(fast-cgi)传输给php-fpm(进程管理程序),php-fpm不做处理,然后php-fpm调用php解析器进程,php解析器解析php脚本信息。php解析器进程可以启动多个,进行并发执行。然后将解析后的脚本返回到php-fpm,php-fpm再通过fast-cgi的形式将脚本信息传送给nginx.服务器再通过http response的形式传送给浏览器。浏览器再进行解析与渲染然后进行呈现。

下面给大家介绍下使用shell脚本一键部署lnmp架构的方法,脚本如下所示:

#/bin/bash

##将需要的安装包传到/opt目录下,并关闭防火墙
systemctl stop firewalld
systemctl disable firewalld &>/dev/null
setenforce 0

###############nginx###############

##先挂载,再安装依赖包
df -h | grep /mnt
if [ $? -ne 0 ];then
mount /dev/cdrom /mnt &>/dev/null
fi
yum -y install pcre-devel zlib-devel gcc gcc-c++ make

##创建运行用户、组(nginx服务程序默认以nobody身份运行,建议为其创建专门的用户账号,以便更准确的控制其访问权限)
useradd -m -s /sbin/nologin nginx

##编译安装
cd /opt
tar zxvf nginx-1.12.0.tar.gz
cd nginx-1.12.0/
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module

make -j 2 && make install

##优化配置文件路径,并把nginx服务的可执行程序文件放入路径环境变量的目录中便于系统识别
ln -s /usr/local/nginx/sbin/* /usr/local/sbin

##添加nginx系统服务
cat > /lib/systemd/system/nginx.service <<eof
[unit]
description=nginx
after=network.target
[service]
type=forking
pidfile=/usr/local/nginx/logs/nginx.pid
execstart=/usr/local/nginx/sbin/nginx
execreload=/bin/kill -1 $mainpid
execstop=/bin/kill -3 $mainpid
privatetmp=true
[install]
wantedby=multi-user.target
eof

##修改文件权限
chmod 754 /lib/systemd/system/nginx.service
systemctl start nginx.service
systemctl enable nginx.service &>/dev/null

###############mysql###############
##安装mysql环境依赖包
yum -y install ncurses ncurses-devel bison cmake

##创建运行用户
useradd -m -s /sbin/nologin mysql

##编译安装
cd /opt
tar zxvf mysql-boost-5.7.20.tar.gz
cd /opt/mysql-5.7.20
cmake \
-dcmake_install_prefix=/usr/local/mysql \
-dmysql_unix_addr=/usr/local/mysql/mysql.sock \
-dsysconfdir=/etc \
-dsystemd_pid_dir=/usr/local/mysql \
-ddefault_charset=utf8 \
-ddefault_collation=utf8_general_ci \
-dwith_extra_charsets=all \
-dwith_innobase_storage_engine=1 \
-dwith_archive_storage_engine=1 \
-dwith_blackhole_storage_engine=1 \
-dwith_perfschema_storage_engine=1 \
-dmysql_datadir=/usr/local/mysql/data \
-dwith_boost=boost \
-dwith_systemd=1

make -j 2 && make install

##修改mysql配置文件
cat > /etc/my.cnf <<eof
[client]
port = 3306
socket=/usr/local/mysql/mysql.sock			
[mysqld]
user = mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
character-set-server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
bind-address = 0.0.0.0
skip-name-resolve
max_connections=2048
default-storage-engine=innodb
max_allowed_packet=16m
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,error_for_division_by_zero,pipes_as_concat,ansi_quotes
eof

##更改mysql安装目录和配置文件的属主属组
chown -r mysql:mysql /usr/local/mysql/
chown mysql:mysql /etc/my.cnf

##设置路径环境变量
echo 'export path=/usr/local/mysql/bin:/usr/local/mysql/lib:$path' >> /etc/profile	
source /etc/profile

##初始化数据化
cd /usr/local/mysql/bin/
./mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data

##添加mysqld系统服务
cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
systemctl daemon-reload
systemctl start mysqld.service
systemctl enable mysqld &>/dev/null

##添加mysql的登录密码
function mima {
passwd=$1
/usr/bin/expect <<-eof
spawn mysqladmin -u root -p password $passwd
expect "enter" {send "\n"}
expect eof
eof
}
mima "abc123"

##授权远程登录
function qwe {
/usr/bin/expect <<-eof
spawn mysql -u root -p
expect "enter" {send "abc123\n"}
expect "mysql" {send "grant all privileges on *.* to 'root'@'%' identified by 'abc123';\n"}
expect "mysql" {send "flush privileges;\n"}
expect "mysql" {send "show databases;\n"}
expect "mysql" {send "quit\n"}
expect eof
eof
}
qwe
###############php###############
##安装环境依赖包
yum -y install gd \
libjpeg libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel

##编译安装
cd /opt
tar jxvf php-7.1.10.tar.bz2
cd ./php-7.1.10/
./configure \
--prefix=/usr/local/php \
--with-mysql-sock=/usr/local/mysql/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 -j 2 && make install

##路径优化
ln -s /usr/local/php/bin/* /usr/local/bin/
ln -s /usr/local/php/sbin/* /usr/local/sbin/

##调整php配置文件
##php有三个配置文件
##php.ini(主配置文件)
cp /opt/php-7.1.10/php.ini-development /usr/local/php/lib/php.ini
sed -i '1170c mysqli.default_socket = /usr/local/mysql/mysql.sock' /usr/local/php/lib/php.ini
sed -i '939c date.timezone = asia/shanghai' /usr/local/php/lib/php.ini

php -m
##php-fpm.conf(进程服务配置文件)
cd /usr/local/php/etc
cp php-fpm.conf.default php-fpm.conf
sed -i '17c pid = run/php-fpm.pid' /usr/local/php/etc/php-fpm.conf
##www.conf(扩展配置文件)
cd /usr/local/php/etc/php-fpm.d
cp www.conf.default www.conf

##启动php-fpm
cd /usr/local/php/sbin/
php-fpm -c /usr/local/php/lib/php.ini

##配置nginx支持php解析
sed -i '65c location ~ \.php$ {' /usr/local/nginx/conf/nginx.conf
sed -i '66c root html;' /usr/local/nginx/conf/nginx.conf
sed -i '67c fastcgi_pass 127.0.0.1:9000;' /usr/local/nginx/conf/nginx.conf
sed -i '68c fastcgi_index index.php;' /usr/local/nginx/conf/nginx.conf
sed -i '69c fastcgi_param script_filename /usr/local/nginx/html$fastcgi_script_name;' /usr/local/nginx/conf/nginx.conf
sed -i '70c include fastcgi_params;' /usr/local/nginx/conf/nginx.conf
sed -i '71c }' /usr/local/nginx/conf/nginx.conf

##验证php测试页和验证数据库工作是否正常

cat > /usr/local/nginx/html/index.php <<eof
<?php
phpinfo();
?>
eof

到此这篇关于使用shell脚本一键部署lnmp架构的方法的文章就介绍到这了,更多相关shell脚本部署lnmp架构内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!