Shell脚本安装lnmp

复制脚本在服务器执行即可安装:curl -o lnmp.sh http://xz.itcytblog.cn/lnmp.sh && bash lnmp.sh

本脚本目前支持指定版本的nginx+mysql+php的安装,脚本有待优化。

目前我发现有较好的处理脚本可学习参考: https://lnmp.org/download.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181

ed -e 's|^mirrorlist=|#mirrorlist=|g' \
-e 's|^#baseurl=http://mirror.centos.org|baseurl=https://opentuna.cn|g' \
-i.bak \
/etc/yum.repos.d/CentOS-*.repo

yum makecache

yum install -y gcc gcc-c++ gtk+-devel

yum install -y apr* autoconf automake bison bzip2 bzip2* cloog-ppl compat* cpp curl curl-devel fontconfig fontconfig-devel freetype freetype* freetype-devel gd gettext gettext-devel glibc kernel kernel-headers keyutils keyutils-libs-devel krb5-devel libcom_err-devel libpng libpng-devel libjpeg* libsepol-devel libselinux-devel libstdc++-devel libtool* libgomp libxml2 libxml2-devel libXpm* libxml* libtiff libtiff* make mpfr ncurses* ntp openssl openssl-devel patch pcre-devel perl php-common php-gd policycoreutils telnet t1lib t1lib* nasm nasm* wget zlib-devel net-tools

wait

cd /usr/local/src
`wget -P /usr/local/src/ http://xz.itcytblog.cn/lanmp/lnmp.tar.gz`
tar -zxvf lnmp.tar.gz
way=/usr/local/src/lnmp

install() {
if [ $? -eq 0 ];then
make 1>/dev/null
if [ $? -eq 0 ];then
make install 1>/dev/null
if [ $? -ne 0 ];then
echo "make install ERROR,exit...";exit 1
else
cd -
fi
else
echo "make install ERROR,exit...";exit 1
fi
else
echo "make install ERROR,exit...";exit 1
fi

}

mysql() {
#安装cmake
if [ -d $way ];then
cd $way
tar zxvf cmake-2.8.12.2.tar.gz
cd cmake-2.8.12.2
else
echo "$way不存在!";exit
fi
./configure 1>/dev/null
install
#安装mysql
mkdir -p /usr/local/boost
cp boost_1_59_0.tar.gz /usr/local/boost
groupadd mysql #添加mysql组
useradd -g mysql mysql -s /bin/false #创建用户mysql并加入到mysql组,不允许mysql用户直接登录系统
mkdir -p /data/mysql #创建MySQL数据库存放目录
chown -R mysql:mysql /data/mysql #设置MySQL数据库存放目录权限
mkdir -p /usr/local/mysql #创建MySQL安装目录
tar zxvf mysql-5.7.27.tar.gz #解压
cd mysql-5.7.27 #进入目录
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DWITH_INNOBASE_STORAGE_ENGINE=1 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EMBEDDED_SERVER=OFF -DWITH_BOOST=/usr/local/boost 1>/dev/null
install
cd /usr/local/mysql
./bin/mysqld --user=mysql --initialize --basedir=/usr/local/mysql --datadir=/data/mysql #生成mysql系统数据库
ln -fs /usr/local/mysql/my.cnf /etc/my.cnf #添加到/etc目录的软连接
cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld #把Mysql加入系统启动
chmod 755 /etc/init.d/mysqld #增加执行权限
chkconfig mysqld on #加入开机启动

cat << EOF >> /etc/rc.d/init.d/mysqld
basedir=/usr/local/mysql
datadir=/data/mysql
EOF

echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
source /etc/profile
wait

#下面这两行把myslq的库文件链接到系统默认的位置,这样你在编译类似PHP等软件时可以不用指定mysql的库文件地址。
ln -s /usr/local/mysql/lib/ /usr/lib/mysql
ln -s /usr/local/mysql/include/mysql /usr/include/mysql
mkdir -p /var/lib/mysql #创建目录
ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock #添加软链接
service mysqld start
[ $? -eq 0 ]&&echo "mysql安装成功。"||exit
}

