安装nginx

来源:互联网 发布:网络流行文化 编辑:程序博客网 时间:2024/06/11 05:32

转载自https://help.aliyun.com/document_detail/50700.html

Nginx是一个小巧而高效的Linux下的Web服务器软件,是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,已经在一些俄罗斯的大型网站上运行多年,目前很多国内外的门户网站、行业网站也都在是使用Nginx,相当稳定。

1、添加运行nginx服务进程的用户

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

2、下载源码包解压编译。

  1. # wget http://nginx.org/download/nginx-1.10.2.tar.gz
  2. # tar xvf nginx-1.10.2.tar.gz -C /usr/local/src
  3. # yum groupinstall "Development tools"
  4. # yum -y install gcc wget gcc-c++ automake autoconf libtool libxml2-devel libxslt-devel perl-devel perl-ExtUtils-Embed pcre-devel openssl-devel
  5. # cd /usr/local/src/nginx-1.10.2
  6. # ./configure \
  7. --prefix=/usr/local/nginx \
  8. --sbin-path=/usr/sbin/nginx \
  9. --conf-path=/etc/nginx/nginx.conf \
  10. --error-log-path=/var/log/nginx/error.log \
  11. --http-log-path=/var/log/nginx/access.log \
  12. --pid-path=/var/run/nginx.pid \
  13. --lock-path=/var/run/nginx.lock \
  14. --http-client-body-temp-path=/var/tmp/nginx/client \
  15. --http-proxy-temp-path=/var/tmp/nginx/proxy \
  16. --http-fastcgi-temp-path=/var/tmp/nginx/fcgi \
  17. --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
  18. --http-scgi-temp-path=/var/tmp/nginx/scgi \
  19. --user=nginx \
  20. --group=nginx \
  21. --with-pcre \
  22. --with-http_v2_module \
  23. --with-http_ssl_module \
  24. --with-http_realip_module \
  25. --with-http_addition_module \
  26. --with-http_sub_module \
  27. --with-http_dav_module \
  28. --with-http_flv_module \
  29. --with-http_mp4_module \
  30. --with-http_gunzip_module \
  31. --with-http_gzip_static_module \
  32. --with-http_random_index_module \
  33. --with-http_secure_link_module \
  34. --with-http_stub_status_module \
  35. --with-http_auth_request_module \
  36. --with-mail \
  37. --with-mail_ssl_module \
  38. --with-file-aio \
  39. --with-ipv6 \
  40. --with-http_v2_module \
  41. --with-threads \
  42. --with-stream \
  43. --with-stream_ssl_module
  44. # make && make install
  45. # mkdir -pv /var/tmp/nginx/client

3、添加SysV启动脚本。

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

4、赋予脚本执行权限。

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

5、添加至服务管理列表,设置开机自启。

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

6、启动服务。

# service nginx start

原创粉丝点击