debian下lnmp一键安装脚本

文章目录

使用dotdeb源在debian6中安装lnmp这篇文章里我曾经介绍了如何使用dotdeb源在debian6系统里安装lnmp,这样安装的lnmp环境非常的稳定。一直以来就想写个shell脚本这样不用再手动输入命令安装及进行各种优化。今天在这里就跟大家分享下我的成果。

已更新:debian中lnmp一键安装脚本完善版

前期工作

该脚本其实就是把前期的几篇文章总结和综合了一下。这里把这几篇文章列出来,也方便以后查找。
使用dotdeb源在debian6中安装lnmp
shell脚本的语法和使用
128m小内存vps优化
ubuntu12.04安装LNMP环境

脚本代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/bin/bash
#remove unneeded software
DEBIAN_FRONTEND=noninteractive apt-get -q -y remove --purge sendmail* apache2* samba* bind9* nscd
invoke-rc.d saslauthd stop
invoke-rc.d xinetd stop
update-rc.d saslauthd disable
update-rc.d xinetd disable
 
# add dotdeb source 
dv=$(cut -d. -f1 /etc/debian_version)
if [ "$dv" = "7" ]; then
   echo -e 'deb http://packages.dotdeb.org wheezy all' >> /etc/apt/sources.list
   echo -e 'deb-src http://packages.dotdeb.org wheezy all' >> /etc/apt/sources.list
elif [ "$dv" = "6" ]; then
   echo -e 'deb http://packages.dotdeb.org squeeze all' >> /etc/apt/sources.list
   echo -e 'deb-src http://packages.dotdeb.org squeeze all' >> /etc/apt/sources.list
fi
#inport GnuPG key
wget http://www.dotdeb.org/dotdeb.gpg
cat dotdeb.gpg | apt-key add -
rm dotdeb.gpg
#update source
apt-get update
 
#make host dir
mkdir /var/www
 
#install zip unzip
apt-get install -y zip unzip
 
#install mysql
apt-get install -y mysql-server mysql-client
cat > /etc/mysql/conf.d/lowendbox.cnf <<END
# These values override values from /etc/mysql/my.cnf
[mysqld]
key_buffer_size = 12M
query_cache_limit = 256K
query_cache_size = 4M
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8' 
character-set-server = utf8 
collation-server = utf8_unicode_ci 
skip-character-set-client-handshake
default-storage-engine = MyISAM
innodb = OFF
skip-innodb
[client]
default-character-set = utf8
END
/etc/init.d/mysql restart
 
#install PHP
apt-get -y install php5-fpm php5-gd php5-common php5-curl php5-imagick php5-mcrypt php5-memcache php5-mysql php5-cgi php5-cli 
#edit php
sed -i  s/'listen = 127.0.0.1:9000'/'listen = \/var\/run\/php5-fpm.sock'/ /etc/php5/fpm/pool.d/www.conf
sed -i  s/'^pm.max_children = [0-9]*'/'pm.max_children = 2'/ /etc/php5/fpm/pool.d/www.conf
sed -i  s/'^pm.start_servers = [0-9]*'/'pm.start_servers = 1'/ /etc/php5/fpm/pool.d/www.conf
sed -i  s/'^pm.min_spare_servers = [0-9]*'/'pm.min_spare_servers = 1'/ /etc/php5/fpm/pool.d/www.conf
sed -i  s/'^pm.max_spare_servers = [0-9]*'/'pm.max_spare_servers = 2'/ /etc/php5/fpm/pool.d/www.conf
sed -i  s/'memory_limit = 128M'/'memory_limit = 64M'/ /etc/php5/fpm/php.ini
sed -i  s/'short_open_tag = Off'/'short_open_tag = On'/ /etc/php5/fpm/php.ini
sed -i  s/'upload_max_filesize = 2M'/'upload_max_filesize = 8M'/ /etc/php5/fpm/php.ini
/etc/init.d/php5-fpm restart
 
#install nginx
apt-get -y install nginx-full
# edit nginx
if [ -f /etc/nginx/nginx.conf ]
then
	# one worker for each CPU and max 1024 connections/worker
	cpu_count=`grep -c ^processor /proc/cpuinfo`
	sed -i \
               "s/worker_processes [0-9]*;/worker_processes $cpu_count;/" \
	      /etc/nginx/nginx.conf
	sed -i \
	       "s/worker_connections [0-9]*;/worker_connections 1024;/" \
	       /etc/nginx/nginx.conf
fi
cat > /etc/nginx/sites-available/default <<EOF
	server {
	listen [::]:80 default ipv6only=on; ## listen for ipv6
	listen 80;
	server_name localhost;
	root /var/www/; 
	index index.php index.html index.htm;
	location ~ \.php$ {
	fastcgi_split_path_info ^(.+\.php)(/.+)$;
	# With php5-fpm:
	fastcgi_pass unix:/var/run/php5-fpm.sock;
	fastcgi_index index.php;
	include fastcgi_params;
EOF
 
cat >> /etc/nginx/sites-available/default <<"EOF"	
	fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;	
	}
	}