nginx() {
#安装pcre zlib
array=(pcre-8.43 zlib-1.2.11)
for i in ${!array[@]}
do
cd $way
tar zxvf ${array[$i]}.tar.gz
cd ${array[$i]}
./configure --prefix=/usr/local/${array[$i]}
install
done

#安装openssl
cd $way
tar zxvf openssl-1.1.0l.tar.gz
cd openssl-1.1.0l
./config --prefix=/usr/local/openssl-1.1.0l
install

#安装nginx
groupadd www
useradd -g www www -s /bin/false
cd $way
tar zxvf nginx-1.16.1.tar.gz
cd nginx-1.16.1
./configure --prefix=/usr/local/nginx --without-http_memcached_module --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-openssl=/usr/local/src/lnmp/openssl-1.1.0l --with-zlib=/usr/local/src/lnmp/zlib-1.2.11 --with-pcre=/usr/local/src/lnmp/pcre-8.43

install
wait
`wget -P /etc/rc.d/init.d/ http://xz.itcytblog.cn/nginx`
`wget -P /usr/local/nginx/conf/ http://xz.itcytblog.cn/nginx.conf`
chmod 775 /etc/rc.d/init.d/nginx

chkconfig nginx on
service nginx restart
[ $? -eq 0 ]&&echo "nginx安装成功。"||exit
}

php() {

tar zxvf php-7.1.2.tar.gz
cd php-7.1.2

yum install -y zlib zlib-devel pcre pcre-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers

./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysqli=/usr/local/mysql/bin/mysql_config --with-mysql-sock=/tmp/mysql.sock --with-pdo-mysql=/usr/local/mysql --enable-mbstring --with-openssl --enable-ftp --with-gd --with-jpeg-dir=/usr --with-png-dir=/usr --with-pear --enable-sockets --with-freetype-dir=/usr --with-zlib --with-libxml-dir=/usr --with-xmlrpc --enable-zip --enable-fpm --enable-xml --enable-sockets --with-gd --with-zlib --with-iconv --enable-zip --with-freetype-dir=/usr/lib/ --enable-soap --enable-pcntl --enable-cli --with-curl
wait
install
wait

cd php-7.1.2
cp php.ini-production /usr/local/php/etc/php.ini
rm -rf /etc/php.ini
ln -s /usr/local/php/etc/php.ini /etc/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
ln -s /usr/local/php/etc/php-fpm.conf /etc/php-fpm.conf
sed -i '/^;pid =/cpid = run/php-fpm.pid' /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
sed -i '/^user =/cuser = www' /usr/local/php/etc/php-fpm.d/www.conf
sed -i '/^group =/cgroup = www' /usr/local/php/etc/php-fpm.d/www.conf
cp /usr/local/src/lnmp/php-7.1.2/sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
chmod +x /etc/rc.d/init.d/php-fpm
chkconfig php-fpm on
echo "zend_extension=opcache.so" >> /usr/local/php/etc/php.ini
yes|rm -rf /usr/local/nginx/conf/nginx.conf
`wget -P /usr/local/nginx/conf/ http://103.224.249.63/nginx.conf`

echo "export PATH=$PATH:/usr/local/php/bin" >> /etc/profile
source /etc/profile
wait

service nginx restart
service php-fpm start
[ $? -eq 0 ]&&echo "php安装成功。"||exit
ip=`ifconfig ens33 | grep inet | grep netmask |awk '{print $2}'`
mv /usr/local/nginx/html/index.html /usr/local/nginx/html/index.php
echo "<?php phpinfo();?>" > /usr/local/nginx/html/index.php
echo "请问访问该网站测试环境:http://${ip}/index.php"
}

mysql
nginx
php

service firewalld status
if [ $? -eq 0 ];then
kou=(80 3306 22)
for i in ${!kou[@]}
do
firewall-cmd --permanent --zone=public --add-port=${kou[$i]}/tcp
firewall-cmd --reload
done
fi &>/dev/null

[ -d /usr/local/src/lnmp ];rm -rf /usr/local/src/*