搭建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