[关闭]
@kklinan 2017-03-09T11:45:41.000000Z 字数 10404 阅读 1077

Nginx 安装

Nginx


安装基础服务

sudo yum install autoconf automake gcc gcc-c++ libtool

安装 PCRE 库

ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ 下载最新的 PCRE 源码包,使用下面命令下
载编译和安装 PCRE 包

  1. cd /usr/local/src
  2. wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.34.tar.gz
  3. tar -zxvf pcre-8.34.tar.gz
  4. cd pcre-8.34
  5. ./configure
  6. make
  7. make install

安装 zlib 库

http://zlib.net/zlib-1.2.8.tar.gz 下载最新的 zlib 源码包,使用下面命令下载编译和安装 zlib包:

  1. cd /usr/local/src
  2. wget http://zlib.net/zlib-1.2.8.tar.gz
  3. tar -zxvf zlib-1.2.8.tar.gz
  4. cd zlib-1.2.8
  5. ./configure
  6. make
  7. make install

安装 ssl

  1. cd /usr/local/src
  2. wget http://www.openssl.org/source/openssl-1.0.1c.tar.gz
  3. tar -zxvf openssl-1.0.1c.tar.gz

载 Nginx 最新稳定版

  1. cd /usr/local/src/
  2. sudo curl -OL http://nginx.org/download/nginx-1.8.0.tar.gz
  3. sudo tar xvf nginx-1.8.0.tar.gz
  4. sudo chown -R root:root nginx-1.8.0

Nginx编译选项简述

参考:

