首页 > 代码库 > Linux下php+mysql+nginx编译搭建(一)

Linux下php+mysql+nginx编译搭建(一)

之前一直都是一键搭建的web服务器,但是一键搭建的环境相对来说都是比较老的。如果要用比较新的环境,特别是正式服务器,就必须自己手动编译搭建了(以下搭建基于linux centos6.5 32位服务器)。

1、 nginx
版本:1.5
下载地址: http://nginx.org/download/nginx-1.5.2.tar.gz

2、 mysql
版本5.5
下载地址:http://downloads.mysql.com/archives/mysql-5.0/mysql-5.5.30.tar.gz

3、 php
版本5.4
下载地址:http://am1.php.net/get/php-5.4.34.tar.gz/from/this/mirror

一:安装nginx
安装一些依赖包:

  1. yum -y install gcc gcc-c++ gcc-devel gcc-c++-devel ssl ssl-devel autoconf make aclocal libtool expat-devel libxml2-devel openssl openssl-devel zlib zlib-devel bzip2 bzip2-devel gd gd-devel libmcrypt libmcrypt-devel libXpm-devel curl-devel libgd-devel gd-devel openldap-devel

进入一个目录:

  1. cd /opt/

下载并解压:

  1. wget http://nginx.org/download/nginx-1.5.2.tar.gz
  2. tar -zxf nginx-1.5.2.tar.gz
  3. wget http://labs.frickle.com/files/ngx_cache_purge-2.1.tar.gz
  4. tar -zxf ngx_cache_purge-2.1.tar.gz

进入目录并编译:

  1. cd nginx-1.5.2
  2. ./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/usr/local/nginx/log/error.log --http-log-path=/usr/local/nginx/log/access.log --pid-path=/usr/local/nginx/run/nginx.pid --user=www --group=www --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/usr/local/nginx/tmp/client --http-proxy-temp-path=/usr/local/nginx/tmp/proxy/ --http-fastcgi-temp-path=/usr/local/nginx/tmp/fcgi/ --add-module=../ngx_cache_purge-2.1 --with-pcre=../pcre-8.34
  3. make && make install

这样就完成了nginx的搭建。
运行:/usr/local/nginx/sbin/nginx
报错:nginx: [emerg] getpwnam("www") failed
在nginx.conf中 把user nobody的注释去掉既可,改成www
再次运行:/usr/local/nginx/sbin/nginx
报错:nginx: [emerg] getpwnam("www") failed in /usr/local/nginx/conf/nginx.conf:1
错误的原因是没有创建www这个用户,应该在服务器系统中添加www用户组和用户www,如下命令:

  1. groupadd -f www
  2. useradd -g www www

第三次运行:/usr/local/nginx/sbin/nginx
报错:nginx: [emerg] mkdir() "/usr/local/nginx/tmp/client" failed (2: No such file or directory)
执行:mkdir -p /usr/local/nginx/tmp/client
然后localhost访问就可以看到:

习惯了了/etc/init.d/nginx start?觉得/usr/local/nginx/sbin/nginx 太长?
Vim /etc/init.d/nginx

  1. #!/bin/bash
  2. #
  3. # Init file for nginx server daemon
  4. #
  5. # chkconfig: 234 99 99
  6. # description: nginx server daemon
  7. #
  8. # source function library
  9. . /etc/rc.d/init.d/functions
  10. # pull in sysconfig settings
  11. [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
  12. RETVAL=0
  13. prog="nginx"
  14. PAT=/usr/local/nginx
  15. NGINXD=/usr/local/nginx/sbin/nginx
  16. PID_FILE=/usr/local/nginx/nginx.pid
  17. start()
  18. {
  19. echo -n $"Starting $prog: "
  20. $NGINXD 2>/dev/null $OPTIONS && success || failure
  21. RETVAL=$?
  22. [ "$RETVAL" = 0 ] && touch /var/lock/subsys/nginx
  23. echo
  24. }
  25. stop()
  26. {
  27. echo -n $"Shutting down $prog: "
  28. killproc nginx
  29. RETVAL=$?
  30. echo
  31. [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/nginx
  32. return $RETVAL
  33. }
  34. reload()
  35. {
  36. echo -n $"Reloading nginx: "
  37. killproc nginx -HUP
  38. RETVAL=$?
  39. echo
  40. return $RETVAL
  41. }
  42. case "$1" in
  43. "start")
  44. start
  45. ;;
  46. "stop")
  47. stop
  48. ;;
  49. "restart")
  50. stop
  51. start
  52. ;;
  53. "reload")
  54. reload
  55. ;;
  56. "status")
  57. status -p $PID_FILE nginx
  58. RETVAL=$?
  59. ;;
  60. *)
  61.  
  62. echo $"Usage: $0 {start|stop|restart|reload|status}"
  63. RETVAL=1
  64. esac
  65. exit $RETVAL

保存,添加x权限。
如需开机启动:
chkconfig nginx on


更多请支持:http://www.webyang.net/Html/web/article_127.html

Linux下php+mysql+nginx编译搭建(一)