@kklinan
2017-03-09T03:45:41.000000Z
字数 10404
阅读 1154
Nginx
sudo yum install autoconf automake gcc gcc-c++ libtool
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ 下载最新的 PCRE 源码包,使用下面命令下
载编译和安装 PCRE 包
cd /usr/local/srcwget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.34.tar.gztar -zxvf pcre-8.34.tar.gzcd pcre-8.34./configuremakemake install
http://zlib.net/zlib-1.2.8.tar.gz 下载最新的 zlib 源码包,使用下面命令下载编译和安装 zlib包:
cd /usr/local/srcwget http://zlib.net/zlib-1.2.8.tar.gztar -zxvf zlib-1.2.8.tar.gzcd zlib-1.2.8./configuremakemake install
cd /usr/local/srcwget http://www.openssl.org/source/openssl-1.0.1c.tar.gztar -zxvf openssl-1.0.1c.tar.gz
cd /usr/local/src/sudo curl -OL http://nginx.org/download/nginx-1.8.0.tar.gzsudo tar xvf nginx-1.8.0.tar.gzsudo chown -R root:root nginx-1.8.0
参考:
–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时无需重新编译)
--prefix=PATH set installation prefix--sbin-path=PATH set nginx binary pathname--conf-path=PATH set nginx.conf pathname--error-log-path=PATH set error log pathname--pid-path=PATH set nginx.pid pathname--lock-path=PATH set nginx.lock pathname--user=USER set non-privileged user forworker processes--group=GROUP set non-privileged group forworker processes--build=NAME set build name--builddir=DIR set build directory--with-rtsig_module enable rtsig module--with-select_module enable select module--without-select_module disable select module--with-poll_module enable poll module--without-poll_module disable poll module--with-threads enable thread pool support--with-file-aio enable file AIO support--with-ipv6 enable IPv6 support--with-http_ssl_module enable ngx_http_ssl_module--with-http_spdy_module enable ngx_http_spdy_module--with-http_realip_module enable ngx_http_realip_module--with-http_addition_module enable ngx_http_addition_module--with-http_xslt_module enable ngx_http_xslt_module--with-http_image_filter_module enable ngx_http_image_filter_module--with-http_geoip_module enable ngx_http_geoip_module--with-http_sub_module enable ngx_http_sub_module--with-http_dav_module enable ngx_http_dav_module--with-http_flv_module enable ngx_http_flv_module--with-http_mp4_module enable ngx_http_mp4_module--with-http_gunzip_module enable ngx_http_gunzip_module--with-http_gzip_static_module enable ngx_http_gzip_static_module--with-http_auth_request_module enable ngx_http_auth_request_module--with-http_random_index_module enable ngx_http_random_index_module--with-http_secure_link_module enable ngx_http_secure_link_module--with-http_degradation_module enable ngx_http_degradation_module--with-http_stub_status_module enable ngx_http_stub_status_module--without-http_charset_module disable ngx_http_charset_module--without-http_gzip_module disable ngx_http_gzip_module--without-http_ssi_module disable ngx_http_ssi_module--without-http_userid_module disable ngx_http_userid_module--without-http_access_module disable ngx_http_access_module--without-http_auth_basic_module disable ngx_http_auth_basic_module--without-http_autoindex_module disable ngx_http_autoindex_module--without-http_geo_module disable ngx_http_geo_module--without-http_map_module disable ngx_http_map_module--without-http_split_clients_module disable ngx_http_split_clients_module--without-http_referer_module disable ngx_http_referer_module--without-http_rewrite_module disable ngx_http_rewrite_module--without-http_proxy_module disable ngx_http_proxy_module--without-http_fastcgi_module disable ngx_http_fastcgi_module--without-http_uwsgi_module disable ngx_http_uwsgi_module--without-http_scgi_module disable ngx_http_scgi_module--without-http_memcached_module disable ngx_http_memcached_module--without-http_limit_conn_module disable ngx_http_limit_conn_module--without-http_limit_req_module disable ngx_http_limit_req_module--without-http_empty_gif_module disable ngx_http_empty_gif_module--without-http_browser_module disable ngx_http_browser_module--without-http_upstream_hash_moduledisable ngx_http_upstream_hash_module--without-http_upstream_ip_hash_moduledisable ngx_http_upstream_ip_hash_module--without-http_upstream_least_conn_moduledisable ngx_http_upstream_least_conn_module--without-http_upstream_keepalive_moduledisable ngx_http_upstream_keepalive_module--with-http_perl_module enable ngx_http_perl_module--with-perl_modules_path=PATH set Perl modules path--with-perl=PATH set perl binary pathname--http-log-path=PATH set http access log pathname--http-client-body-temp-path=PATH set path to storehttp client request body temporary files--http-proxy-temp-path=PATH set path to storehttp proxy temporary files--http-fastcgi-temp-path=PATH set path to storehttp fastcgi temporary files--http-uwsgi-temp-path=PATH set path to storehttp uwsgi temporary files--http-scgi-temp-path=PATH set path to storehttp scgi temporary files--without-http disable HTTP server--without-http-cache disable HTTP cache--with-mail enable POP3/IMAP4/SMTP proxy module--with-mail_ssl_module enable ngx_mail_ssl_module--without-mail_pop3_module disable ngx_mail_pop3_module--without-mail_imap_module disable ngx_mail_imap_module--without-mail_smtp_module disable ngx_mail_smtp_module--with-google_perftools_module enable ngx_google_perftools_module--with-cpp_test_module enable ngx_cpp_test_module--add-module=PATH enable an external module--with-cc=PATH set C compiler pathname--with-cpp=PATH set C preprocessor pathname--with-cc-opt=OPTIONS set additional C compiler options--with-ld-opt=OPTIONS set additional linker options--with-cpu-opt=CPU build for the specified CPU, valid values:pentium, pentiumpro, pentium3, pentium4,athlon, opteron, sparc32, sparc64, ppc64--without-pcre disable PCRE library usage--with-pcre force PCRE library usage--with-pcre=DIR set path to PCRE library sources--with-pcre-opt=OPTIONS set additional build options for PCRE--with-pcre-jit build PCRE with JIT compilation support--with-md5=DIR set path to md5 library sources--with-md5-opt=OPTIONS set additional build options for md5--with-md5-asm use md5 assembler sources--with-sha1=DIR set path to sha1 library sources--with-sha1-opt=OPTIONS set additional build options for sha1--with-sha1-asm use sha1 assembler sources--with-zlib=DIR set path to zlib library sources--with-zlib-opt=OPTIONS set additional build options for zlib--with-zlib-asm=CPU use zlib assembler sources optimizedfor the specified CPU, valid values:pentium, pentiumpro--with-libatomic force libatomic_ops library usage--with-libatomic=DIR set path to libatomic_ops library sources--with-openssl=DIR set path to OpenSSL library sources--with-openssl-opt=OPTIONS set additional build options for OpenSSL--with-debug enable debug logging
cd nginx-1.8.0sudo ./configure --helpsudo ./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
sudo makesudo make install
#!/bin/sh## nginx - this script starts and stops the nginx daemon## chkconfig: 2345 85 15# description: Nginx is an HTTP(S) server, HTTP(S) reverse \# proxy and IMAP/POP3 proxy server# processname: nginx# config: /usr/local/nginx/conf/nginx.conf# pidfile: /var/run/nginx.pid# Source function library.. /etc/rc.d/init.d/functions# Source networking configuration.. /etc/sysconfig/network# Check that networking is up.[ "$NETWORKING" = "no" ] && exit 0nginx="/usr/local/nginx/sbin/nginx"prog=$(basename $nginx)NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"lockfile=/var/lock/subsys/nginxstart() {[ -x $nginx ] || exit 5[ -f $NGINX_CONF_FILE ] || exit 6echo -n $"Starting $prog: "daemon $nginx -c $NGINX_CONF_FILEretval=$?echo[ $retval -eq 0 ] && touch $lockfilereturn $retval}stop() {echo -n $"Stopping $prog: "killproc $prog -QUITretval=$?echo[ $retval -eq 0 ] && rm -f $lockfilereturn $retval}restart() {configtest || return $?stopsleep 1start}reload() {configtest || return $?echo -n $"Reloading $prog: "killproc $nginx -HUPRETVAL=$?echo}force_reload() {restart}configtest() {$nginx -t -c $NGINX_CONF_FILE}rh_status() {status $prog}rh_status_q() {rh_status >/dev/null 2>&1}case "$1" instart)rh_status_q && exit 0$1;;stop)rh_status_q || exit 0$1;;restart|configtest)$1;;reload)rh_status_q || exit 7$1;;force-reload)force_reload;;status)rh_status;;condrestart|try-restart)rh_status_q || exit 0;;*)echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"exit 2esac
重新设置启动脚本的所有者: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
user healthd;worker_processes 2;#error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;pid logs/nginx.pid;events {worker_connections 2048;}http {include mime.types;default_type application/octet-stream;log_format main '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';access_log logs/access.log main;sendfile on;#tcp_nopush on;#keepalive_timeout 0;keepalive_timeout 65;gzip on;include include/*.conf;}
创建配置文件中的include目录:sudo mkdir include
切换目录:cd include/
创建:sudo vim localhost.conf
server {listen 80;server_name localhost;root html;#charset koi8-r;#access_log logs/host.access.log main;location / {index index.html index.htm;}#error_page 404 /404.html;# redirect server error pages to the static page /50x.html#error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000#location ~ \.php$ {fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;include fastcgi.conf;}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {# deny all;#}}
检查Nginx配置是否有误:sudo service nginx configtest
启动Nginx:sudo service nginx start
查看启动进程是否存在Nginx:ps aux | grep nginx
访问服务测试是否成功:curl -v http://localhost/