–prefix=PATH : 指定nginx的安装目录。默认 /usr/local/nginx
–conf-path=PATH : 设置nginx.conf配置文件的路径。nginx允许使用不同的配置文件启动,通过命令行中的-c选项。默认为prefix/conf/nginx.conf
–user=name: 设置nginx工作进程的用户。安装完成后,可以随时在nginx.conf配置文件更改user指令。默认的用户名是nobody。–group=name类似
–with-pcre : 设置PCRE库的源码路径,如果已通过yum方式安装,使用–with-pcre自动找到库文件。使用–with-pcre=PATH时,需要从PCRE网站下载pcre库的源码(版本4.4 – 8.30)并解压,剩下的就交给Nginx的./configure和make来完成。perl正则表达式使用在location指令和 ngx_http_rewrite_module模块中。
–with-zlib=PATH : 指定 zlib(版本1.1.3 – 1.2.5)的源码解压目录。在默认就启用的网络传输压缩模块ngx_http_gzip_module时需要使用zlib 。
–with-http_ssl_module : 使用https协议模块。默认情况下,该模块没有被构建。前提是openssl与openssl-devel已安装
–with-http_stub_status_module : 用来监控 Nginx 的当前状态
–with-http_realip_module : 通过这个模块允许我们改变客户端请求头中客户端IP地址值(例如X-Real-IP 或 X-Forwarded-For),意义在于能够使得后台服务器记录原始客户端的IP地址
–add-module=PATH : 添加第三方外部模块,如nginx-sticky-module-ng或缓存模块。每次添加新的模块都要重新编译(Tengine可以在新加入module时无需重新编译)

  1. --prefix=PATH set installation prefix
  2. --sbin-path=PATH set nginx binary pathname
  3. --conf-path=PATH set nginx.conf pathname
  4. --error-log-path=PATH set error log pathname
  5. --pid-path=PATH set nginx.pid pathname
  6. --lock-path=PATH set nginx.lock pathname
  7. --user=USER set non-privileged user for
  8. worker processes
  9. --group=GROUP set non-privileged group for
  10. worker processes
  11. --build=NAME set build name
  12. --builddir=DIR set build directory
  13. --with-rtsig_module enable rtsig module
  14. --with-select_module enable select module
  15. --without-select_module disable select module
  16. --with-poll_module enable poll module
  17. --without-poll_module disable poll module
  18. --with-threads enable thread pool support
  19. --with-file-aio enable file AIO support
  20. --with-ipv6 enable IPv6 support
  21. --with-http_ssl_module enable ngx_http_ssl_module
  22. --with-http_spdy_module enable ngx_http_spdy_module
  23. --with-http_realip_module enable ngx_http_realip_module
  24. --with-http_addition_module enable ngx_http_addition_module
  25. --with-http_xslt_module enable ngx_http_xslt_module
  26. --with-http_image_filter_module enable ngx_http_image_filter_module
  27. --with-http_geoip_module enable ngx_http_geoip_module
  28. --with-http_sub_module enable ngx_http_sub_module
  29. --with-http_dav_module enable ngx_http_dav_module
  30. --with-http_flv_module enable ngx_http_flv_module
  31. --with-http_mp4_module enable ngx_http_mp4_module
  32. --with-http_gunzip_module enable ngx_http_gunzip_module
  33. --with-http_gzip_static_module enable ngx_http_gzip_static_module
  34. --with-http_auth_request_module enable ngx_http_auth_request_module
  35. --with-http_random_index_module enable ngx_http_random_index_module
  36. --with-http_secure_link_module enable ngx_http_secure_link_module
  37. --with-http_degradation_module enable ngx_http_degradation_module
  38. --with-http_stub_status_module enable ngx_http_stub_status_module
  39. --without-http_charset_module disable ngx_http_charset_module
  40. --without-http_gzip_module disable ngx_http_gzip_module
  41. --without-http_ssi_module disable ngx_http_ssi_module
  42. --without-http_userid_module disable ngx_http_userid_module
  43. --without-http_access_module disable ngx_http_access_module
  44. --without-http_auth_basic_module disable ngx_http_auth_basic_module
  45. --without-http_autoindex_module disable ngx_http_autoindex_module
  46. --without-http_geo_module disable ngx_http_geo_module
  47. --without-http_map_module disable ngx_http_map_module
  48. --without-http_split_clients_module disable ngx_http_split_clients_module
  49. --without-http_referer_module disable ngx_http_referer_module
  50. --without-http_rewrite_module disable ngx_http_rewrite_module
  51. --without-http_proxy_module disable ngx_http_proxy_module
  52. --without-http_fastcgi_module disable ngx_http_fastcgi_module
  53. --without-http_uwsgi_module disable ngx_http_uwsgi_module
  54. --without-http_scgi_module disable ngx_http_scgi_module
  55. --without-http_memcached_module disable ngx_http_memcached_module
  56. --without-http_limit_conn_module disable ngx_http_limit_conn_module
  57. --without-http_limit_req_module disable ngx_http_limit_req_module
  58. --without-http_empty_gif_module disable ngx_http_empty_gif_module
  59. --without-http_browser_module disable ngx_http_browser_module
  60. --without-http_upstream_hash_module
  61. disable ngx_http_upstream_hash_module
  62. --without-http_upstream_ip_hash_module
  63. disable ngx_http_upstream_ip_hash_module
  64. --without-http_upstream_least_conn_module
  65. disable ngx_http_upstream_least_conn_module
  66. --without-http_upstream_keepalive_module
  67. disable ngx_http_upstream_keepalive_module
  68. --with-http_perl_module enable ngx_http_perl_module
  69. --with-perl_modules_path=PATH set Perl modules path
  70. --with-perl=PATH set perl binary pathname
  71. --http-log-path=PATH set http access log pathname
  72. --http-client-body-temp-path=PATH set path to store
  73. http client request body temporary files
  74. --http-proxy-temp-path=PATH set path to store
  75. http proxy temporary files
  76. --http-fastcgi-temp-path=PATH set path to store
  77. http fastcgi temporary files
  78. --http-uwsgi-temp-path=PATH set path to store
  79. http uwsgi temporary files
  80. --http-scgi-temp-path=PATH set path to store
  81. http scgi temporary files
  82. --without-http disable HTTP server
  83. --without-http-cache disable HTTP cache
  84. --with-mail enable POP3/IMAP4/SMTP proxy module
  85. --with-mail_ssl_module enable ngx_mail_ssl_module
  86. --without-mail_pop3_module disable ngx_mail_pop3_module
  87. --without-mail_imap_module disable ngx_mail_imap_module
  88. --without-mail_smtp_module disable ngx_mail_smtp_module
  89. --with-google_perftools_module enable ngx_google_perftools_module
  90. --with-cpp_test_module enable ngx_cpp_test_module
  91. --add-module=PATH enable an external module
  92. --with-cc=PATH set C compiler pathname
  93. --with-cpp=PATH set C preprocessor pathname
  94. --with-cc-opt=OPTIONS set additional C compiler options
  95. --with-ld-opt=OPTIONS set additional linker options
  96. --with-cpu-opt=CPU build for the specified CPU, valid values:
  97. pentium, pentiumpro, pentium3, pentium4,
  98. athlon, opteron, sparc32, sparc64, ppc64
  99. --without-pcre disable PCRE library usage
  100. --with-pcre force PCRE library usage
  101. --with-pcre=DIR set path to PCRE library sources
  102. --with-pcre-opt=OPTIONS set additional build options for PCRE
  103. --with-pcre-jit build PCRE with JIT compilation support
  104. --with-md5=DIR set path to md5 library sources
  105. --with-md5-opt=OPTIONS set additional build options for md5
  106. --with-md5-asm use md5 assembler sources
  107. --with-sha1=DIR set path to sha1 library sources
  108. --with-sha1-opt=OPTIONS set additional build options for sha1
  109. --with-sha1-asm use sha1 assembler sources
  110. --with-zlib=DIR set path to zlib library sources
  111. --with-zlib-opt=OPTIONS set additional build options for zlib
  112. --with-zlib-asm=CPU use zlib assembler sources optimized
  113. for the specified CPU, valid values:
  114. pentium, pentiumpro
  115. --with-libatomic force libatomic_ops library usage
  116. --with-libatomic=DIR set path to libatomic_ops library sources
  117. --with-openssl=DIR set path to OpenSSL library sources
  118. --with-openssl-opt=OPTIONS set additional build options for OpenSSL
  119. --with-debug enable debug logging

