Lamp 一键部署

注:记得把所需要的软件包,先放到/opt目录中;以及该机子的内存大小、核数先设置好!

#!/bin/bash

systemctl stop firewalld
systemctl disable firewalld
setenforce 0  &> /dev/null
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

read -p  "请输入web服务器ip:" IP
read -p "请输入对应的域名:" NAME
read -p "请输入服务器的核心数:" N

yum -y install gcc gcc-c++ make pcre pcre-devel expat-devel perl

cd /opt/
tar zxvf apr-1.6.2.tar.gz
tar zxvf apr-util-1.6.0.tar.gz
tar jxvf httpd-2.4.29.tar.bz2

mv apr-1.6.2 /opt/httpd-2.4.29/srclib/apr
mv apr-util-1.6.0 /opt/httpd-2.4.29/srclib/apr-util


cd /opt/httpd-2.4.29/
./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi

make -j$N && make install


ln -s /usr/local/httpd/conf/httpd.conf /etc/
ln -s /usr/local/httpd/bin/* /usr/local/bin/

cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
chmod +x /etc/init.d/httpd
sed -i "1i #!/bin/bash\n# chkconfig: 35 85 21 \n" /etc/init.d/httpd
chkconfig  --add httpd

cp -a /usr/local/httpd/conf /usr/local/httpd/conf.bak
cp -p /usr/local/httpd/conf/httpd.conf{,.bak}


sed -i  "s/Listen 80/Listen $IP:80/" /usr/local/httpd/conf/httpd.conf.bak
sed -i "197i ServerName $NAME:80" /usr/local/httpd/conf/httpd.conf.bak
/usr/bin/cp -fp /usr/local/httpd/conf/httpd.conf.bak  /usr/local/httpd/conf/httpd.conf

echo "$IP  $NAME" >> /etc/hosts
systemctl restart httpd

yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake

cd /opt/
tar zxvf mysql-5.7.17.tar.gz
tar zxvf boost_1_59_0.tar.gz


mv boost_1_59_0 /usr/local/boost
cd /opt/mysql-5.7.17/
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$N && make install

useradd -M -s /sbin/nologin  mysql

cat > /etc/my.cnf <<EOF
[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
EOF

chown -R mysql:mysql /usr/local/mysql/

chown mysql:mysql /etc/my.cnf

echo 'export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
source /etc/profile

cd /usr/local/mysql/bin/

./mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data

cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
systemctl daemon-reload
systemctl start mysqld.service
systemctl enable mysqld



yum -y install expect

/usr/bin/expect <<-EOF
spawn mysqladmin -u root -p password "abc123"
expect "Enter password:"
send "\n"
expect eof
EOF

mysql -u root -p"abc123" -e "grant all privileges on *.* to 'root'@'%' identified by 'abc123';"

yum -y install \
gd \
libjpeg libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel


cd /opt
tar jxvf php-7.1.10.tar.bz2
cd /opt/php-7.1.10/

./configure \
--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 \
--enable-zip


make -j$N && make install

cp /opt/php-7.1.10/php.ini-development /usr/local/php7/php.ini

sed -i 's#mysqli.default_socket =#mysqli.default_socket =/usr/local/mysql/mysql.sock#' /usr/local/php7/php.ini
sed -i '939a date.timezone =  Asia/Shanghai' /usr/local/php7/php.ini
ln -s /usr/local/php7/bin/* /usr/local/bin/

/usr/bin/cp  -fp /usr/local/httpd/conf/httpd.conf  /usr/local/httpd/conf/httpd.conf.bak

sed -i "393a AddType application/x-httpd-php .php\nAddType application/x-httpd-php-source .phps" /usr/local/httpd/conf/httpd.conf.bak

sed -i "s/DirectoryIndex index.html/DirectoryIndex index.html  index.php/ "  /usr/local/httpd/conf/httpd.conf.bak

/usr/bin/cp  -fp /usr/local/httpd/conf/httpd.conf.bak  /usr/local/httpd/conf/httpd.conf

systemctl restart httpd

mv /usr/local/httpd/htdocs/index.html /usr/local/httpd/htdocs/index.bak
echo -e "<?php \nphpinfo(); \n?> \n" > /usr/local/httpd/htdocs/index.php