- 先把各种包和源安装好
[root@localhost ~]# mkdir lamp 创建一个lamp目录
[root@localhost ~]# ls
anaconda-ks.cfg lamp
[root@localhost ~]# cd lamp
[root@localhost lamp]# mkdir soft 创建一个目录,方便放入所有的安装包
[root@localhost lamp]# touch install.sh 创建一个脚本
[root@localhost lamp]# chmod +x install.sh
[root@localhost lamp]# ls
install.sh soft
[root@localhost soft]# cp CentOS-Base.repo /etc/yum.repos.d/ 把网络源复制过来方便使用
[root@localhost soft]# ls
apr-1.6.5.tar.gz
apr-util-1.6.1.tar.gz
CentOS-Base.repo
httpd-2.4.46.tar.bz2
mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
php-7.2.8.tar.xz
[root@localhost soft]# curl -o epel-release-latest-8.noarch.rpm https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm 下载epel源
[root@localhost soft]# ls
apr-1.6.5.tar.gz
apr-util-1.6.1.tar.gz
CentOS-Base.repo
epel-release-latest-8.noarch.rpm
httpd-2.4.46.tar.bz2
mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
php-7.2.8.tar.xz
[root@localhost soft]# rpm -Uvh epel-release-latest-8.noarch.rpm
- 写脚本
[root@localhost lamp]# vim install.sh
#!/bin/bash
kernel_count=$(grep processor /proc/cpuinfo |wc -l)
apache_dir=/usr/local/apache
mysql_untar=$(echo mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz|awk -F'.tar' '{print $1}')
mysql_basedir=/usr/local
datadir=/opt/data
read -p "请输入您的密码:" mysql_pass
mv /etc/yum.repos.d/* /tmp/
cp soft/CentOS-Base.repo /etc/yum.repos.d/
rpm -qa|grep epel &>/dev/null
if [ $? -eq 0 ];then
yum -y reinstall soft/epel-release-latest-8.noarch.rpm
else
yum -y install soft/epel-release-latest-8.noarch.rpm
fi
sed -i 's|^#baseurl=https://download.fedoraproject.org/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
sed -i 's/\$releasever/8/g' /etc/yum.repos.d/epel*
yum -y groups mark install "Development Tools" &>/dev/null
yum -y install make openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ ncurses-devel openssl cmake mariadb-devel libxml2 libxml2-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel pcre-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php-mysqlnd ncurses-compat-libs
id apache &>/dev/null
if [ $? -ne 0 ];then
useradd -r -M -s /sbin/nologin apache
fi
rm -rf apr-1.6.5 apr-util-1.6.1 httpd-2.4.46
tar xf soft/apr-1.6.5.tar.gz
tar xf soft/apr-util-1.6.1.tar.gz
tar xf soft/httpd-2.4.46.tar.bz2
cd apr-1.6.5
sed -i '/$RM "$cfgfile"/d' configure
if [ ! -d /usr/local/apr ];then
./configure --prefix=/usr/local/apr && \
make -j $kernel_count && make install
fi
cd ../apr-util-1.6.1
if [ ! -d /usr/local/apr-util ];then
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make -j $kernel_count && make install
fi
cd ../httpd-2.4.46
if [ ! -d $apache_dir ];then
./configure --prefix=$apache_dir \
--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 && \
make -j $kernel_count && make install
echo "export PATH=$apache_dir/bin:\$PATH" > /etc/profile.d/httpd.sh
ln -s $apache_dir/include /usr/include/httpd
sed -i '#ServerName/s/#//g'/etc/httpd24/httpd.conf
$apache_dir/bin/apachectl start
fi
cd ..
id mysql &>/dev/null
if [ $? -ne 0 ];then
useradd -r -M -s /sbin/nologin mysql
fi
if [ ! -d $mysql_basedir/$mysql_untar ];then
read -p "请输入mysql密码:" mysql_pass
echo "正在解压$mysql_untar.tar.gz"
tar xf soft/$mysql_untar.tar.gz -C $mysql_basedir && \
ln -s $mysql_basedir/$mysql_untar $mysql_basedir/mysql &>/dev/null
chown -R mysql.mysql $mysql_basedir/mysql*
echo "export PATH=$mysql_basedir/mysql/bin:\$PATH" > /etc/profile.d/mysql.sh
mkdir -p $datadir
chown -R mysql.mysql $datadir
$mysql_basedir/mysql/bin/mysqld --initialize-insecure --datadir=$datadir --user=mysql && \
ln -s $mysql_basedir/mysql/include /usr/include/mysql &>/dev/null
echo "$mysql_basedir/mysql/lib" > /etc/ld.so.conf.d/mysql.conf
ldconfig
cat > /etc/my.cnf <<EOF
[mysqld]
basedir = $mysql_basedir/mysql
datadir = $datadir
socket = /tmp/mysql.sock
port = 3306
pid-file = $datadir/mysql.pid
user = mysql
skip-name-resolve
EOF
cp -a $mysql_basedir/mysql/support-files/mysql.server /etc/init.d/mysqld
sed -ri "s#^(basedir=).*#\1$mysql_basedir/mysql#g" /etc/init.d/mysqld
sed -ri "s#^(datadir=).*#\1$datadir#g" /etc/init.d/mysqld
service mysqld start
sleep 5
$mysql_basedir/mysql/bin/mysql -uroot -e "set password=password('$mysql_pass');"
fi
tar xf soft/php-7.2.8.tar.xz
cd php-7.2.8
if [ ! -d /usr/local/php7 ];then
./configure --prefix=/usr/local/php7 \
--with-config-file-path=/etc \
--enable-fpm \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-soap \
--with-openssl \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--enable-exif \
--enable-ftp \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-zlib-dir \
--with-freetype-dir \
--with-gettext \
--enable-json \
--enable-mbstring \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-readline \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--enable-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-pcntl \
--enable-posix && \
make -j $kernel_count && make install
echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php.sh
mv /etc/php.ini{,-bak}
cp php.ini-production /etc/php.ini
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod a+x /etc/init.d/php-fpm
cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf
service php-fpm start
fi
sed -i '/proxy_module/s/#//g' /etc/httpd24/httpd.conf
sed -i '/proxy_fcgi_module/s/#//g' /etc/httpd24/httpd.conf
vhosts_status=$(grep "Include .*vhosts.conf" /etc/httpd24/httpd.conf |grep -v '^#'|wc -l)
if [ $vhosts_status -eq 0 ];then
echo "Include /etc/httpd24/extra/vhosts.conf" >> /etc/httpd24/httpd.conf
fi
cat > /etc/httpd24/extra/vhosts.conf << EOF
<VirtualHost *:80>
DocumentRoot "$apache_dir/htdocs"
ServerName www.example.com
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000$apache_dir/htdocs/$1
<Directory "$apache_dir/htdocs>
Require all granted
</Directory>
</VirtualHost>
EOF
cat > $apache_dir/htdocs/index.php <<EOF
<?php
phpinfo();
?>
EOF
chown -R apache.apache $apache_dir/htdocs
sed -i '/AddType application\/x-gzip .gz .tgz/aAddType application/x-httpd-php-source .php' /etc/httpd24/httpd.conf
sed -i '/AddType application\/x-gzip .gz .tgz/aAddType application/x-httpd-php .php' /etc/httpd24/httpd.conf
sed -i '/ DirectoryIndex/s/index.html/index.php index.html/g' /etc/httpd24/httpd.conf
$apache_dir/bin/apachectl restart
service php-fpm restart
service mysqld restart
[root@localhost lamp]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 127.0.0.1:9000 0.0.0.0:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 80 *:3306 *:*
[root@localhost lamp]# mysql -uroot -p123 输入新密码123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.31 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
用192.168.50.137访问网站