LNMP:Linux+ Nginx+MySQL+PHP编译安装

前面介绍了nginx和memcached的工作机制,接下来我们主要配置下LNMP的编译安装以及将Memcached整合到LNMP中(基于**Red
Hat
Enterprise Linux
Server release 5.8)**

一、安装Nginx:

 

1、解决依赖关系

编译安装nginx需要事先需要安装开发包组”Development Tools”和 “Development
Libraries”。同时,还需要专门安装pcre-devel包:

  1. # yum -y install pcre-devel
  2. # yum -y groupinstall
    “group_name”

2、安装

首先添加用户nginx,实现以之运行nginx服务进程:

  1. # groupadd -r nginx
  2. # useradd -r -g nginx nginx

接着开始编译和安装:nginx-1.2.3.tar.gz下载路径:ftp://172.16.0.1/pub/Sources/nginx

  1. # tar xvf nginx-1.2.3.tar.gz
  2. # cd nginx-1.2.3
  3. # ./configure \
  4. –prefix=/usr \
  5. –sbin-path=/usr/sbin/nginx \
  6. –conf-path=/etc/nginx/nginx.conf \
  7. –error-log-path=/var/log/nginx/error.log
    \
  8. –http-log-path=/var/log/nginx/access.log
    \
  9. –pid-path=/var/run/nginx/nginx.pid
    \
  10. –lock-path=/var/lock/nginx.lock \
  11. –user=nginx \
  12. –group=nginx \
  13. –with-http_ssl_module \
  14. –with-http_flv_module \
  15. –with-http_stub_status_module \
  16. –with-http_gzip_static_module \
  17. –http-client-body-temp-path=/var/tmp/nginx/client/
    \
  18. –http-proxy-temp-path=/var/tmp/nginx/proxy/
    \
  19. –http-fastcgi-temp-path=/var/tmp/nginx/fcgi/
    \
  20. –http-uwsgi-temp-path=/var/tmp/nginx/uwsgi
    \
  21. –http-scgi-temp-path=/var/tmp/nginx/scgi
    \
  22. –with-pcre
  23. # make && make install

说明:如果想使用nginx的perl模块,可以通过为configure脚本添加–with-http_perl_module选项来实现,但目前此模块仍处于实验性使用阶段,可能会在运行中出现意外,因此,其实现方式这里不再介绍。如果想使用基于nginx的cgi功能,也可以基于FCGI来实现,具体实现方法请参照网上的文档。

 

3、为nginx提供SysV init脚本:(仅作参考)

新建文件/etc/rc.d/init.d/nginx,内容如下:

  1. #!/bin/sh
  2. #
  3. # nginx – this script starts and stops the nginx daemon
  4. #
  5. # chkconfig: – 85 15
  6. # description: Nginx is an HTTP(S) server, HTTP(S) reverse \
  7. # proxy and IMAP/POP3 proxy server
  8. # processname: nginx
  9. # config: /etc/nginx/nginx.conf
  10. # config: /etc/sysconfig/nginx
  11. # pidfile: /var/run/nginx.pid
    1. # Source function library.
  12. . /etc/rc.d/init.d/functions
    1. # Source networking configuration.
  13. . /etc/sysconfig/network
    1. # Check that networking is up.
  14. [ “$NETWORKING” = “no” ] && exit 0
    1. nginx=”/usr/sbin/nginx”
  15. prog=$(basename $nginx)
    1. NGINX_CONF_FILE=”/etc/nginx/nginx.conf”
    1. [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
    1. lockfile=/var/lock/subsys/nginx
    1. make_dirs() {
  16. # make required directories
  17. user=`nginx -V 2>&1 | grep “configure arguments:” | sed
    ‘s/[^*]*–user=\([^
    ]*\).*/\1/g’ -`
  18. options=`$nginx -V 2>&1 | grep ‘configure arguments:’`
  19. for opt in $options; do
  20. if [ `echo $opt | grep ‘.*-temp-path’` ]; then
  21. value=`echo $opt | cut -d “=” -f
    2`
  22. if [ ! -d “$value” ]; then
  23. # echo “creating” $value
  24. mkdir -p $value && chown -R $user $value
  25. fi
  26. fi
  27. done
  28. }
    1. start() {
  29. [ -x $nginx ] || exit 5
  30. [ -f $NGINX_CONF_FILE ] || exit 6
  31. make_dirs
  32. echo -n $”Starting $prog: “
  33. daemon $nginx -c $NGINX_CONF_FILE
  34. retval=$?
  35. echo
  36. [ $retval -eq 0 ] && touch $lockfile
  37. return $retval
  38. }
    1. stop() {
  39. echo -n $”Stopping $prog: “
  40. killproc $prog -QUIT
  41. retval=$?
  42. echo
  43. [ $retval -eq 0 ] && rm -f $lockfile
  44. return $retval
  45. }
    1. restart() {
  46. configtest || return $?
  47. stop
  48. sleep 1
  49. start
  50. }
    1. reload() {
  51. configtest || return $?
  52. echo -n $”Reloading $prog: “
  53. killproc $nginx -HUP
  54. RETVAL=$?
  55. echo
  56. }
    1. force_reload() {
  57. restart
  58. }
    1. configtest() {
  59. $nginx -t -c $NGINX_CONF_FILE
  60. }
    1. rh_status() {
  61. status $prog
  62. }
    1. rh_status_q() {
  63. rh_status >/dev/null 2>&1
  64. }
    1. case “$1” in
  65. start)
  66. rh_status_q && exit 0
  67. $1
  68. ;;
  69. stop)
  70. rh_status_q || exit 0
  71. $1
  72. ;;
  73. restart|configtest)
  74. $1
  75. ;;
  76. reload)
  77. rh_status_q || exit 7
  78. $1
  79. ;;
  80. force-reload)
  81. force_reload
  82. ;;
  83. status)
  84. rh_status
  85. ;;
  86. condrestart|try-restart)
  87. rh_status_q || exit 0
  88. ;;
  89. *)
  90. echo $”Usage: $0
    {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}”
  91. exit 2
  92. esac

而后为此脚本赋予执行权限:

  1. # chmod +x /etc/rc.d/init.d/nginx

添加至服务管理列表,并让其开机自动启动:

  1. # chkconfig –add nginx
  2. # chkconfig nginx on

而后就可以启动服务并测试了(这一步要保证你的httpd没有在运行):

  1. # service nginx start

图片 1

相关文章