我的第一个脚本-centos5.4-nginx-php-mysql-自动安装


由于最近买了一个burst的vps, 于是开始看些linux下的东西, 挺有意思, 开始在网上进行一些centos的设置, 并且记录在我的wiki上, 最近开始接触到脚本, 发现很强大, 省得学习时重装系统后繁琐的重复设置, 于是自己尝试写了一点, 主要为了偷懒, 而且写得十分粗燥, 不过总算在centos5.4和fedora12中测试通过.
如果安装成功, 能在最后看到各种服务的启动[OK]或是successfully, 由于不是编译安装, 所以基本不是最新版, 但应该是稳定版, 而且由于是默认安装, 以后升级也方便, 出了问题也容易google排查, 因为都是默认安装路径, 不会出现找不到文件的情况. 当然也可以选择LNMP之类的一键包, 只要有人维护, 这类包升级也方便.
OK, 安装很简单, 尽量在新装的系统中应用, 如果原先装过mysql, 则可能造成root密码为原先的密码.
1. 首先下载脚本centos.sh

2. 更改权限 chmod +x centos.sh
3. 运行./centos.sh
4. 输入mysql root的密码
5. 等待完成, 最后一排绿色[OK]和一个spawn-fcgi的successfully的字样就完成了
在fedora12和centos5.4中成功, 如果是debian或是ubuntu的话, 可能改成apt-get就行了, 暂时没有测试

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
#!/bin/bash
#   molezz centos shell: http://www.molezz.net/
#   version: 0.2
#   2010-04-23
#
#   Reference:
#   http://www.centospub.com/
#   http://www.atans.cn/post/93/
#   http://www.zquan.me/article.asp?id=31
#
 
# check root
if [ $(id -u) != "0" ]; then
        echo "ERROR: You are not root, please use root"
        exit 1
fi
 
echo "---------------------set mysql root password--------------------"
mysqlrootpasswd="root"
echo "please input the mysql root password: "
read -p "(defalt password: root):" mysqlrootpasswd
if [ "$mysqlrootpasswd" = "" ]; then
		mysqlrootpasswd = "root"
fi
 
echo "---------------------installation start-------------------------"
 
# update and clear
yum -y update
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
rpm -qa|grep httpd
rpm -e httpd
rpm -qa|grep nginx
rpm -e nginx
rpm -qa|grep spawn-fcgi
rpm -e spawn-fcgi
rpm -qa|grep php
rpm -e php-mysql
rpm -qa|grep mysql
rpm -e mysql-server
rpm -e perl-DBD-MySQL
rpm -e mysql
 
echo "--------------------------install nginx-------------------------"
rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-3.noarch.rpm
yum -y install nginx
chkconfig --level 235 nginx on
service nginx start
 
 
echo "--------------------------install mysql-------------------------"
yum -y install mysql-server
chkconfig --level 235 mysqld on
 
 
echo "-----------------install php and spawn-fcgi-----------------------"
yum -y install php-cgi php-mysql
yum -y install spawn-fcgi
echo "spawn-fcgi -C 5 -a 127.0.0.1 -p 9000 -u nginx -g nginx -f /usr/bin/php-cgi" >> /etc/rc.local
 
# modify the nginx.conf, I hope I can write beautiful later................
sed '104,111s/#//g' /etc/nginx/nginx.conf > /etc/nginx/nginx.conf.out
mv -f /etc/nginx/nginx.conf.out  /etc/nginx/nginx.conf
sed 's/index.html index.htm/index.html index.htm index.php/g' /etc/nginx/nginx.conf > /etc/nginx/nginx.conf.out
mv -f /etc/nginx/nginx.conf.out  /etc/nginx/nginx.conf
sed 's:/scripts:/usr/share/nginx/html:g' /etc/nginx/nginx.conf > /etc/nginx/nginx.conf.out
mv -f /etc/nginx/nginx.conf.out  /etc/nginx/nginx.conf
 
 
service mysqld start
/usr/bin/mysqladmin -u root password $mysqlrootpasswd
sed '/22 -j/{h;p;g;s/22 -j/80 -j/g}' /etc/sysconfig/iptables > /etc/sysconfig/iptables.out
mv -f /etc/sysconfig/iptables.out /etc/sysconfig/iptables
service iptables restart
spawn-fcgi -C 5 -a 127.0.0.1 -p 9000 -u nginx -g nginx -f /usr/bin/php-cgi
service nginx restart
echo "<?php phpinfo(); ?>" >> /usr/share/nginx/html/test.php
 
 
 
echo "********************************************************************"
echo "********************************************************************"
echo "**                             completed!                         **"
echo "********************************************************************"
echo "**                                                                **"
echo "**           php info:           http://127.0.0.1/test.php        **"
echo "**           nginx config file:  /etc/nginx/nginx.conf            **"
echo "**           web dir:            /usr/share/nginx/html            **"
echo "**           mysql dir:          /usr/bin                         **"
echo "**           mysql database dir: /var/lib/mysql                   **"
echo "**           php location:       /usr/bin                         **"
echo "**                                                                **"
echo "********************************************************************"
echo "**                                                                **"
echo "**        for mysql security, you should run secure setting       **"
echo "**        /usr/bin/mysql_secure_installation                      **"
echo "**                                                                **"
echo "********************************************************************"
echo "mysql root password :  $mysqlrootpasswd              "