1、简介
使用shell脚本安装lnmp,纯粹是偷懒,平时安装一些东西都写成脚本了,方便以后在其他机器安装的时候不用再去查找文档。
PHP版本5.6.6
MYSQL版本5.6.26
NGINX版本1.15.6
2、环境说明
阿里云ECS(1G1核)CentOS 7.4 64位
3、shell脚本
3.1 cnl_function.sh
01 | #!/bin/bash |
02 | #chennailuan's function |
03 |
04 | #check last command id Ok or not. |
05 | check_ok(){ |
06 | if [ $? != 0 ] |
07 | then |
08 | echo Error,Check the error log. |
09 | exit 1 |
10 | fi |
11 | } |
12 |
13 | #if the packge installed ,then omit |
14 | myum(){ |
15 | if ! rpm -qa| grep -q "^$1" |
16 | then |
17 | yum install -y $1 |
18 | check_ok |
19 | else |
20 | echo $1 already installed. |
21 | fi |
22 | } |
23 |
24 | #check service is running or not ,example nginx ,httpd ,php-fpm |
25 | check_service(){ |
26 | if [ $1 == "phpfpm" ] |
27 | then |
28 | s= "php-fpm" |
29 | else |
30 | s=$1 |
31 | fi |
32 |
33 | n=` ps aux | grep $s | wc -l` |
34 | if [ $n - gt 1 ] |
35 | then |
36 | echo "$1 service is already started." |
37 | else |
38 | if [ -f /etc/init.d/$1 ] |
39 | then |
40 | /etc/init.d/$1 start |
41 | check_ok |
42 | else |
43 | install_$1 |
44 | fi |
45 | fi |
46 | } |
3.2 cnl_install_lnmp_init.sh
01 | #!/bin/bash |
02 | source ./cnl_function.sh |
03 |
04 | echo "It will install lamp=========================================================================================begin" |
05 | #sleep 2 |
06 |
07 | #get the archive of the system ,i686 or x86_64 |
08 | ar=`arch` |
09 |
10 | #close selinux |
11 | sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config |
12 | selinux_s=`getenforce` |
13 | if [ $selinux_s == "enforcing" ] |
14 | then |
15 | setenforce 0 |
16 | fi |
17 |
18 | #install some packges |
19 | for p in gcc wget perl perl-devel libaio libaio-devel pcre-devel zlib-devel autoconf openssl openssl-devel |
20 | do |
21 | myum $p |
22 | done |
23 |
24 | #install epel. |
25 | if rpm -qa epel-release > /dev/null |
26 | then |
27 | rpm -e epel-release |
28 | fi |
29 | if ls /etc/yum.repos.d/epel-7.repo* > /dev/null 2>&1 |
30 | then |
31 | rm -f /etc/yum.repos.d/epel-7.repo* |
32 | fi |
33 | wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/epel-7.repo |
3.3 cnl_install_lnmp.sh
001 | #!/bin/bash |
002 | source ./cnl_function.sh |
003 | source ./cnl_install_lnmp_init.sh |
004 |
005 | #function of installing mysqld |
006 | install_mysqld(){ |
007 | cd /usr/ local /src |
008 | [ -f mysql-5.6.26-linux-glibc2.5-$ar. tar .gz ] || wget http://cdn.mysql.com/archives/mysql-5.6/mysql-5.6.26-linux-glibc2.5-$ar. tar .gz |
009 | check_ok |
010 | tar -zxf mysql-5.6.26-linux-glibc2.5-$ar. tar .gz |
011 | check_ok |
012 | [ -d /usr/ local /mysql ] && mv /usr/ local /mysql /usr/ local /mysql_` date +%s` |
013 | mv mysql-5.6.26-linux-glibc2.5-$ar /usr/ local /mysql |
014 | check_ok |
015 | if ! grep '^mysql:' /etc/ passwd |
016 | then |
017 | useradd -M mysql -s /sbin/nologin |
018 | fi |
019 | myum compat-libstdc++-33 |
020 | check_ok |
021 | [ -d /data/mysql ] && mv /data/mysql /data/mysql_` date +%s` |
022 | mkdir -p /data/mysql |
023 | chown -R mysql:mysql /data/mysql |
024 | cd /usr/ local /mysql |
025 | ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql |
026 | check_ok |
027 | cp support-files/my-default.cnf /etc/my.cnf |
028 | check_ok |
029 | sed -i '/^\[mysqld\]$/a\datadir = /data/mysql' /etc/my.cnf |
030 | cp support-files/mysql.server /etc/init.d/mysqld |
031 | sed -i 's#^datadir=#datadir=/data/mysql#' /etc/init.d/mysqld |
032 | chmod 755 /etc/init.d/mysqld |
033 | chkconfig --add mysqld |
034 | chkconfig mysqld on |
035 | service mysqld start |
036 | check_ok |
037 | } |
038 |
039 | #function of install nginx |
040 | install_nginx(){ |
041 | cd /usr/ local /src |
042 | [ -f nginx-1.15.6. tar .gz ] || wget http://nginx.org/download/nginx-1.15.6. tar .gz |
043 | tar -zxf nginx-1.15.6. tar .gz |
044 | cd nginx-1.15.6 |
045 | myum pcre-devel |
046 | [ -d /usr/ local /nginx ] && cp -R /usr/ local /nginx /usr/ local /nginx_` date +%s` |
047 | check_ok |
048 | ./configure \ |
049 | --prefix=/usr/ local /nginx \ |
050 | --with-http_stub_status_module \ |
051 | --with-http_ssl_module \ |
052 | --with-ipv6 \ |
053 | --with-http_v2_module \ |
054 | --with-poll_module \ |
055 | --with-http_realip_module \ |
056 | --with-http_sub_module \ |
057 | --with-http_gzip_static_module \ |
058 | --with-http_dav_module \ |
059 | --with-http_flv_module |
060 | make && make install |
061 | check_ok |
062 | if [ -f /etc/init.d/nginx ] |
063 | then |
064 | mv /etc/init.d/nginx /etc/init.d/nginx_` date +%s` |
065 | fi |
066 | curl https://cnlpublic.nl166.com/cnlfile/nginx/.nginx_init -o /etc/init.d/nginx |
067 | check_ok |
068 | chmod 755 /etc/init.d/nginx |
069 | chkconfig --add nginx |
070 | chkconfig nginx on |
071 | curl https://cnlpublic.nl166.com/cnlfile/nginx/.nginx_conf -o /usr/ local /nginx/conf/nginx.conf |
072 | check_ok |
073 | if ! grep -q '^www:' /etc/ passwd |
074 | then |
075 | useradd -M -s /sbin/nologin www |
076 | fi |
077 |
078 | service nginx start |
079 | check_ok |
080 | echo -e "<?php \n phpinfo(); \n ?>" > /usr/ local /nginx/html/index.php |
081 | check_ok |
082 | } |
083 |
084 | #function of install php-fpm version 5.6 |
085 | install_phpfpm(){ |
086 | cd /usr/ local /src/ |
087 | [ -f php-5.6.6. tar .gz ] || wget http://mirrors.sohu.com/php/php-5.6.6. tar .gz |
088 | tar -zxf php-5.6.6. tar .gz && cd php-5.6.6 |
089 | for p in openssl-devel bzip2 -devel \ |
090 | libxml2-devel curl-devel libpng-devel libjpeg-devel \ |
091 | freetype-devel libmcrypt-devel libtool-ltdl-devel perl-devel |
092 | do |
093 | myum $p |
094 | done |
095 |
096 | if ! grep -q '^www:' /etc/ passwd |
097 | then |
098 | useradd -M -s /sbin/nologin www |
099 | fi |
100 | check_ok |
101 | ./configure \ |
102 | --prefix=/usr/ local /php-fpm \ |
103 | --with-config- file -path=/usr/ local /php-fpm/etc \ |
104 | -- enable -fpm \ |
105 | --with-fpm-user=www \ |
106 | --with-fpm-group=www \ |
107 | --with-mysql=/usr/ local /mysql \ |
108 | --with-mysql-sock=/tmp/mysql.sock \ |
109 | --with-pdo-mysql \ |
110 | --with-pdo-sqlite \ |
111 | --with-libxml- dir \ |
112 | --with-gd \ |
113 | --with-gettext \ |
114 | --with-jpeg- dir \ |
115 | --with-png- dir \ |
116 | --with-freetype- dir \ |
117 | --with-iconv-div \ |
118 | --with-zlib- dir \ |
119 | --with-mcrypt \ |
120 | -- enable -soap \ |
121 | -- enable -gd-native-ttf \ |
122 | -- enable - ftp \ |
123 | -- enable -mbstring \ |
124 | -- enable -exif \ |
125 | -- enable -sockets \ |
126 | --disable-ipv6 \ |
127 | --with-pear \ |
128 | --with-curl \ |
129 | --with-mysqli \ |
130 | --with-openssl |
131 | check_ok |
132 | make && make install |
133 | check_ok |
134 | [ -f /usr/ local /php-fpm/etc/php.ini ] || cp php.ini-production /usr/ local /php-fpm/etc/php.ini |
135 | if /usr/ local /php-fpm/bin/php -i || grep -iq 'date.timezone => no value' |
136 | then |
137 | sed -i '/;date.timezone =$/a\date.timezone = "PRC"' /usr/ local /php-fpm/etc/php.ini |
138 | check_ok |
139 | fi |
140 | [ -f /usr/ local /php-fpm/etc/php-fpm.conf ] || curl https://cnlpublic.nl166.com/cnlfile/php/.phpfpm_conf -o /usr/ local /php-fpm/etc/php-fpm.conf |
141 | [ -f /etc/init.d/phpfpm ] || cp sapi/fpm/init.d.php-fpm /etc/init.d/phpfpm |
142 |
143 | chmod 755 /etc/init.d/phpfpm |
144 | chkconfig phpfpm on |
145 | ln -s /usr/ local /php-fpm/bin/php /usr/ local /bin/php |
146 | service phpfpm start |
147 | check_ok |
148 | } |
149 |
150 | #function of install lnmp |
151 | lnmp(){ |
152 | check_service mysqld |
153 | check_service nginx |
154 | check_service phpfpm |
155 | echo "The lnmp done,Please use 'http://your ip/index.php' to access" |
156 | } |
157 |
158 | read -p "Initialization completion, Enter (Y) to start installation LNMP :" n |
159 | if [ $n == 'Y' ] |
160 | then |
161 | echo "Start installation==============================================================================================================================>" |
162 | lnmp |
163 | else |
164 | echo "Cancel the installation." |
165 | fi |
4、开始安装
上面上个文件放在同一目录并都给予执行(x)权限
在shell目录执行 sh cnl_install_lnmp.sh
输入 Y 确认执行安装,需要安装的安装包会自己检查,本人在自己的几台服务器都测试过,安装正常。
安装完会自己加到系统服务 ,并启动。
写的文章仅供自己参考,仅供自己参考,仅供自己参考,免得太久没有使用忘记了。