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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
[root@node1 ~]# yum -y install lrzsz
//先将包文件拖进来
[root@node1 ~]# cd /opt/
[root@node1 opt]# ls
apr-1.7.0.tar.gz apr-util-1.6.1.tar.gz httpd-2.4.54.tar.gz mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz php-8.2.5.tar.gz
[root@node1 ~]# cd /
[root@node1 ~]# vim lamp.sh
//在里面写入以下内容
#!/bin/bash

#安装工具包
echo "正在安装需要的工具包,请稍等..."
yum -y install bzip2 vim make wget openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ libxml2-devel &>/dev/null
echo "安装完成"

#解压apr包
echo "解压apr包文件"
cd /opt
tar -zxvf apr-1.7.0.tar.gz -C /usr/local/ &>/dev/null
tar -zxvf apr-util-1.6.1.tar.gz -C /usr/local &>/dev/null

#安装apr包
cd /usr/local/apr-1.7.0/
sed -i '/$RM "$cfgfile"/d' configure
echo "正在编译安装apr,请耐心等待..."
./configure --prefix=/usr/local/apr &> /dev/null
make &> /dev/null
make install &> /dev/null
echo "apr安装完成"

#安装apr-util
cd /usr/local/apr-util-1.6.1/
echo "正在编译安装apr-util,请耐心等待..."
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr &> /dev/null
make &> /dev/null
make install &> /dev/null
echo "apr-util安装完成"

#安装httpd
echo "正在解压httpd源码包"
cd /opt
tar -zxvf httpd-2.4.54.tar.gz &>/dev/null
echo “httpd源码包解压完成”
cd /opt/httpd-2.4.54
echo "正在编译安装httpd,请耐心等待..."
./configure --prefix=/usr/local/apache \
--sysconfdir=/etc/httpd24 \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-zlib \
--with-pcre \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util/ \
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=prefork &>/dev/null
make &> /dev/null
make install &> /dev/null
cd
echo "httpd安装完成"

#配置systemctl管理httpd
echo "正在配置systemctl管理httpd"
# 定义服务单元文件路径
cat > /usr/lib/systemd/system/httpd.service <<EOF
[Unit]
Description=Apache HTTP Server
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/apache/bin/apachectl start
ExecStop=/usr/local/apache/bin/apachectl stop
ExecReload=/bin/kill -HUP \$MAINPID

[Install]
WantedBy=multi-user.target
EOF

echo "启动httpd服务"
systemctl restart httpd
systemctl enable httpd

#部署lamp-mysql
echo "-----正在部署mysql-----"
#创建mysql用户
echo "正在验证mysql用户..."
id mysql &> /dev/null
if [ $? -eq 0 ];then
echo "mysql用户已存在"
else
echo "正在创建mysql用户..."
useradd -r -M -s /sbin/nolgin mysql
echo "mysql用户创建成功"
fi
#安装依赖包
echo "正在安装依赖包..."
yum -y install libncurses* &> /dev/null
echo "安装完成"

#配置mysql
echo "正在配置mysql..."
tar -zxvf /opt/mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz -C /usr/local/ &> /dev/null
ln -sv /usr/local/mysql-5.7.37-linux-glibc2.12-x86_64 /usr/local/mysql &> /dev/null
chown -R mysql.mysql /usr/local/mysql &> /dev/null
mkdir -p /opt/data &> /dev/null
chown -R mysql.mysql /opt/data/ &> /dev/null
yum -y install libaio &> /dev/null

#数据库初始化
/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/opt/data/ &> /dev/null
ln -sv /usr/local/mysql/include/ /usr/local/include/mysql &> /dev/null
echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf &> /dev/null
ldconfig &> /dev/null

#编辑mysql配置文件
cat > /etc/my.cnf <<EOF
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
EOF

sed -ri "s#^(basedir=).*#\1/usr/local/mysql#g" /usr/local/mysql/support-files/mysql.server
sed -ri "s#^(datadir=).*#\1/opt/data#g" /usr/local/mysql/support-files/mysql.server

#定义mysql服务可以使用systemd来进行管理
cat > /usr/lib/systemd/system/mysqld.service <<EOF
[Unit]
Description=mysql server daemon
After=network.targe

