在centos上安装服务

目标

简单记录一下如何在centos上安装服务。

  • 为了实现在centos/linux上实现开机启动,一般有两种做法 *

  • 在/etc/rc.local上写启动脚本

  • 安装成服务,然后定义其run level

下面简单说说安装成服务的过程

  • 在/etc/init.d/目录下编写启动脚本

  • 使用chkconfig命令添加服务。

1
2
chkconfig --add nginx
chkconfig [--level 2345] nginx on

启动脚本有一定的要求,主要是要求在脚本开头的注释有一定的格式。形如这样

1
2
3
4
5
6
7
8
9
10
11
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /etc/nginx/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:     /var/run/nginx.pid

–EOF–

设置 Centos 中 Iptables 遇到的问题

接上篇,装完nginx之后,80端口访问不了。查了一下是iptables的问题,当时先简单把iptables停了

1
service iptables start

下面来说打开80端口时遇到的一个坑。centos默认的配置规则中有这么一条。

1
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited

当使用如下命令打开端口时

1
iptables -p tcp -A RH-Firewall-1-INPUT  --dport 80 -j ACCEPT

80端口仍然无法访问,因为上述命令将规则加在最后一条,在这之前就被上行命令给拒掉了,所以完全是没有意义的。只能插在那条之前才能生效。

1
2
iptables -p tcp -I RH-Firewall-1-INPUT 5 --dport 80 -j ACCEPT
service iptables save

–EOF–

centos环境下搭建LNMP

本文描述了如何在centos5.9环境下搭建LNMP环境

吐槽: 之前在ubuntu下面搭过一次,这次因为某种需要再来一次,无语。。。

使用网页的软件源

1
2
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget http://mirrors.163.com/.help/CentOS5-Base-163.repo -O /etc/yum.repos.d/CentOS-Base-163.repo

安装php

卸载系统默认php

1
yum remove php php-common

安装php5.3

1
yum install php53 php53-devel php53-cli

安装spawn-fcgi, php-fastcgi

1
2
wget http://centos.alt.ru/repository/centos/5/i386/centalt-release-5-3.noarch.rpm
yum install spawn-fcgi

php-fastcgi脚本

/usr/bin/php-fastcgi

1
2
#!/bin/sh
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 6 -u nginx -g nginx -f /usr/bin/php-cgi

/etc/init.d/php-fastcgi

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
#!/bin/sh
#
# php-fastcgi - Use PHP as a FastCGI process via nginx.
#
# chkconfig: - 85 15
# description: Use PHP as a FastCGI process via nginx.
# processname: php-fastcgi
# pidfile: /var/run/php-fastcgi.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

phpfastcgi="/usr/bin/php-fastcgi"
prog=$(basename php-cgi)
cgi='/usr/bin/php-cgi'

lockfile=/var/lock/subsys/php-fastcgi

start() {
    [ -x $phpfastcgi ] || exit 5
    echo -n $"Starting $prog: "
    daemon $phpfastcgi
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -TERM
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    #configtest || return $?
    stop
    start
}

reload() {
    #configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $prog -HUP
    RETVAL=$?
    echo
}

force_reload() {
    restart
}

rh_status() {
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart}"
        exit 2
esac

fastcgi加入服务

1
2
chkconfig --add php-fastcgi
chkconfig php-fastcgi on

安装扩展

1
yum install php53-mysql php53-mbstring

安装phpredis

  • 编译安装
1
2
3
4
5
wget https://github.com/nicolasff/phpredis/archive/master.zip
unzip master
cd phpredis-master/
phpize
make && make install
  • 增加文件/etc/php.d/redis.ini
1
2
; Enable redis extension module
extension=redis.so

安装nginx

编译安装

1
2
3
4
5
6
7
yum install zlib-devel openssl openssl-devel pcre pcre-devel
wget http://nginx.org/download/nginx-1.4.1.tar.gz
tar xvfz nginx-1.4.1.tar.gz
cd nginx-1.4.1
./configure --with-http_ssl_module
make && make install
useradd -M -r --shell /sbin/nologin --home-dir /opt/nginx nginx

