LAMP(Linux+Apache+MySQL+PHP)环境介绍、配置、搭建
LAMP介绍
LAMP是由Linux, Apache, MySQL, PHP组成的,即把Apache、MySQL以及PHP安装在Linux系统上,组成一个环境来运行PHP的脚本语言。Apache是最常用的Web服务软件,而MySQL是比较小型的数据库软件。三个角色可以安装在一台机器上,也可以分开(但httpd和php要在一起的)
为什么需要LAMP?
手动逐个编译安装各个组件,需要输入各种命令,且有可能会中途出错。
对于想要成功搭建 LAMP 建站环境来说,可以节约大量时间和精力。
LAMP 有什么优点?
LAMP 是最经典的建站环境之一,风靡世界十几年。
而本脚本只需几个简单交互,选择需要安装的包,即可一路安装,无人值守。
准备工作
bash
yum install -y bzip2 tar net-tools wget expect
安装MySQL:
MySQL介绍
MySQL是一个关系型数据库,由mysql ab公司开发,mysql在2008年被sun公司收购(10亿刀),2009年sun公司被oracle公司收购(74亿刀)
MySQL官网https://www.mysql.com 最新版本5.7GA/8.0DMR
MySQL5.6变化比较大,5.7性能上有很大提升
MySQL安装过程
bash
# 安装必要的软件包
yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake
# 添加mysql用户,设置不登录
useradd -s /sbin/nologin mysql
# 解压MySQL源码
tar xf /opt/mysql-5.7.17.tar.gz -C /opt
# 解压Boost源码
tar xf /opt/boost_1_59_0.tar.gz -C /usr/local/
# 重命名Boost源码目录
mv -f /usr/local/boost_1_59_0 /usr/local/boost
# 进入MySQL源码目录
cd /opt/mysql-5.7.17
# 配置MySQL编译选项
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=/usr/local/boost \
-DWITH_SYSTEMD=1
# 使用多核编译并安装
make -j 4 && make install
# 将自定义的my.cnf配置文件移动到/etc并修改权限
cp -f /opt/my.cnf /etc/my.cnf
chown -R mysql:mysql /usr/local/mysql/
chown mysql:mysql /etc/my.cnf
# 在环境变量中添加MySQL/bin路径
echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
source /etc/profile
# 初始化MySQL数据库
cd /usr/local/mysql/bin/
./mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
# 复制MySQL的systemd服务文件
cp -f /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
# 重新加载systemd配置
systemctl daemon-reload
# 启动MySQL服务并设置开机自启动
systemctl start mysqld.service
systemctl enable mysqld
# 检查MySQL服务是否正常运行在3306端口
netstat -anpt | grep 3306
my.cnf
sh
[client]
port = 3306
default-character-set=utf8
socket=/usr/local/mysql/mysql.sock
[mysql]
port = 3306
default-character-set=utf8
socket=/usr/local/mysql/mysql.sock
auto-rehash
[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
mysql设置用户密码脚本
bash
#!/usr/bin/expect
set timeout 30
spawn mysqladmin -u root -p password \"123456\"
expect {
"*password*" {send "\r"}
}
expect eof
安装Httpd(Apache)
脚本源代码:
bash
#!/bin/bash
# 安装软件包
function install_packages {
yum install -y $1
}
# 提取tarball文件并将其解压到/opt目录下
function extract_tarball {
tar xf $1 -C /opt
}
# 配置并编译安装软件
function configure_and_make {
cd $1
./configure $2
make -j $(nproc) && make install
}
# 配置Apache,将IP和端口替换为更安全的值
function configure_httpd {
LOCAL_IP=$(ip addr show ens33 | grep "inet " | awk '{print $2}' | cut -d/ -f1)
sed -i "s/#ServerName www.example.com:80/ServerName $LOCAL_IP:80/" /usr/local/httpd/conf/httpd.conf
}
# 启动服务并设置开机自启动
function start_service {
systemctl daemon-reload
systemctl start $1.service
systemctl enable $1
}
# 创建符号链接
function create_symbolic_links {
local source_path="$1"
local target_path="$2"
# 检查是否以root身份运行
if [ "$(id -u)" -ne 0 ]; then
echo "必须以root身份运行此脚本。" >&2
return 1
fi
# 遍历源路径下的文件,创建符号链接
for file in "$source_path"/*
do
base=$(basename "$file")
link_target="$target_path/$base"
# 检查目标是否已存在
if [ -L "$link_target" ]; then
echo "警告:$link_target 已存在,将被覆盖。" >&2
else
if [ -e "$file" ]; then
echo "创建符号链接: $file => $link_target"
ln -s -T "$file" "$link_target" || { echo "创建链接失败,请检查权限和路径。" >&2; return 1; }
else
echo "错误:$file 不存在。" >&2
return 1
fi
fi
done
}
# 主脚本逻辑开始
extract_tarball "/opt/apr-1.6.2.tar.gz"
extract_tarball "/opt/apr-util-1.6.0.tar.gz"
extract_tarball "/opt/httpd-2.4.29.tar.bz2"
mv /opt/apr-1.6.2 /opt/httpd-2.4.29/srclib/apr
mv /opt/apr-util-1.6.0 /opt/httpd-2.4.29/srclib/apr-util
# 安装编译依赖包
install_packages \
"gcc gcc-c++ make pcre-devel expat-devel perl"
# 配置并编译安装Apache
configure_and_make "/opt/httpd-2.4.29" \
"--prefix=/usr/local/httpd \
--enable-so \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi"
configure_httpd
# 创建必要的符号链接
create_symbolic_links "/usr/local/httpd/conf" "/etc"
create_symbolic_links "/usr/local/httpd/bin" "/usr/local/bin"
# 创建Apache的systemd服务文件
echo '[Unit]
Description=The Apache HTTP
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/httpd/logs/httpd.pid
ExecStart=/usr/local/bin/apachectl $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
' > /usr/lib/systemd/system/httpd.service
# 启动Apache服务并设置开机自启动
start_service httpd
安装PHP
bash
#!/bin/bash
# 使用函数封装功能,增加可读性和可维护性
function install_packages {
yum install -y $1
}
function extract_tarball {
tar xf $1 -C /opt
}
function configure_and_make {
cd $1
./configure $2
make -j $(nproc) && make install
}
function configure_httpd {
# 配置 Apache,替换 IP 和端口为更安全的值
LOCAL_IP=$(ip addr show ens33 | grep "inet " | awk '{print $2}' | cut -d/ -f1)
sed -i "s/Listen 80/Listen $LOCAL_IP:80/g" /usr/local/httpd/conf/httpd.conf
sed -i "s/#ServerName www.example.com:80/ServerName $LOCAL_IP:80/" /usr/local/httpd/conf/httpd.conf
}
function start_service {
systemctl daemon-reload
systemctl start $1.service
systemctl enable $1
}
function create_symbolic_links {
local source_path="$1"
local target_path="$2"
# 检查是否以root身份运行
if [ "$(id -u)" -ne 0 ]; then
echo "必须以root身份运行此脚本。" >&2
return 1
fi
# 遍历源路径下的文件
for file in "$source_path"/*
do
base=$(basename "$file")
link_target="$target_path/$base"
# 检查目标是否已存在
if [ -L "$link_target" ]; then
echo "警告:$link_target 已存在,将被覆盖。" >&2
else
if [ -e "$file" ]; then
echo "创建符号链接: $file => $link_target"
ln -s -T "$file" "$link_target" || { echo "创建链接失败,请检查权限和路径。" >&2; return 1; }
else
echo "错误:$file 不存在。" >&2
return 1
fi
fi
done
}
# 安全的 MySQL 初始化和配置
function initialize_mysql {
cd /usr/local/mysql/bin
./mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
}
# 更新 PATH 以包含 PHP
function configure_php {
# 定义变量
PHP_INI_SRC=/opt/php-7.1.24/php.ini-development
PHP_INI_DEST=/usr/local/php7/php.ini
HTTPD_CONF=/etc/httpd.conf
HTTPD_HTDICT=/usr/local/httpd/htdocs
MYSQL_INIT_SH=/opt/mysql_init2.exp
UNZIP_FILE=/opt/Discuz_X3.4_SC_UTF8.zip
DIS_DIR=/opt/dis/dir_SC_UTF8/upload
BBS_DIR=/usr/local/httpd/htdocs/bbs
# 复制php.ini文件并编辑
cp -f "$PHP_INI_SRC" "$PHP_INI_DEST"
sed -i 's/;date.timezone =/date.timezone = Asia\/Shanghai/g' /usr/local/php7/php.ini
sed -i "s/mysqli.default_socket =/mysqli.default_socket = \/usr\/local\/mysql\/mysql.sock/g" /usr/local/php7/php.ini
# 链接php命令
create_symbolic_links "/usr/local/php7/bin/*" "/usr/local/bin/"
# 编辑httpd.conf文件
sed -i 's/DirectoryIndex index.html/DirectoryIndex index.php/g' "$HTTPD_CONF"
sed -i '396a\ AddType application/x-httpd-php .php\n AddType application/x-httpd-php-source .phps\n' "$HTTPD_CONF"
# 删除并创建新的index.php
rm -f "$HTTPD_HTDICT/index.html"
touch "$HTTPD_HTDICT/index.php"
echo '<?php phpinfo();?>' > "$HTTPD_HTDICT/index.php"
chmod 644 "$HTTPD_HTDICT/index.php"
systemctl restart httpd.service
# 安装unzip,解压文件并复制到目标目录
sudo yum install unzip -y
unzip -f "$UNZIP_FILE" -d "$DIS_DIR"
cp -r /opt/dis/dir_SC_UTF8/upload/ /usr/local/httpd/htdocs/bbs
# 安全地设置脚本和目录权限
chmod 755 "$MYSQL_INIT_SH"
chmod -R 755 "$DIS_DIR"
chown -R daemon "$BBS_DIR/config"
chown -R daemon "$BBS_DIR/data"
chown -R daemon "$BBS_DIR/uc_client"
chown -R daemon "$BBS_DIR/uc_server/data"
}
install_packages \
"gd libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel curl curl-devel openssl openssl-devel"
extract_tarball "/opt/php-7.1.24.tar.gz"
configure_and_make "/opt/php-7.1.24" \
"--prefix=/usr/local/php7 \
--with-apxs2=/usr/local/httpd/bin/apxs \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-config-file-path=/usr/local/php7 \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer"
configure_php
systemctl restart httpd
安装论坛
创建数据库脚本
bash
#!/usr/bin/expect
set timeout 30
set password "123456"
spawn mysql -uroot -p
expect {
"*password*" {send "$password\r"}
}
expect ">*"
send "CREATE DATABASE bbs;\r"
expect ">*"
send "GRANT all ON bbs.* TO 'bbsuser'@'%' IDENTIFIED BY 'admin123';\r"
expect ">*"
send "flush privileges;\r"
expect ">*"
send "quit\r"
expect eof