Nginx 编译:

  1. cd nginx-1.8.0
  2. sudo ./configure --help
  3. sudo ./configure --prefix=/usr/local/nginx --with-http_ssl_module

模块安装

安装headers-more-nginx-module模块(https://github.com/openresty/headers-more-nginx-module)请先下载到/usr/local/src/目录并解压重命名为:headers-more-nginx-module
编译命令:
sudo ./configure --prefix=/usr/local/nginx --with-http_ssl_module --add-module=/usr/local/src/headers-more-nginx-module

  1. sudo make
  2. sudo make install

创建启动脚本

  1. #!/bin/sh
  2. #
  3. # nginx - this script starts and stops the nginx daemon
  4. #
  5. # chkconfig: 2345 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: /var/run/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=/var/lock/subsys/nginx
  21. start() {
  22. [ -x $nginx ] || exit 5
  23. [ -f $NGINX_CONF_FILE ] || exit 6
  24. echo -n $"Starting $prog: "
  25. daemon $nginx -c $NGINX_CONF_FILE
  26. retval=$?
  27. echo
  28. [ $retval -eq 0 ] && touch $lockfile
  29. return $retval
  30. }
  31. stop() {
  32. echo -n $"Stopping $prog: "
  33. killproc $prog -QUIT
  34. retval=$?
  35. echo
  36. [ $retval -eq 0 ] && rm -f $lockfile
  37. return $retval
  38. }
  39. restart() {
  40. configtest || return $?
  41. stop
  42. sleep 1
  43. start
  44. }
  45. reload() {
  46. configtest || return $?
  47. echo -n $"Reloading $prog: "
  48. killproc $nginx -HUP
  49. RETVAL=$?
  50. echo
  51. }
  52. force_reload() {
  53. restart
  54. }
  55. configtest() {
  56. $nginx -t -c $NGINX_CONF_FILE
  57. }
  58. rh_status() {
  59. status $prog
  60. }
  61. rh_status_q() {
  62. rh_status >/dev/null 2>&1
  63. }
  64. 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

重新设置启动脚本的所有者:sudo chown root:root /etc/init.d/nginx
重新查看启动脚本的所有者:ll /etc/init.d/nginx
查看启动脚本的命令:sudo service nginx
查看系统服务的命令帮助:chkconfig --help
将启动脚本添加至系统服务:sudo chkconfig --add nginx
查看系统服务列表:sudo chkconfig --list
启动Nginx服务:sudo service nginx start
搜索查看Nginx的运行进程:ps aux | grep nginx

配置

切换目录:cd /usr/local/nginx/conf
编辑Nginx的默认配置文件:sudo vim nginx.conf

  1. user healthd;
  2. worker_processes 2;
  3. #error_log logs/error.log;
  4. #error_log logs/error.log notice;
  5. #error_log logs/error.log info;
  6. pid logs/nginx.pid;
  7. events {
  8. worker_connections 2048;
  9. }
  10. http {
  11. include mime.types;
  12. default_type application/octet-stream;
  13. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  14. '$status $body_bytes_sent "$http_referer" '
  15. '"$http_user_agent" "$http_x_forwarded_for"';
  16. access_log logs/access.log main;
  17. sendfile on;
  18. #tcp_nopush on;
  19. #keepalive_timeout 0;
  20. keepalive_timeout 65;
  21. gzip on;
  22. include include/*.conf;
  23. }

创建配置文件中的include目录:sudo mkdir include
切换目录:cd include/
创建:sudo vim localhost.conf

  1. server {
  2. listen 80;
  3. server_name localhost;
  4. root html;
  5. #charset koi8-r;
  6. #access_log logs/host.access.log main;
  7. location / {
  8. index index.html index.htm;
  9. }
  10. #error_page 404 /404.html;
  11. # redirect server error pages to the static page /50x.html
  12. #
  13. error_page 500 502 503 504 /50x.html;
  14. location = /50x.html {
  15. root html;
  16. }
  17. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  18. #
  19. location ~ \.php$ {
  20. fastcgi_pass 127.0.0.1:9000;
  21. fastcgi_index index.php;
  22. include fastcgi.conf;
  23. }
  24. # deny access to .htaccess files, if Apache's document root
  25. # concurs with nginx's one
  26. #
  27. #location ~ /\.ht {
  28. # deny all;
  29. #}
  30. }

检查Nginx配置是否有误:sudo service nginx configtest
启动Nginx:sudo service nginx start
查看启动进程是否存在Nginx:ps aux | grep nginx
访问服务测试是否成功:curl -v http://localhost/

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注