控制脚本

/etc/rc.d/init.d/nginx

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
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15 
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /etc/nginx/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:     /var/run/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

make_dirs() {
   # make required directories
   user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   if [ -z "`grep $user /etc/passwd`" ]; then
       useradd -M -s /bin/nologin $user
   fi
   options=`$nginx -V 2>&1 | grep 'configure arguments:'`
   for opt in $options; do
       if [ `echo $opt | grep '.*-temp-path'` ]; then
           value=`echo $opt | cut -d "=" -f 2`
           if [ ! -d "$value" ]; then
               # echo "creating" $value
               mkdir -p $value && chown -R $user $value
           fi
       fi
   done
}

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    #make_dirs
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    configtest || return $?
    stop
    sleep 1
    start
}

reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}

force_reload() {
    restart
}

configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

加入服务

1
2
chkconfig --add nginx
chkconfig --level 2345 nginx on

配置文件

1
2
# 建立虚拟机目录 
mkdir -p /usr/local/nginx/sites-enabled

虚拟机配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
server {
    server_name www.demo.com demo.com;
    access_log /srv/www/demo/logs/access.log;
    error_log /srv/www/demo/logs/error.log;
    root /srv/www/demo;

    location / {
        index  index.html index.htm index.php;
    }

    location ~ \.php$ {
        include /usr/local/nginx/conf/fastcgi_params;
        #fastcgi_pass unix:/var/run/php-fastcgi/php-fastcgi.socket;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /srv/www/demo$fastcgi_script_name;
    }
}

在/usr/local/nginx/conf/nginx.conf 加入如下行

