一,LNMP简介
LNMP通常指的是一组自由运行网站的一套系统的网站服务器架构组成,它集合了linux,nginx,mysql,php,全部都是开源免费的产品组成的。其中也可以有很多替代品,比如LNMP以前是没有的,以前是LAMP,A就是Apache,LAMP现在和LNMP都同样盛行。下面是LNMP的替代简介
L:Linux是计算机操作系统的一种,是目前最流行的免费操作系统。代表版本有debian、centos、ubuntu等。
N:Nginx是一个高性能的HTTP和反向代理服务器。
M:Mysql是一个小型数据型数据库管理系统,自从被收购以后部分功能开始收费,也可以用Maridb,Mongon替代。
P:php是一种在服务器端执行的嵌入HTML文档的脚本语言。
LNMP(脚本)
#!/bin/bash
#function: 部署lnmp
#author: lcn
##################判断是否为root用户################
if
[ "$USER" != "root" ]
then
echo "错误:非root用户,权限不足!"
exit 0
fi
#####################防火墙与高级权限###################
systemctl stop firewalld && systemctl disable firewalld && echo "防火墙已经关闭"
sed -i 's/SELINUX=*/SELINUX=disabled/g' /etc/selinux/config && echo "关闭selinux"
#########下载epel扩展库############
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
################清除repo链接,才可以自动加载epel的库#############
yum clean all
if
[ $? -eq 0 ];
then
echo "清除成功"
else
echo "清除失败"
exit 1
fi
##################安装ngnix的依赖包####################
yum -y install zlib-devel openssl-devel pcre-devel gcc gcc-c++ make cmake nginx
if
[ $? = 0 ];
then
echo "下载成功"
else
echo "下载失败"
exit 1
fi
##########源码编译nginx1.18、创建ngnix用户############
useradd -s /sbin/nologin -c "Web Nginx" -M nginx
NGINX="/root/nginx-1.18.0.tar.gz"
if
[ ! -e $NGINX ]
then
echo "安装包不存在,请上传安装文件到/root/,上传完成再重新运行该脚本"
echo "或wget http://nginx.org/download/nginx-1.18.0.tar.gz下载安装包"
exit 1
fi
tar -zxf $NGINX && cd /root/nginx-1.18.0
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_gzip_static_module \
--with-pcre \
--with-http_stub_status_module \
--with-http_dav_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_flv_module \
--with-http_mp4_module
make && make install
if
[ $? = 0 ];
then
echo "编译安装已成功"
else
echo "编译安装失败"
exit 1
fi
sleep 2
###############赋予所属用户和所属用户组###############
chown -R nginx:nginx /usr/local/nginx/
################配置nginx支持php################
cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
cat > /usr/local/nginx/conf/nginx.conf <<EOF
user nginx;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
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;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
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;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html\$fastcgi_script_name;
include fastcgi_params;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
EOF
####检查配置文件的是否有错误####
nginx -t
sleep 5
#####编辑nginx.sh文件,设置nginx环境变量、生成环境变量、启动nginx服务#######
cat > /etc/profile.d/nginx.sh <<EOF
export PATH=/usr/local/nginx/sbin:$PATH
EOF
source /etc/profile.d/nginx.sh
nginx
#############设置nginx开机自动启动、赋予权限################
cat >> /etc/rc.d/rc.local <<EOF
/usr/local/nginx/sbin/nginx
EOF
chmod +x /etc/rc.d/rc.local
##########安装MySQL########
##############删除系统自带的mariadb###########
rpm -qa |grep mariadb
rpm -e --nodeps mariadb-libs-5*
############安装依赖包libaio#############
yum -y install make gcc-c++ cmake bison-devel ncurses-devel perl openssl-devel
if
[ $? = 0 ]
then
echo "依赖包安装成功"
else
echo "依赖包安装失败"
exit 1
fi
sleep 5
############添加用户及组###########
groupadd mysql
useradd mysql -g mysql -M -s /sbin/nologin
#########上传、解压安装包##########
MySQL="/root/mysql-5.5.62.tar.gz"
if
[ ! -e $MySQL ]
then
echo "安装包不存在,请上传安装文件到/root/,上传完成再重新运行该脚本"
exit 1
fi
tar -zxf $MySQL && cd mysql-5.5.62
############编译安装##########
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/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=/tmp/mysqld.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
make && make install
if
[ $? = 0 ]
then
echo "编译安装已成功"
else
echo "编译安装失败"
exit 1
fi
sleep 2
#######################
yum -y install autoconf
cd /usr/local/mysql
./scripts/mysql_install_db --user=mysql --datadir=/usr/local/mysql/data
#########账户权限配置###########
chown mysql:mysql /usr/local/mysql
############配置文件###########
cat >> /etc/my.cnf << EOF
[client]
default-character-set=utf8
port = 3306
socket = /tmp/mysql.sock
[mysqld]
datadir=/usr/local/mysql/data
default-storage-engine=InnoDB
lower_case_table_names=1
character-set-server=utf8
init_connect='SET NAMES utf8
port = 3306
socket = /tmp/mysql.sock
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
character-set-server=utf8
init_connect='SET NAMES utf8'
log-bin=mysql-bin
binlog_format=mixed
server-id = 1
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
default-character-set=utf8
[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
EOF
##########变量配置文件###########
cat >> /etc/profile << EOF
export PATH=$PATH:/usr/local/mysql/support-files
EOF
source /etc/profile
rm -rf /usr/bin/mysql*
##################启动登入数据库#####################
/usr/local/mysql/support-files/mysql.server start
ln -s /usr/local/mysql/bin/mysql /usr/bin && echo "完成,请登录mysql数据库"
sleep 5
#################安装php####################
#################安装php依赖包#####################
yum -y remove php*
yum -y install libmcrypt-devel libxml2-devel curl-devel libjpeg-devel libpng-devel freetype-devel
if
[ $? = 0 ];
then
echo "下载php依赖包已成功"
else
echo "下载php依赖包失败"
exit 1
fi
sleep 2
###################卸载之前安装的php、下载cmake#################
yum remove php* -y
cd /root
tar -zxf cmake-3.18.0-rc1.tar.gz && cd cmake-3.18.0-rc1/
./configure --prefix=/usr/local/cmake
make && make install
if
[ $? -eq 0 ];
then
echo "编译安装已成功"
else
echo "编译安装失败"
exit 1
fi
sleep 2
###################设置cmake的环境变量、生成cmake的环境变量#################
cat > /etc/profile.d/cmake.sh <<EOF
export PATH=/usr/local/cmake/bin:$PATH
EOF
source /etc/profile.d/cmake.sh
###############################卸载之前安装过的libzip,下载libzip####################
yum remove libzip -y
cd /root
tar -zxf libzip-1.7.1.tar.gz && cd /root/libzip-1.7.1/
cmake /root/libzip-1.7.1
make && make install
if
[ $? = 0 ];
then
echo "编译安装已成功"
else
echo "编译安装失败"
exit 1
fi
sleep 2
######添加搜索路径到配置文件、更新配置#########
cat > /etc/ld.so.confl <<EOF
/usr/local/lib64
/usr/local/lib
/usr/lib
/usr/lib64
EOF
ldconfig -v
##############安装php##############
cd /root
tar -zxf php-7.1.4.tar.gz && cd /root/php-7.1.4/
##########编译安装###########
./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/ \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv-dir \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--with-gd \
--with-curl \
--with-openssl \
--with-gettext \
--with-mhash \
--with-xmlrpc \
--without-pear \
--enable-fpm \
--enable-xml \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--enable-mbregex \
--enable-mbstring \
--enable-ftp \
--enable-pcntl \
--enable-sockets \
--enable-zip \
--enable-soap \
--enable-maintainer-zts \
--disable-fileinfo \
--disable-rpath
make && make install
if
[ $? = 0 ];
then
echo "编译安装已成功"
else
echo "编译安装失败"
exit 1
fi
sleep 2
##########复制php.ini到编译安装后的目录下#####
cp /root/php-7.1.4/php.ini-production /usr/local/php/php.ini
###########复制php-fpm.conf的配置文件######
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.conf
###########修改用户和用户组为nginx##########
sed -i 's/user = nobody/user = nginx/g' /usr/local/php/etc/php-fpm.conf
sed -i 's/group = nobody/group = nginx/g' /usr/local/php/etc/php-fpm.conf
############设置php的环境变量、生效php的环境变量###########
cat > /etc/profile.d/php.sh <<EOF
export PATH=/usr/local/php/bin:$PATH
EOF
source /etc/profile.d/php.sh
#######复制php-fpm的启动脚本##########
cp /root/php-7.1.4/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
######设置开机自动启动php-fpm并启动php-fpm##########
chkconfig php-fpm on && /etc/init.d/php-fpm start
###########测试配置是否成功##########
cat >> /usr/local/nginx/html/index.php <<EOF
<?php phpinfo(); ?>
EOF
IP=$(hostname -I | awk '{print $1}' | awk -F '.' '{print $1"."$2"."$3"."$4}')
echo "lnmp服务已部署完成,请用$IP/index.php访问网页"