闲来无事自己写了个LNMP环境的安装脚本。
该脚本可以单独安装nginx,mysql,php,也可以一步到位直接安装所有软件。其中,nginx安装完,需根据具体环境修改配置文件,再启动服务。mysql,php安装完服务自动启动。
环境:
软件安装路径:/opt/apps/
数据存放目录:/opt/data/
安装包下载目录:/opt/src/
具体软件版本如下:
nginx-1.10.1
mysql-5.6.32
php-7.0.10
脚本如下:
# more lnmp_install.sh #!/bin/sh ##############################defind functions that install lnmp....###################### #######nginx########################## Install_nginx () { echo -e "\033[33m Install nginx....\033[0m" if [ -d /opt/apps/nginx/ ]; then echo -e "\033[31m Nginx already exists in your system......\033[0m" exit fi echo -e "\033[33m Install base environment of nginx......\033[0m" yum -y install pcre* openssl* echo -e "\033[33m Download nginx(1.10.1)......\033[0m" if [ ! -f /opt/src/nginx-1.10.1.tar.gz ]; then wget http://nginx.org/download/nginx-1.10.1.tar.gz -P /opt/src fi cd /opt/src/ tar -zxvf nginx-1.10.1.tar.gz cd nginx-1.10.1 echo -e "\033[33m Compose nginx......\033[0m" ./configure --prefix=/opt/apps/nginx --conf-path=/opt/conf/nginx/nginx.conf --error-log-path=/opt/logs/nginx/error.log --http-log-path=/opt/logs/nginx/access.log --with-http_stub_status_module --with-http_gzip_static_module --with-ht tp_flv_module --with-http_ssl_module --with-http_realip_module --http-client-body-temp-path=/opt/apps/nginx/client_body_temp --http-fastcgi-temp-path=/opt/apps/nginx/fastcgi_temp --http-proxy-temp-path=/opt/apps/nginx/proxy_temp --http- uwsgi-temp-path=/opt/apps/nginx/uwsgi_temp --http-scgi-temp-path=/opt/apps/nginx/scgi_temp make && make install if [ $? -eq "0" ]; then echo -e "\033[32m Nginx is installed successfully......\033[0m" exit else echo -e "\033[31m Something was wrong during installing nginx,please check and try again......\033[0m" exit fi } ######mysql############################ Install_mysql () { #Removing default rpm packages of mysql if they have been installed.... if [ -f /usr/bin/mysqld_safe ];then echo -e "\033[33m Remove default mysql-server RPM packages......\033[0m" yum -y remove mysql-server fi if [ -f /usr/bin/mysqldump ];then echo -e "\033[33m Remove default mysql-client RPM packages......\033[0m" yum -y remove mysql fi #Adding mysql‘s user and group.. if [ ! -d /opt/data/mysql];then mkdir -p /opt/data/mysql fi echo -e "\033[33m Setting Environment variables......\033[0m" userdel -r mysql groupdel -r mysql groupadd mysql useradd -r -g mysql -s /sbin/nologin mysql #setting the env of mysql...... base_dir=/opt/apps/mysql/ data_dir=/opt/data/mysql/ src_dir=/opt/src/ #Download mysql and install if [ ! -d /opt/src/mysql-5.6.32/ ];then if [ ! -f /opt/src/mysql-5.6.32.tar.gz ];then wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.32.tar.gz -P /opt/src/ fi cd /opt/src/ tar -zxvf /opt/src/mysql-5.6.32.tar.gz fi cd /opt/src/mysql-5.6.32 cmake . -DCMAKE_INSTALL_PREFIX=${base_dir} -DMYSQL_DATADIR=${data_dir} -DMYSQL_UNIX_ADDR=${data_dir}mysql.sock -DWITH_INNOBASE_STORAGE_ENGINE=1 -DMYSQL_TCP_PORT=3306 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_g eneral_ci -DWITH_DEBUG=0 if [ "$?" -ne "0" ];then echo -e ‘Configure mysql failure,Please check compile Environment...‘ exit 2 fi make -j 3 if [ "$?" -ne "0" ];then echo ‘Error,please look config.log for more information...‘ exit 2 fi make install if [ "$?" -eq "0" ];then echo -e ‘Mysql install complete done...‘ else echo -e ‘Something wrong between Installation...‘ fi #Initialization mysql.. echo -e ‘Initialization mysql...‘ cd ${src_dir}mysql-5.6.32 cp support-files/my-default.cnf ${data_dir}/my.cnf cp support-files/mysql.server /etc/init.d/mysqld sed -i -e ‘46s/basedir=/basedir=${base_dir}/‘ -e ‘47s/datadir=/datadir=${data_dir}/‘ /etc/init.d/mysqld chmod a+x /etc/init.d/mysqld chkconfig --add mysqld sed -i -e "s#@[email protected]#hostname#" ./scripts/mysql_install_db.sh sh ./scripts/mysql_install_db.sh --user=mysql --basedir=${base_dir} --datadir=${data_dir} > /dev/null 2>&1 chown -R mysql.mysql /opt/apps/mysql/ chown -R mysql.mysql /opt/data/mysql/ #Start mysql... ulimit -s unlimited service mysqld start } #######php########################################################### Install_php () { if [ ! -f /opt/src/php-7.0.10.tar.gz ];then echo -e "\033[33m Download php-7.0.10.tar.gz......\033[0m" wget http://cn2.php.net/get/php-7.0.10.tar.gz/from/this/mirror -P /opt/src cd /opt/src && mv mirror php-7.0.10.tar.gz fi echo -e "\033[33m Complising and installing php-7.0.10 ......\033[0m" tar -zxvf php-7.0.10.tar.gz cd php-7.0.10 yum install -y php-mcrypt libmcrypt libmcrypt-devel libjpeg-turbo-devel.x86_64 ./configure --prefix=/opt/apps/php --with-config-file-path=/opt/apps/php/etc --with-mcrypt=/usr/include --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-gd --with-iconv --with-zlib --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-mbregex --enable-fpm --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --w ithout-pear --with-gettext --enable-session --with-curl --with-jpeg-dir --with-freetype-dir --enable-opcache if [ $? -eq "0"];then echo -e "\033[31m Php install faild....Please check and try again....\033[0m" exit fi make && make install cp php.ini-production /opt/apps/php/etc/php.ini cp sapi/fpm/init.d.php-fpm /etc/init.d/php7-fpm cp /opt/apps/php/etc/php-fpm.conf.default /opt/apps/php/etc/php-fpm.conf cp /opt/apps/php/etc/php-fpm.d/www.conf.default /opt/apps/php/etc/php-fpm.d/www.conf echo "zend_extension=/opt/apps/php/lib/php/extensions/no-debug-non-zts-20151012/opcache.so" >> /opt/apps/php/etc/php.ini chmod 775 /etc/init.d/php7-fpm echo -e "\033[33m Starting php7-frm service......\033[0m" /etc/init.d/php7-fpm start if [ $? -eq "0" ];then echo -e "\032[33m PHP installed and started successfully......\033[0m" else: echo -e "\033[31m PHP installed successfully,but start failed,please check the configuration of php......\033[0m" fi } ###############################Main program###################################### mkdir -p /opt/{apps,conf,src,data} read -p "Which part of lnmp you want to install?(nginx|mysql|php|all):" softwar case ${softwar} in nginx) Install_nginx ;; mysql) Install_mysql ;; php) Install_php ;; all) if [ -d /opt/apps/nginx ];then echo -e "\033[33m Nginx was already installed in your system...\033[0m" else Install_nginx fi if [ -d /opt/apps/mysql ];then echo -e "\033[33m Mysql was already installed in your system...\033[0m" else Install_mysql fi if [ -d /opt/apps/php ];then echo -e "\033[33m PHP was already installed in your system...\033[0m" else Install_php fi ;; *) echo -e "\033[33m Sorry,please input a right choice\033[0m" esac
时间: 2024-10-07 12:31:51