在CentOS上安装nginx服务器

一、环境描述

  1. 虚拟机配置
    CPU:单核
    内存:2 GB
    硬盘:120 GB
    IP:10.24.17.108

  2. 操作系统
    版本:CentOS 6.6 x86_64
    安装方式:Minimal

  3. 虚拟化环境
    VMware Workstation 12.1.0

  4. nginx
    版本:nginx-1.10.1.tar.gz
    安装方式:编译源码安装

二、下载nginx源码包

在bash中运行以下命令,下载nginx的源码包:

  1. cd /root/Downloads
  2. # 下载nginx源码包
  3. wget http://nginx.org/download/nginx-1.10.1.tar.gz
  4. # 下载pcre源码包
  5. wget http://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.38.tar.gz
  6. # 下载zlib源码包
  7. wget http://zlib.net/zlib-1.2.8.tar.gz

三、安装依赖包

在bash中运行以下命令,在服务器上安装nginx的依赖包:

  1. yum --disablerepo=\* --enablerepo=c6-media install -y openssl openssl-devel

在bash中运行以下命令,安装编译环境:

  1. yum --disablerepo=\* --enablerepo=c6-media groupinstall -y "Development Tools"

四、创建用户和组

在bash中运行以下命令,为nginx创建用户和组:

  1. groupadd -r nginx
  2. useradd -s /sbin/nologin -g nginx -r nginx

五、解压缩源码包

在bash中运行以下命令,解压缩nginx和依赖包的源码包:

  1. tar xvzf nginx-1.10.1.tar.gz
  2. tar xvzf pcre-8.38.tar.gz
  3. tar xvzf zlib-1.2.8.tar.gz
  4. cd nginx-1.10.1

六、编译安装

在bash中运行以下命令,编译安装nginx:

  1. # 配置编译选项
  2. ./configure \
  3. --prefix=/usr/local/nginx \
  4. --user=nginx \
  5. --group=nginx \
  6. --with-select_module \
  7. --with-poll_module \
  8. --with-http_ssl_module \
  9. --with-pcre=/root/Downloads/pcre-8.38 \
  10. --with-pcre-jit \
  11. --with-zlib=/root/Downloads/zlib-1.2.8
  12. # 编译安装
  13. make && make install

七、配置自动启动服务

在bash中运行以下命令,将nginx配置为自动启动的系统服务:

  1. vi /etc/init.d/nginx

在上述init脚本中输入并保存以下内容:

  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: /usr/local/nginx/conf/nginx.conf
  10. # pidfile: /usr/local/nginx/pid/nginx.pid
  11. # Source function library.
  12. . /etc/rc.d/init.d/functions
  13. # Source networking configuration.
  14. . /etc/sysconfig/network
  15. # Check that networking is up.
  16. [ "$NETWORKING" = "no" ] && exit 0
  17. nginx="/usr/local/nginx/sbin/nginx"
  18. prog=$(basename $nginx)
  19. NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
  20. lockfile=/usr/local/nginx/lock/nginx.lock
  21. make_dirs() {
  22. # make required directories
  23. user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
  24. if [ -z "`grep $user /etc/passwd`" ]; then
  25. useradd -M -s /bin/nologin $user
  26. fi
  27. options=`$nginx -V 2>&1 | grep 'configure arguments:'`
  28. for opt in $options; do
  29. if [ `echo $opt | grep '.*-temp-path'` ]; then
  30. value=`echo $opt | cut -d "=" -f 2`
  31. if [ ! -d "$value" ]; then
  32. # echo "creating" $value
  33. mkdir -p $value && chown -R $user $value
  34. fi
  35. fi
  36. done
  37. }
  38. start() {
  39. [ -x $nginx ] || exit 5
  40. [ -f $NGINX_CONF_FILE ] || exit 6
  41. make_dirs
  42. echo -n $"Starting $prog: "
  43. daemon $nginx -c $NGINX_CONF_FILE
  44. retval=$?
  45. echo
  46. [ $retval -eq 0 ] && touch $lockfile
  47. return $retval
  48. }
  49. stop() {
  50. echo -n $"Stopping $prog: "
  51. killproc $prog -QUIT
  52. retval=$?
  53. echo
  54. [ $retval -eq 0 ] && rm -f $lockfile
  55. return $retval
  56. }
  57. restart() {
  58. configtest || return $?
  59. stop
  60. sleep 1
  61. start
  62. }
  63. reload() {
  64. configtest || return $?
  65. echo -n $"Reloading $prog: "
  66. killproc $nginx -HUP
  67. RETVAL=$?
  68. echo
  69. }
  70. force_reload() {
  71. restart
  72. }
  73. configtest() {
  74. $nginx -t -c $NGINX_CONF_FILE
  75. }
  76. rh_status() {
  77. status $prog
  78. }
  79. rh_status_q() {
  80. rh_status >/dev/null 2>&1
  81. }
  82. case "$1" in
  83. start)
  84. rh_status_q && exit 0
  85. $1
  86. ;;
  87. stop)
  88. rh_status_q || exit 0
  89. $1
  90. ;;
  91. restart|configtest)
  92. $1
  93. ;;
  94. reload)
  95. rh_status_q || exit 7
  96. $1
  97. ;;
  98. force-reload)
  99. force_reload
  100. ;;
  101. status)
  102. rh_status
  103. ;;
  104. condrestart|try-restart)
  105. rh_status_q || exit 0
  106. ;;
  107. *)
  108. echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
  109. exit 2
  110. esac