1
include /usr/local/nginx/sites-enabled/*;

安装mysql

1
yum install mysql-server

启动

1
2
service nginx start
service php-fastcgi start

tips

  • 由于centos默认的iptables是开着的,所以上述装好之后,需要关闭iptables或者配置规则才能使用。

centos5.9下安装redis

本文描述了如何在centos5.9环境下安装redis

安装编译环境

安装编译器

安装编译器gcc
1
yum install gcc gcc-c++ kernel-devel

安装jemalloc

安装jemalloc
1
2
3
4
5
6
wget http://www.canonware.com/download/jemalloc/jemalloc-3.3.1.tar.bz2
tar xfvj jemalloc-3.3.1.tar.bz2
cd jemalloc-3.3.1
./configure
make
make install

安装tcl

安装tcl
1
2
3
4
5
6
wget http://prdownloads.sourceforge.net/tcl/tcl8.6.0-src.tar.gz
tar xfvz tcl8.6.0-src.tar.gz
cd tcl8.6.0/unix
./configure
make
make install

安装redis

安装redis
1
2
3
wget http://redis.googlecode.com/files/redis-2.6.13.tar.gz
tar xvfz redis-2.6.13.tar.gz
cd redis-2.6.13

修改文件

安装redis
1
2
make test
make

若make失败请做如下修改

Makefile开头加 CFLAGS= -march=i686
src/.make_settings里的OPT,改为OPT=-O2  -march=i686

make test会卡住, 忽略之

配置redis

/etc/init.d/redis_6379
1
2
3
4
5
6
7
sudo cp src/redis-server /usr/local/bin/
sudo cp src/redis-cli /usr/local/bin/
mkdir /etc/redis
mkdir /var/redis
cp utils/redis_init_script /etc/init.d/redis_6379
cp redis.conf /etc/redis/6379.conf
mkdir /var/redis/6379

vim /etc/redis/6379.conf

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
daemonize yes
pidfile /var/run/redis.pid
port 6379
timeout 0
tcp-keepalive 0
loglevel notice
logfile /var/log/redis_6379.log
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /var/redis/6379
slave-serve-stale-data yes
slave-read-only yes
repl-disable-tcp-nodelay no
slave-priority 100
appendonly no
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
#绑定ip
#bind 127.0.0.1

启动redis

1
redis-server /etc/redis/6379.conf

测试

1
2
3
redis-cli
redis 127.0.0.1:6379> ping
PONG

使用VirtualBox搭建linux环境

本文描述了使用vbox搭建linux环境时可能遇到的一些问题.

virtualbox 下载地址

传送门

各发行版地址

网易镜像 搜狐镜像

可能会遇到的问题

磁盘大小选择

  • 在建立虚拟磁盘可以选择动态分配,并且尽量大一些,虽然vbox提供resize操作,但是十分不方便。动态分配磁盘不会占据很多磁盘空间。

版本选择

  • 尽量选择i386发型版,并且将cpu的PAE选项勾选上。

网络

  • 在有网络的时候将网卡从nat方式改成桥接模式,便于ssh访问。

  • 没有网络的时候可以使用如下命令映射端口,使用ssh访问

1
VBoxManage modifyvm "hostname" --natpf1 "guestssh,tcp,,2222,,22"

然后使用2222端口访问ssh服务。

  • 安装完成后,可以同过clone来重复安装相同版本发行版,但是会遇到网卡问题。解决方式如下:

注释掉第一个网卡,并改名第二个网卡为eth0

/etc/udev/rules.d/70-persistent-net.rules

1
2
3
4
5
6
7
8
9
10
11
# This file was automatically generated by the /lib/udev/write_net_rules
# program, run by the persistent-net-generator.rules rules file.
#
# You can modify it, as long as you keep each rule on a single
# line, and change only the value of the NAME= key.

# PCI device 0x8086:/sys/devices/pci0000:00/0000:00:03.0 (e1000)
#SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="08:00:27:f0:78:2b", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

# PCI device 0x8086:/sys/devices/pci0000:00/0000:00:03.0 (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="08:00:27:fc:ab:9d", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

备份

使用导出虚拟机备份,推荐使用ova格式。

搭建LNMP环境

本文描述了如何快速搭建LNMP(linux, nginx, mysql, php)开发环境.

备注:下述操作是基于ubuntu 12.04

安装所需软件包

安装所需软件包
1
2
sudo aptitude update
sudo aptitude install nginx php5-cli php5-cgi spawn-fcgi psmisc

建立web根目录

安装所需软件包
1
2
3
mkdir -p /var/www/demo
mkdir -p /var/www/demo/logs
chown -R www-data:www-data /var/www/demo

配置nginx vhost文件

unix sockets方式

/etc/nginx/sites-enabled/demo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
server {
    server_name www.demo.com demo.com;
    access_log /var/www/demo/logs/access.log;
    error_log /var/www/demo/logs/error.log;
    root /var/www/demo;

    location / {
        index  index.html index.htm index.php;
    }

    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/var/run/php-fastcgi/php-fastcgi.socket;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /srv/www/demo$fastcgi_script_name;
    }
}

tcp sockets方式

/etc/nginx/sites-enabled/demo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
server {
    server_name www.demo.com demo.com;
    access_log /var/www/demo/logs/access.log;
    error_log /var/www/demo/logs/error.log;
    root /var/www/demo;

    location / {
        index  index.html index.htm;
    }

    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /var/www/demo$fastcgi_script_name;
    }
}

配置php-fastcgi

unix sockets方式

/usr/bin/php-fastcgi
1
2
3
4
5
6
7
8
9
10
#!/bin/bash

FASTCGI_USER=www-data
FASTCGI_GROUP=www-data
SOCKET=/var/run/php-fastcgi/php-fastcgi.socket
PIDFILE=/var/run/php-fastcgi/php-fastcgi.pid
CHILDREN=6
PHP5=/usr/bin/php5-cgi

/usr/bin/spawn-fcgi -s $SOCKET -P $PIDFILE -C $CHILDREN -u $FASTCGI_USER -g $FASTCGI_GROUP -f $PHP5

tcp sockets方式

/usr/bin/php-fastcgi
1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash

FASTCGI_USER=www-data
FASTCGI_GROUP=www-data
ADDRESS=127.0.0.1
PORT=9000
PIDFILE=/var/run/php-fastcgi/php-fastcgi.pid
CHILDREN=6
PHP5=/usr/bin/php5-cgi

/usr/bin/spawn-fcgi -a $ADDRESS -p $PORT -P $PIDFILE -C $CHILDREN -u $FASTCGI_USER -g $FASTCGI_GROUP -f $PHP5

设置可执行

/usr/bin/php-fastcgi
1
chmod +x /usr/bin/php-fastcgi

php-fastcgi管理脚本

/etc/init.d/php-fastcgi
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
#!/bin/bash

PHP_SCRIPT=/usr/bin/php-fastcgi
FASTCGI_USER=www-data
FASTCGI_GROUP=www-data
PID_DIR=/var/run/php-fastcgi
PID_FILE=/var/run/php-fastcgi/php-fastcgi.pid
RET_VAL=0

case "$1" in
    start)
      if [[ ! -d $PID_DIR ]]
      then
        mkdir $PID_DIR
        chown $FASTCGI_USER:$FASTCGI_GROUP $PID_DIR
        chmod 0770 $PID_DIR
      fi
      if [[ -r $PID_FILE ]]
      then
        echo "php-fastcgi already running with PID `cat $PID_FILE`"
        RET_VAL=1
      else
        $PHP_SCRIPT
        RET_VAL=$?
      fi
  ;;
    stop)
      if [[ -r $PID_FILE ]]
      then
        kill `cat $PID_FILE`
        rm $PID_FILE
        RET_VAL=$?
      else
        echo "Could not find PID file $PID_FILE"
        RET_VAL=1
      fi
  ;;
    restart)
      if [[ -r $PID_FILE ]]
      then
        kill `cat $PID_FILE`
        rm $PID_FILE
        RET_VAL=$?
      else
        echo "Could not find PID file $PID_FILE"
      fi
      $PHP_SCRIPT
      RET_VAL=$?
  ;;
    status)
      if [[ -r $PID_FILE ]]
      then
        echo "php-fastcgi running with PID `cat $PID_FILE`"
        RET_VAL=$?
      else
        echo "Could not find PID file $PID_FILE, php-fastcgi does not appear to be running"
      fi
  ;;
    *)
      echo "Usage: php-fastcgi {start|stop|restart|status}"
      RET_VAL=1
  ;;
esac
exit $RET_VAL

启动php-fastcgi

1
2
3
4
chmod +x /etc/init.d/php-fastcgi
update-rc.d php-fastcgi defaults
/etc/init.d/php-fastcgi start
/etc/init.d/nginx start

安装mysql

1
aptitude install mysql-server php5-mysql

使用Octopress在github上搭建博客

本文简述了如何在linux环境下搭建Octopress博客,并且更新到pages.github.com网站.

备注:下述操作是基于ubuntu 12.04

安装rvm

官方推荐非root用户的单用户安装模式,如下为单用户安装模式。

安装rvm
1
curl -L https://get.rvm.io | bash -s stable

备注:若最小模式安装ubuntu,则需要先安装curl

安装curl
1
sudo apitude install curl -y

rvm安装完成之后,执行如下命令安装ruby相关依赖包

安装rvm所需依赖包
1
2
rvm autolibs enable
rvm requirements

生效环境变量,执行如下命令

设置rvm环境
1
. ~/.profile

安装ruby

Octopress 要求ruby版本为1.9.3

安装ruby
1
rvm install 1.9.3

因为rvm需要使用login环境的sh环境,最简单的方式获得环境为

安装ruby
1
ssh localhost

安装Octopress

安装Octopress
1
2
3
git clone git://github.com/imathis/octopress.git octopress
cd octopress
bundle

安装默认theme

安装Octopress
1
rake install

生成页面&预览

生成页面
1
2
rake generate
rake preview

此时可在本地4000端口看见博客 可用的theme可以在这里找到

设置github

首先在octopress目录下执行

1
rake setup_github_pages

按照要求填写git地址,如本博客为 git@github.com:lpisces/lpisces.github.com.git 然后执行

1
rake deploy

即可完成部署