EOF
cat  >> /etc/nginx/fastcgi_params <<EOF
	fastcgi_connect_timeout 60;
	fastcgi_send_timeout 180;
	fastcgi_read_timeout 180;
	fastcgi_buffer_size 128k;
	fastcgi_buffers 4 256k;
	fastcgi_busy_buffers_size 256k;
	fastcgi_temp_file_write_size 256k;
	fastcgi_intercept_errors on;
EOF
#wordpress
cat  > /etc/nginx/wordpress.conf <<"EOF"
	if (-d $request_filename){
    rewrite ^/(.*)([^/])$ /$1$2/ permanent;
	}
	if (-f $request_filename/index.html){
    rewrite (.*) $1/index.html break;
	}
	if (-f $request_filename/index.php){
    rewrite (.*) $1/index.php;
	}
	if (!-f $request_filename){
    rewrite (.*) /index.php;
	}
EOF
#DZ
cat  > /etc/nginx/discuz.conf <<"EOF"
	rewrite ^([^\.]*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2 last;
	rewrite ^([^\.]*)/article-([0-9]+)-([0-9]+)\.html$ $1/portal.php?mod=view&aid=$2&page=$3 last;
	rewrite ^([^\.]*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;
	rewrite ^([^\.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;
	rewrite ^([^\.]*)/group-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3 last;
	rewrite ^([^\.]*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&$2=$3 last;
	rewrite ^([^\.]*)/blog-([0-9]+)-([0-9]+)\.html$ $1/home.php?mod=space&uid=$2&do=blog&id=$3 last;
	rewrite ^([^\.]*)/(fid|tid)-([0-9]+)\.html$ $1/index.php?action=$2&value=$3 last;
	rewrite ^([^\.]*)/([a-z]+[a-z0-9_]*)-([a-z0-9_\-]+)\.html$ $1/plugin.php?id=$2:$3 last;
	if (!-e $request_filename) {
	return 404;
	}
EOF
 
#restart lnmp
echo "-------------------------" &&
echo "     restart lnmp!       " &&
echo "-------------------------"
/etc/init.d/nginx restart
/etc/init.d/php5-fpm restart
/etc/init.d/mysql restart
echo "--------------------------" &&
echo "lnmp install successfully!" &&
echo "--------------------------"
 
echo "--------------------------" &&
echo "   begin to add vhost     " &&
echo "--------------------------"
echo "input hostname(like tennfy.com):"
read hostname
echo "input url rewrite rule name(wordpress or discuz):"
read rewriterule
 
/etc/init.d/nginx stop
 
cat > /etc/nginx/conf.d/${hostname}.conf <<EOF
server {
listen 80;
#ipv6
#listen [::]:80 default_server;
root /var/www/${hostname};
index index.php index.html index.htm;
server_name ${hostname} www.${hostname};
location / {
	include ${rewriterule}.conf;
}
location ~ \.php$ {
	fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
EOF
 
cat  >> /etc/nginx/conf.d/${hostname}.conf <<"EOF"      
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
	}
	}
EOF
mkdir /var/www/${hostname}
cd /var/www/${hostname}
chmod -R 777 /var/www
chown -R www-data /var/www
cat  >> /var/www/${hostname}/info.php <<EOF
	<?php phpinfo(); ?>
EOF
/etc/init.d/nginx start
echo "----------------------------" &&
echo "  vhost add successfully!   " &&
echo "----------------------------"

脚本使用方法

将以上脚本保存为lnmp_tennfy.sh,并保存到你自己的服务器上,如http://tennfyfile.qiniudn.com/lnmp_tennfy.sh。

debian中执行以下命令:

1
2
wget http://tennfyfile.qiniudn.com/lnmp_tennfy.sh
bash lnmp_tennfy.sh

执行过程中,会要求你输入hostname以及rewriterule,其中

1
2
hostname为你的虚拟主机域名
rewriterule为伪静态规则,这里可以填写wordpress或者discuz

提示vhost add successfully!即为脚本安装成功。

配置文件目录

1
2
3
4
5
6
网站目录:      /var/www/hostname
nginx目录:    /etc/nginx
nginx配置文件: /etc/nginx/conf.d/hostname.conf
mysql目录:    /etc/mysql  
php目录:      /etc/php5
php.ini:      /etc/php5/fpm/php.ini或/etc/php/php.ini

操作命令

1
2
3
重启mysql: /etc/init.d/mysql restart
重启php:  /etc/init.d/php5-fpm restart
重启nginx: /etc/init.d/nginx restart

文章小结

该脚本可能还有些问题,欢迎大家提出意见。



本文出自 TENNFY WU,转载时请注明出处及相应链接。

本文永久链接: http://www.tennfy.com/2008.html

下一篇文章:

上一篇文章:

1人参与了讨论

  1. 屌丝智慧 说:

    惭愧 现在都不是很明白 脚本是干嘛的

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

8 + 1 = ?


您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

返回顶部