最近自学了下Shell编程,也算是入门吧!按照如下教程,编译安装了LAMP(Apache-2.4.6 + MySQL-5.5.25 + PHP-5.3.27)
CentOS6.3编译安装LAMP(2):编译安装 Apache2.2.25
CentOS6.3编译安装LAMP(2):编译安装 Apache2.4.6
CentOS6.3编译安装LAMP(3):编译安装 MySQL5.5.25
CentOS6.3编译安装LAMP(4):编译安装 PHP5.2.17
CentOS6.3编译安装LAMP(4):编译安装 PHP5.3.27
在生产环境上,如果按着上述教程一个一个地去安装,势必有些麻烦,于是萌发了一个念头:能不能把这些Shell脚本汇总到一个脚本中,实现自动化安装呢?答案是可以的。
【个人专用&入门级】LAMP一键安装包
软件版本:
Apache-2.4.6 + MySQL-5.5.25 + PHP-5.3.27
运行环境:
目前仅在CentOS-5.5(32位)上试验并编译通过,后续会在CentOS6.3上试验下,包括64位系统。
源码下载:
放在了百度云盘,点击进入下载页面,需要下载的文件及文件夹清单如下:
Apache-2.4.6
MySQL-5.5.25
PHP-5.3.27
phpMyAdmin-3.5.8
Config
Shell
lamp.sh
把它们上传到Linux主机的某个目录下即可,如 /usr/local/src 目录
运行方式:
chmod +x ./lamp.sh && ./lamp.sh
部分Shell脚本预览
lamp.sh
#!/bin/bash PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH ################################################################### ## author: snsgou.com ## website: http://www/snsgou.com/lamp ## version: 0.1 ################################################################### #需要切换到root用户 if [[ $EUID -ne 0 ]]; then echo "This script must be run as root" exit 1 fi chmod -R 755 ./* CUR_PATH=$(pwd) CONFIG_PATH="$CUR_PATH/Config" SHELL_PATH="$CUR_PATH/Shell" action=$1 [[ -z $1 ]] && action=install case "$action" in install) source $SHELL_PATH/lamp_install.sh ;; uninstall) source $SHELL_PATH/lamp_uninstall.sh ;; add) source $SHELL_PATH/vhost_add.sh ;; del) source $SHELL_PATH/vhost_del.sh ;; esac
Shell/lamp_install.sh
#!/bin/bash PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH ################################################################### ## author: snsgou.com ## website: /article/detail/702 ## version: 0.1 ################################################################### #当前路径检测 if [[ -z $CUR_PATH ]]; then echo '$CUR_PATH is not defined' exit 1 fi ################################################################### ## 全局变量 ################################################################### #网站目录(暂不支持修改) SITE_PATH='/var/www' #Apache2.4的mpm选择(可修改) # prefork (Recommend); worker (Multi-threaded); event (Multi-threaded,not support ssl.) Apache_MPM=prefork #MySQL的root密码(可修改) MySQL_ROOT_PWD='123456' #MySQL的data目录(暂不支持修改) MySQL_DATA_PATH='/usr/local/mysql/data' #设置时区 rm -rf /etc/localtime cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime ################################################################### ## 准备工作 ################################################################### #卸载已存在的rpm包 rpm -e httpd rpm -e mysql rpm -e php yum -y remove httpd yum -y remove mysql-server mysql yum -y remove php yum -y remove php-mysql yum -y install yum-fastestmirror #selinux可能会致使编译安装失败,我们先禁用它 setenforce 0 #yum安装必要工具 yum -y install gcc gcc-c++ kernel-devel ntp vim-enhanced flex bison autoconf make automake bzip2-devel ncurses-devel zlib-devel libjpeg-devel libpng-devel libtiff-devel freetype-devel libXpm-devel gettext-devel pam-devel libtool libtool-ltdl openssl openssl-devel fontconfig-devel libxml2-devel curl-devel libicu libicu-devel libmcrypt libmcrypt-devel libmhash libmhash-devel pcre-devel libtool-libs gd file patch mlocate diffutils readline-devel glibc-devel glib2-devel libcap-devel ################################################################### ## 安装 Apache-2.4 ################################################################### #安装 apr cd $CUR_PATH/Apache-2.4.6 rm -rf ./apr-1.4.6 tar -xzvf ./apr-1.4.6.tar.gz cd ./apr-1.4.6 mkdir /usr/local/apr ./configure --prefix=/usr/local/apr make && make install #安装 apr-util cd $CUR_PATH/Apache-2.4.6 rm -rf ./apr-util-1.4.1 tar -xzvf ./apr-util-1.4.1.tar.gz cd ./apr-util-1.4.1 mkdir /usr/local/apr-util ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/bin/apr-1-config make && make install #安装 pcre cd $CUR_PATH/Apache-2.4.6 rm -rf ./pcre-8.33 tar -xzvf ./pcre-8.33.tar.gz cd ./pcre-8.33 mkdir /usr/local/pcre ./configure --prefix=/usr/local/pcre --with-apr=/usr/local/apr/bin/apr-1-config make && make install #切换到Apache源码目录 cd $CUR_PATH/Apache-2.4.6 rm -rf ./httpd-2.4.6 tar -xzvf ./httpd-2.4.6.tar.gz cd ./httpd-2.4.6 #生成configure ./configure --prefix=/usr/local/apache --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-pcre=/usr/local/pcre --enable-so --enable-deflate=shared --enable-expires=shared --enable-ssl=shared --enable-headers=shared --enable-rewrite=shared --enable-static-support --with-mpm=$Apache_MPM #编译 make && make install #使用init脚本管理httpd #cp -f ./build/rpm/httpd.init /etc/init.d/httpd cp -f $CONFIG_PATH/httpd-2.4.6.init /etc/init.d/httpd #增加执行权限 chmod 755 /etc/init.d/httpd #添加httpd到服务项,并设置开机启动 chkconfig --add httpd chkconfig --level 2345 httpd on #chkconfig --list httpd #查看是否设置成功 #建立httpd的软链接,到时候,Apache的配置文件路径为 /etc/httpd/conf/httpd.conf, #其实真实路径为 /usr/local/apache/conf/httpd.conf rm -rf /etc/httpd mkdir /etc/httpd ln -sf /usr/local/apache/conf /etc/httpd/conf #设置软链接以适应init脚本 ln -sf /usr/local/apache/bin/httpd /usr/sbin/httpd ln -sf /usr/local/apache/bin/apachectl /usr/sbin/apachectl rm -rf /var/log/httpd/ ln -s /usr/local/apache/logs /var/log/httpd #添加apache用户组及用户 groupadd apache useradd -g apache -s /usr/sbin/nologin apache chown -R apache:apache /usr/local/apache #新建网站目录 rm -rf $SITE_PATH/html mkdir -p $SITE_PATH chmod -R 755 $SITE_PATH cp -f $CONFIG_PATH/httpd-vhosts-2.4.6.conf /etc/httpd/conf/extra/httpd-vhosts.conf mkdir -p /usr/local/apache/conf/vhost touch /usr/local/apache/conf/vhost/none.conf cp -f $CONFIG_PATH/httpd-2.4.6.conf /usr/local/apache/conf/httpd.conf cp -f $CONFIG_PATH/index.html $SITE_PATH/index.html cp -f $CONFIG_PATH/p.php $SITE_PATH/p.php cp -f $CONFIG_PATH/phpinfo.php $SITE_PATH/phpinfo.php #启动Apache #service httpd start echo "############################# Apache-2.4 install completed #############################" ################################################################### ## 安装 MySQL-5.5 ################################################################### #安装cmake cd $CUR_PATH/MySQL-5.5.25 rm -rf ./cmake-2.8.8 tar -xzvf ./cmake-2.8.8.tar.gz cd ./cmake-2.8.8 ./configure make && make install #解压MySQL cd $CUR_PATH/MySQL-5.5.25 rm -rf ./mysql-5.5.25 tar -xzvf ./mysql-5.5.25.tar.gz cd ./mysql-5.5.25 mkdir -p $MySQL_DATA_PATH groupadd mysql useradd -g mysql -s /usr/sbin/nologin mysql #cmake编译 cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=$MySQL_DATA_PATH -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_unicode_ci -DWITH_READLINE=1 -DWITH_SSL=system -DWITH_EMBEDDED_SERVER=1 -DENABLED_LOCAL_INFILE=1 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_DEBUG=0 #安装 make && make install #复制配置文件 #cp ./support-files/my-medium.cnf /etc/my.cnf cp -f $CONFIG_PATH/my-5.5.25.cnf /etc/my.cnf #设置权限 chmod +x /usr/local/mysql chmod +w /usr/local/mysql chown -R mysql:mysql /usr/local/mysql #添加MySQL的软链接以适应init脚本 ln -sv /usr/local/mysql/bin/mysql /usr/sbin/mysql ln -sv /usr/local/mysql/bin/mysqladmin /usr/sbin/mysqladmin ln -sv /usr/local/mysql/bin/mysqldump /usr/sbin/mysqldump #初始化数据库 /usr/local/mysql/scripts/mysql_install_db --defaults-file=/etc/my.cnf --user=mysql --basedir=/usr/local/mysql --datadir=$MySQL_DATA_PATH #配置开机自动启动 cp -f ./support-files/mysql.server /etc/init.d/mysqld chmod +x /etc/init.d/mysqld chkconfig --add mysqld chkconfig --level 2345 mysqld on #chkconfig --list mysqld #查看是否已应用上 #启动MySQL service mysqld start #设置root密码 /usr/local/mysql/bin/mysqladmin password $MySQL_ROOT_PWD mysql -uroot -p$MySQL_ROOT_PWD <<EOF drop database test; delete from mysql.user where user=''; update mysql.user set password=password('$MySQL_ROOT_PWD') where user='root'; delete from mysql.user where not (user='root') ; flush privileges; exit EOF echo "############################# MySQL-5.5 install completed #############################" ################################################################### ## 安装 PHP-5.3 ################################################################### #安装 libmcrypt cd $CUR_PATH/PHP-5.3.27 rm -rf ./libmcrypt-2.5.8 tar -xzvf ./libmcrypt-2.5.8.tar.gz cd ./libmcrypt-2.5.8 ./configure --prefix=/usr make && make install #安装 mhash cd $CUR_PATH/PHP-5.3.27 rm -rf ./mhash-0.9.9.9 tar -xzvf ./mhash-0.9.9.9.tar.gz cd ./mhash-0.9.9.9 ./configure --prefix=/usr make && make install #安装 mcrypt /sbin/ldconfig #搜索出可共享的动态链接库 cd $CUR_PATH/PHP-5.3.27 rm -rf ./mcrypt-2.6.8 tar -xzvf ./mcrypt-2.6.8.tar.gz cd ./mcrypt-2.6.8 ./configure make && make install #安装 libiconv cd $CUR_PATH/PHP-5.3.27 rm -rf ./libiconv-1.14 tar -xzvf ./libiconv-1.14.tar.gz cd ./libiconv-1.14 ./configure --prefix=/usr/local/libiconv make && make install #安装 PHP-5.3.27 cd $CUR_PATH/PHP-5.3.27 rm -rf ./php-5.3.27 tar -xzvf ./php-5.3.27.tar.gz cd ./php-5.3.27 ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache/bin/apxs --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-openssl --with-zlib --with-curl --with-bz2 --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-mcrypt --with-mhash --with-gettext --with-iconv=/usr/local/libiconv --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-pdo-mysql=/usr/local/mysql --enable-gd-native-ttf --enable-mbstring --enable-zip --enable-ftp --enable-soap --enable-sockets --enable-bcmath --without-pear make && make install #复制配置文件php.ini #cp ./php.ini-development /etc/php.ini cp -f $CONFIG_PATH/php-5.3.27.ini /etc/php.ini mkdir /etc/php.d #重启Apache #service httpd restart #添加SELINUX规则 chcon -c -v -R -u system_u -r object_r -t textrel_shlib_t /usr/local/apache/modules/libphp5.so echo "############################# PHP-5.3 install completed #############################" ################################################################### ## 安装 phpMyAdmin-3.5.8 ################################################################### cd $CUR_PATH/phpMyAdmin-3.5.8 rm -rf ./phpMyAdmin-3.5.8.2-all-languages tar -xzvf ./phpMyAdmin-3.5.8.2-all-languages.tar.gz mv ./phpMyAdmin-3.5.8.2-all-languages $SITE_PATH/phpmyadmin chmod -R 755 $SITE_PATH/phpmyadmin chown -R apache:apache $SITE_PATH/phpmyadmin cp -f $CONFIG_PATH/phpMyAdmin-3.5.8-config.inc.php $SITE_PATH/phpmyadmin/config.inc.php #启动Apache service httpd start echo "############################# phpMyAdmin-3.5.8 install completed #############################" ################################################################### ## 更改网站目录权限 ################################################################### chown -R apache:apache /usr/local/apache chown -R apache:apache $SITE_PATH chmod -R 755 $SITE_PATH
整个过程编译下来,大概花了 1小时20分钟!