修改脚本权限,然后设置为随系统自动启动:

  1. mkdir -p /usr/local/nginx/pid
  2. mkdir -p /usr/local/nginx/lock
  3. chmod 755 /etc/init.d/nginx
  4. chkconfig nginx on

配置环境变量:

  1. sed -i '80s/$/&:\/usr\/local\/nginx\/sbin/' /etc/profile
  2. source /etc/profile

至此,可以通过以下命令操作nginx服务:

  1. # 启动nginx服务
  2. service nginx start
  3. # 停止nginx服务
  4. service nginx stop
  5. # 重启nginx服务
  6. service nginx restart
  7. # 查看nginx服务状态
  8. service nginx status

八、开启gzip压缩

在bash中运行以下命令,为nginx开启gzip压缩功能,减少网络带宽占用:

  1. # 配置pid文件位置
  2. sed -i '9s/#pid logs\/nginx.pid;/pid pid\/nginx.pid;/' /usr/local/nginx/conf/nginx.conf
  3. # 启用gzip压缩
  4. sed -i '33s/#gzip on;/gzip on;/' /usr/local/nginx/conf/nginx.conf
  5. # 启用gzip压缩的最小文件
  6. sed -i '33a\ gzip_min_length 1k;' /usr/local/nginx/conf/nginx.conf
  7. # gzip压缩级别
  8. sed -i '34a\ gzip_comp_level 2;' /usr/local/nginx/conf/nginx.conf
  9. # 进行压缩的文件类型
  10. sed -i '35a\ gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;' /usr/local/nginx/conf/nginx.conf
  11. # 是否在http header中添加Vary: Accept-Encoding
  12. sed -i '36a\ gzip_vary on;' /usr/local/nginx/conf/nginx.conf
  13. # 禁用IE 6 gzip
  14. sed -i '37a\ gzip_disable "MSIE [1-6]\\.";' /usr/local/nginx/conf/nginx.conf
  15. # 为ttf、otf和svg字体启用gzip
  16. sed -i '38a\ gzip_types font/ttf font/otf image/svg+xml;' /usr/local/nginx/conf/nginx.conf
  17. # 上传文件最大尺寸
  18. sed -i '39a\ client_max_body_size 20m;' /usr/local/nginx/conf/nginx.conf

九、开启缓存

在bash中运行以下命令,为nginx开启静态内容缓存,减少网络带宽占用:

  1. # 为图片文件设置缓存
  2. sed -i '85a\\n' /usr/local/nginx/conf/nginx.conf
  3. sed -i '86a\ location ~* ^.+\\.(ico|gif|jpg|jpeg|png)$ {' /usr/local/nginx/conf/nginx.conf
  4. sed -i '87a\ access_log off;' /usr/local/nginx/conf/nginx.conf
  5. sed -i '88a\ expires 30d;' /usr/local/nginx/conf/nginx.conf
  6. sed -i '89a\ }' /usr/local/nginx/conf/nginx.conf
  7. # 为文本文件设置缓存
  8. sed -i '90a\' /usr/local/nginx/conf/nginx.conf
  9. sed -i '91a\ location ~* ^.+\\.(css|js|txt|xml|swf|wav)$ {' /usr/local/nginx/conf/nginx.conf
  10. sed -i '92a\ access_log off;' /usr/local/nginx/conf/nginx.conf
  11. sed -i '93a\ expires 24h;' /usr/local/nginx/conf/nginx.conf
  12. sed -i '94a\ }' /usr/local/nginx/conf/nginx.conf
  13. # 为静态页面设置缓存
  14. sed -i '95a\\n' /usr/local/nginx/conf/nginx.conf
  15. sed -i '96a\ location ~* ^.+\\.(html|htm)$ {' /usr/local/nginx/conf/nginx.conf
  16. sed -i '97a\ expires 1h;' /usr/local/nginx/conf/nginx.conf
  17. sed -i '98a\ }' /usr/local/nginx/conf/nginx.conf
  18. # 为字体设置缓存
  19. sed -i '99a\' /usr/local/nginx/conf/nginx.conf
  20. sed -i '100a\ location ~* ^.+\\.(eot|ttf|otf|woff|svg)$ {' /usr/local/nginx/conf/nginx.conf
  21. sed -i '101a\ access_log off;' /usr/local/nginx/conf/nginx.conf
  22. sed -i '102a\ expires max;' /usr/local/nginx/conf/nginx.conf
  23. sed -i '103a\ }' /usr/local/nginx/conf/nginx.conf

十、启动服务

在bash中运行以下命令,启动nginx服务:

  1. service nginx start