[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStop=/usr/local/mysql/support-files/mysql.server stop
ExecReload=/bin/kill -HUP \$MAINPID

[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload && echo "*****重新加载成功*****"
systemctl enable --now mysqld && echo "*****开机自启mysql成功*****"

#设置数据库密码
read -p "请输入您要设置的数据库密码:" mysql_password
/usr/local/mysql/bin/mysql -uroot -e "set password=password('$mysql_password')"

#做一个链接,添加mysql命令的变量到环境变量
echo 'export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
source /etc/profile
echo "-----mysql部署完成-----"


#部署lamp-php
#安装依赖包,安装GD库和GD库关联程序,用来处理和生成图片
echo "正在安装依赖包"
yum -y install \
libjpeg \
libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 \
libxml2-devel \
zlib zlib-devel \
curl curl-devel &>/dev/null
yum -y install wget gcc gcc-c++ autoconf automake libtool make libxml2 libxml2-devel openssl openssl-devel sqlite-devel libpng libpng-devel libpsl libpsl-devel libicu-devel libcurl-devel &> /dev/null
yum -y install openldap openldap-devel &>/dev/null
cp -frp /usr/lib64/libldap* /usr/lib/
yum --enablerepo=devel -y install oniguruma-devel &>/dev/null
echo "依赖包已经安装完成"
#解压php包文件
cd /opt
tar -zxvf php-8.2.5.tar.gz &>/dev/null
echo "解压php包文件成功"


#配置编译安装php
cd php-8.2.5
echo "正在配置php"
./configure --prefix=/usr/local/php \
--with-apxs2=/usr/local/apache/bin/apxs \
--with-mysql-sock=/tmp/mysql.sock \
--enable-fpm \
--enable-mysqlnd \
--enable-mysqlnd-compression-support \
--with-zlib \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-mbregex \
--enable-mbstring \
--enable-intl \
--enable-ftp \
--enable-gd-jis-conv \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--enable-soap \
--with-gettext \
--enable-fileinfo \
--enable-opcache \
--with-pear \
--with-ldap=shared \
--without-gdbm \
--with-curl \
--enable-ctype \
--without-pear --disable-phar &>/dev/null
echo "正在编译安装php"
make &>/dev/null
make install &>/dev/null
echo "php编译安装完成"

#配置php
echo "正在配置php"

#测试环境时使用php.ini-development文件,生产环境使用php.ini-production文件
cp php.ini-development /usr/local/php/lib/php.ini

#修改php配置文件,设置时区
sed -i 's/;date.timezone =/date\.timezone = \Asia\/Shanghai/' /usr/local/php/lib/php.ini

#修改httpd服务配置文件,让apache支持php
echo "AddType application/x-httpd-php .php" >> /etc/httpd24/httpd.conf
echo "AddType application/x-httpd-php-source .phps" >> /etc/httpd24/httpd.conf

sed -i 's/index.html/index\.php index\.html/' /etc/httpd24/httpd.conf

#验证php测试页
rm -f /usr/local/apache/htdocs/index.html

cat > /usr/local/apache/htdocs/index.php <<EOF
<?php
phpinfo();
?>
EOF

echo "php配置完成"

#启动apache服务
/usr/local/apache/bin/apachectl restart
echo "lamp部署完成"

执行脚本

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
[root@node1 ~]# chmod +x lamp.sh 
[root@node1 ~]# ./lamp.sh
正在安装需要的工具包,请稍等...
安装完成
解压apr包文件
正在编译安装apr,请耐心等待...
apr安装完成
正在编译安装apr-util,请耐心等待...
apr-util安装完成
正在解压httpd源码包
“httpd源码包解压完成”
正在编译安装httpd,请耐心等待...
httpd安装完成
正在配置systemctl管理httpd
启动httpd服务
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
-----正在部署mysql-----
正在验证mysql用户...
正在创建mysql用户...
useradd: Warning: missing or non-executable shell '/sbin/nolgin'
mysql用户创建成功
正在安装依赖包...
安装完成
正在配置mysql...
*****重新加载成功*****
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
*****开机自启mysql成功*****
请输入您要设置的数据库密码:redhat
-----mysql部署完成-----
正在安装依赖包
依赖包已经安装完成
解压php包文件成功
正在配置php
正在编译安装php
php编译安装完成
正在配置php
php配置完成
lamp部署完成