Ubuntu配置ngnix+phpmyadmin

1 minute read

一、背景

操作系统:Ubuntu11.04-X86 nginx: 1.04 目录位于:/usr/local/nginx 之前我是使用apache作为phpmyadmin的web服务器,后来因为一个项目的环境是nginx配置下来感觉很简单而且业界对nginx评价又比较高所以才转移到nginx;

二、安装配置过程

如果还枚安装过nginx、php、phpmyadmin环境的话,请使用下面的命令安装:

sudo apt-get install nginx mysql-server php5-cgi php5-mysql phpmyadmin

如果和我的环境一样的话,那么安装

sudo apt-get install php5-cgi

创建php-fastcgi脚本:

sudo vi /etc/init.d/php-fastcgi

插入如下脚本:

#!/bin/bash
BIND=127.0.0.1:9000
USER=www-data
PHP_FCGI_CHILDREN=15
PHP_FCGI_MAX_REQUESTS=1000

PHP_CGI=/usr/bin/php-cgi
PHP_CGI_NAME=`basename $PHP_CGI`
PHP_CGI_ARGS="- USER=$USER PATH=/usr/bin PHP_FCGI_CHILDREN=$PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS=$PHP_FCGI_MAX_REQUESTS $PHP_CGI -b $BIND"
RETVAL=0

start() {
      echo -n "Starting PHP FastCGI: "
      start-stop-daemon --quiet --start --background --chuid "$USER" --exec /usr/bin/env -- $PHP_CGI_ARGS
      RETVAL=$?
      echo "$PHP_CGI_NAME."
}
stop() {
      echo -n "Stopping PHP FastCGI: "
      killall -q -w -u $USER $PHP_CGI
      RETVAL=$?
      echo "$PHP_CGI_NAME."
}

case "$1" in
    start)
      start
  ;;
    stop)
      stop
  ;;
    restart)
      stop
      start
  ;;
    *)
      echo "Usage: php-fastcgi {start|stop|restart}"
      exit 1
  ;;
esac
exit $RETVAL

然后设置php-fastcgi随系统启动:

sudo chmod +x /etc/init.d/php-fastcgi
sudo service php-fastcgi start
sudo update-rc.d php-fastcgi defaults

配置nginx的phpmyadmin:

sudo mkdir -p /usr/local/nginx/conf/server/localhost
sudo vi /usr/local/nginx/conf/server/localhost/phpmyadmin.conf

插入如下片段:

server{
	listen 80;
	server_name phpmyadmin;
	access_log /var/log/phpmyadmin.access_log;
	error_log /var/log/phpmyadmin.error_log;

	location / {
	  root /usr/share/phpmyadmin;
	  index index.php;
	}

	location ~ \.php$ {
	    fastcgi_pass    127.0.0.1:9000;
	    fastcgi_index   index.php;
	    fastcgi_param   SCRIPT_FILENAME /usr/share/phpmyadmin$fastcgi_script_name;
	    include         fastcgi_params;
	}
}
其中的日志文件位置和fastcgi_pass中的端口根据自己的环境修改

在nginx.conf中include phpmyadmin配置:

sudo vi /usr/local/nginx/conf/nginx.conf

在最后一个大括号之前插入:

include /usr/local/nginx/conf/server/localhost/phpmyadmin.conf;

启动nginx:

kafeitu@kafeitu-nb:~$ sudo /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
kafeitu@kafeitu-nb:~$ sudo /usr/local/nginx/sbin/nginx

打开/etc/hosts,找到127.0.0.1 localhost然后再最后面加入phpmyadmin保存退出

127.0.0.1   localhost phpmyadmin

现在访问:http://phpmyadmin/ ,开始享受吧……

参考资料:Howto nginx + php5 + mysql + phpmyadmin + ubuntu shortest setup