系统:CentOS 7.9
软件版本:Nginx:1.20.1 MySQL:5.7.39 PHP:7.0.33
注意系统无以上软件其他版本,可以使用yum源即可。
vim lnmp.sh
#/bin/sh
error_handling() {
if [ $? -ne 0 ]; then
echo " ERROR: $@"
exit 1
fi
}
TMP_LOG=/tmp/log.$$
NGINX_PACKAGE=nginx
MYSQL_PACKAGE=mysql-community-server
MYSQL_RPM=http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
MYSQL_PASSRE=Devonl77@163.com
KERNEL_VERSION=`uname -r | awk -F "." '{print($6)}'`
EPELSOURCE=https://repo.ius.io/ius-release-el7.rpm
EPELSOURCES=https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
WEBTATIC=https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
PHPDEVEL=php70w-devel
PHP70W=php70w.x86_64
PHP70WCLI=php70w-cli.x86_64
PHP70WCOMMON=php70w-common.x86_64
PHP70WGD=php70w-gd.x86_64
PHP70WLDAP=php70w-ldap.x86_64
PHP70WMBSTRING=php70w-mbstring.x86_64
PHP70WMCRYPT=php70w-mcrypt.x86_64
PHP70WPDO=php70w-pdo.x86_64
PHP70WMYSQLND=php70w-mysqlnd
PHP70WFPM=php70w-fpm
PHP70WOPCACHE=php70w-opcache
PHP70WPECLREDIS=php70w-pecl-redis
PHP70WPECLMONGODB=php70w-pecl-mongodb
##### Step1
echo "# Step 1: install Nginx"
echo " please make sure that the YUM source is configured correctly"
echo " yum update -y && yum install nginx -y"
echo " begin yum update ..."
echo " maybe very slow, please check ${TMP_LOG}"
echo " Add epel source"
yum install -y $EPELSOURCE $EPELSOURCES >${TMP_LOG} 2>&1
yum update -y >${TMP_LOG} 2>&1
echo " begin install ${NGINX_PACKAGE}"
yum install ${NGINX_PACKAGE} -y >>${TMP_LOG} 2>&1
error_handling "yum install ${NGINX_PACKAGE} failed."
echo " Nginx Version: "
nginx -v
echo " Set Nginx service enable"
systemctl enable nginx >${TMP_LOG} 2>&1
error_handling "Set Nginx start failed."
echo
##### Step2
echo "# Step 2: install MySQL"
echo " Update MySQL yum source"
rpm -Uvh $MYSQL_RPM >${TMP_LOG} 2>&1
if [ "$KERNEL_VERSION"=="el8" ]; then
echo " Begin install $MYSQL_PACKAGE"
yum module -y disable mysql >${TMP_LOG} 2>&1
yum install -y $MYSQL_PACKAGE --nogpgcheck >${TMP_LOG} 2>&1
error_handling "yum install ${MYSQL_PACKAGE} failed."
else
echo " Begin install $MYSQL_PACKAGE"
yum install -y $MYSQL_PACKAGE --nogpgcheck >${TMP_LOG} 2>&1
error_handling "yum install ${MYSQL_PACKAGE} failed."
fi
echo " MySQL Version: "
mysql -V
echo
##### Step3
echo "# Step 3: install PHP"
error_handling "Add epel source failed"
echo " Add Webtatic source"
rpm -Uvh $WEBTATIC >${TMP_LOG} 2>&1
error_handling "Add Webtatic source failed"
echo " Begin install PHP"
yum install -y $PHPDEVEL $PHP70W $PHP70WCLI $PHP70WCOMMON $PHP70WGD $PHP70WLDAP $PHP70WMBSTRING $PHP70WMCRYPT $PHP70WMCRYPT $PHP70WPDO $PHP70WMYSQLND $PHP70WFPM $PHP70WOPCACHE $PHP70WPECLREDIS $PHP70WPECLMONGODB >${TMP_LOG} 2>&1
error_handling "yum install PHP failed."
echo " PHP Version: "
php -v
echo
##### Step4
echo "# Step 4: Configure nginx.conf"
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
sed -i '46i\ location / {\n index index.php index.html index.htm;\n }' /etc/nginx/nginx.conf
sed -i '50i\ location ~ \.php$ {\n root /usr/share/nginx/html;\n fastcgi_pass 127.0.0.1:9000;\n fastcgi_index index.php;\n fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;\n include fastcgi_params;\n }' /etc/nginx/nginx.conf
echo "# Start and enable Nginx Service"
systemctl start nginx
error_handling "Start Nginx Service failed."
##### Step5
echo "# Step 5: Start and enable MySQL Service"
systemctl enable mysqld
systemctl start mysqld
error_handling "Start MySQL failed"
##### Step6
echo "# Step 6: install expect "
yum install -y expect ${TMP_LOG} 2>&1
error_handling "Install expect failed"
echo "# Step 7: init MySQL Password"
MYSQL_PASSINI=`grep 'temporary password' /var/log/mysqld.log | awk -F " " '{print $NF}'`
/usr/bin/expect << EOF
spawn mysql_secure_installation
expect {
"Enter password for user root" { send "$MYSQL_PASSINI\r"; exp_continue }
"Change the password for root" { send "y\r"; exp_continue }
"New password" { send "$MYSQL_PASSRE\r"; exp_continue }
"Re-enter new password" { send "$MYSQL_PASSRE\r"; exp_continue }
"Do you wish to continue with the password provided" { send "Y\r"; exp_continue }
"Remove anonymous users" { send "Y\r"; exp_continue }
"Disallow root login remotely" { send "Y\r"; exp_continue }
"Remove test database and access to it" { send "Y\r"; exp_continue }
"Reload privilege tables now" { send "Y\r"; exp_continue }
}
EOF
error_handling "Init MySQL Password failed"
##### Step8
echo "# Step 8: configure PHP"
echo "<?php echo phpinfo(); ?>" >> /usr/share/nginx/html/phpinfo.php
##### Step9
echo "# Step 9: start and enable PHP"
systemctl start php-fpm
systemctl enable php-fpm
error_handling "Start PHP failed."
echo "Now that LNMP has been built, please visit: YOURIP/phpinfo.php for test php,visit YOUIP for test nginx"
echo "---Author: DevonL---"
echo "---MaiL: devonl77@163.com---"
echo "---MaiL: devonl77@163.com---"
echo "MySQL Password: Devonl77@163.com"
脚本执行权限
chmod +x lnmp.sh
执行脚本
./lnmp.sh
等待执行,安装执行完毕即可
安装完之后
访问:ip地址,出现如下nginx服务正常
访问:ip地址/phpinfo.php,出现php版本信息,php服务正常
Mysql的用户名:root 密码:Devonl77@163.com
转载请标明引用