Nginx教程

nginx的安装

本文主要是介绍nginx的安装,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

nginx的安装方式
一、YUM安装部署
二、源码安装部署


一、YUM安装部署

1、登录http://nginx.org/官网,点击右侧的download。
2、点击当前页面最下方的stable and mainline:
3、选择RHEL、CentOS:
4、复制配置文件,创建nginx.repo文件:
[root@anonymous yum.repos.d]# vim nginx.repo

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

开始安装nginx,默认情况是安装稳定版的(安装的是nginx/1.20.1)
[root@anonymous yum.repos.d]# yum clean all
[root@anonymous yum.repos.d]# yum makecache
[root@anonymous yum.repos.d]# yum repolist all
[root@anonymous yum.repos.d]# yum -y install nginx
[root@anonymous yum.repos.d]# nginx -v
nginx version: nginx/1.20.1

升级到主线版本(已经升级到nginx/1.21.1)
[root@anonymous yum.repos.d]# yum -y install yum-utils
[root@anonymous yum.repos.d]# yum-config-manager --enable nginx-mainline
[root@anonymous yum.repos.d]# yum -y install nginx
[root@anonymous yum.repos.d]# nginx -v
nginx version: nginx/1.21.1


二、源码安装部署

1、登录http://nginx.org/官网,点击右侧的download。
选择需要下载的稳定版本nginx1.20.1,右键复制下载地址
选择需要下载的主线版本nginx1.21.1作为升级用,右键复制下载地址
[root@anonymous ~]# wget http://nginx.org/download/nginx-1.20.1.tar.gz
[root@anonymous ~]# wget http://nginx.org/download/nginx-1.21.1.tar.gz

2、安装依赖包
[root@anonymous ~]# yum -y install gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel

3、解压安装包到/usr/src
[root@anonymous ~]# tar zxf nginx-1.20.1.tar.gz -C /usr/src/
[root@anonymous ~]# tar zxf nginx-1.21.1.tar.gz -C /usr/src/
[root@anonymous ~]# ls /usr/src/
debug  kernels  nginx-1.20.1  nginx-1.21.1

4开始安装nginx

查看预编译帮助
[root@anonymous nginx-1.20.1]# ./configure --help

  --help                             print this message

  --prefix=PATH                      set installation prefix
  --sbin-path=PATH                   set nginx binary pathname
  --modules-path=PATH                set modules path
  --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 for
                                     worker processes
  --group=GROUP                      set non-privileged group for
                                     worker processes

  --build=NAME                       set build name
  --builddir=DIR                     set build directory

  --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-http_ssl_module             enable ngx_http_ssl_module
  --with-http_v2_module              enable ngx_http_v2_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_xslt_module=dynamic    enable dynamic ngx_http_xslt_module
  --with-http_image_filter_module    enable ngx_http_image_filter_module
  --with-http_image_filter_module=dynamic
                                     enable dynamic ngx_http_image_filter_module
  --with-http_geoip_module           enable ngx_http_geoip_module
  --with-http_geoip_module=dynamic   enable dynamic 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_slice_module           enable ngx_http_slice_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_mirror_module       disable ngx_http_mirror_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_grpc_module         disable ngx_http_grpc_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_module
                                     disable ngx_http_upstream_hash_module
  --without-http_upstream_ip_hash_module
                                     disable ngx_http_upstream_ip_hash_module
  --without-http_upstream_least_conn_module
                                     disable ngx_http_upstream_least_conn_module
  --without-http_upstream_random_module
                                     disable ngx_http_upstream_random_module
  --without-http_upstream_keepalive_module
                                     disable ngx_http_upstream_keepalive_module
  --without-http_upstream_zone_module
                                     disable ngx_http_upstream_zone_module

  --with-http_perl_module            enable ngx_http_perl_module
  --with-http_perl_module=dynamic    enable dynamic 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 store
                                     http client request body temporary files
  --http-proxy-temp-path=PATH        set path to store
                                     http proxy temporary files
  --http-fastcgi-temp-path=PATH      set path to store
                                     http fastcgi temporary files
  --http-uwsgi-temp-path=PATH        set path to store
                                     http uwsgi temporary files
  --http-scgi-temp-path=PATH         set path to store
                                     http scgi temporary files

  --without-http                     disable HTTP server
  --without-http-cache               disable HTTP cache

  --with-mail                        enable POP3/IMAP4/SMTP proxy module
  --with-mail=dynamic                enable dynamic 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-stream                      enable TCP/UDP proxy module
  --with-stream=dynamic              enable dynamic TCP/UDP proxy module
  --with-stream_ssl_module           enable ngx_stream_ssl_module
  --with-stream_realip_module        enable ngx_stream_realip_module
  --with-stream_geoip_module         enable ngx_stream_geoip_module
  --with-stream_geoip_module=dynamic enable dynamic ngx_stream_geoip_module
  --with-stream_ssl_preread_module   enable ngx_stream_ssl_preread_module
  --without-stream_limit_conn_module disable ngx_stream_limit_conn_module
  --without-stream_access_module     disable ngx_stream_access_module
  --without-stream_geo_module        disable ngx_stream_geo_module
  --without-stream_map_module        disable ngx_stream_map_module
  --without-stream_split_clients_module
                                     disable ngx_stream_split_clients_module
  --without-stream_return_module     disable ngx_stream_return_module
  --without-stream_set_module        disable ngx_stream_set_module
  --without-stream_upstream_hash_module
                                     disable ngx_stream_upstream_hash_module
  --without-stream_upstream_least_conn_module
                                     disable ngx_stream_upstream_least_conn_module
  --without-stream_upstream_random_module
                                     disable ngx_stream_upstream_random_module
  --without-stream_upstream_zone_module
                                     disable ngx_stream_upstream_zone_module

  --with-google_perftools_module     enable ngx_google_perftools_module
  --with-cpp_test_module             enable ngx_cpp_test_module

  --add-module=PATH                  enable external module
  --add-dynamic-module=PATH          enable dynamic external module

  --with-compat                      dynamic modules compatibility

  --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-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 optimized
                                     for 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


预编译:
预编译主要是用来检查系统环境是否满足安装软件包的条件,并生成Makefile文件,
改文件为编译,安装,升级nginx指明了相应参数。

[root@anonymous nginx-1.20.1]# ./configure
./configure --help        可以查看预编译参数
--prefix=PATH        指定nginx编译安装的目录
--user=            指定nginx的属主
--group=            指定nginx的属组
--with-***            指定编译的模块
--without-***        指定不编译的模块
--add-module        编译第三方模块

开始预编译
[root@anonymous nginx-1.20.1]# ./configure --prefix=/usr/local/nginx  --user=nginx  --group=nginx --with-http_stub_status_module  --with-http_ssl_module
编译并安装nginx
[root@anonymous nginx-1.20.1]# make&make install
给启动文件做个链接文件
[root@anonymous nginx-1.20.1]# ln -s /usr/local/nginx/sbin/nginx /usr/sbin/
检查配置文件语法错误
[root@anonymous nginx-1.20.1]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
查看nginx的版本
[root@anonymous nginx-1.20.1]# nginx -v
nginx version: nginx/1.20.1
查看nginx的详细信息,版本和编译的参数等
[root@anonymous nginx-1.20.1]# nginx -V
nginx version: nginx/1.20.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module


启动nginx
[root@anonymous nginx-1.20.1]# nginx
查看nginx进程
[root@anonymous nginx-1.20.1]# ps -ef |grep nginx
root       1834      1  0 04:10 ?        00:00:00 nginx: master process nginx
nginx      1835   1834  0 04:10 ?        00:00:00 nginx: worker process
root       5072   1676  0 04:59 pts/0    00:00:00 grep --color=auto nginx
查看nginx端口
[root@anonymous nginx-1.20.1]# netstat -anptul|grep :80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1834/nginx: master

-s 发送信号给master处理
立刻停止nginx服务,不管请求是否完成
[root@anonymous nginx-1.20.1]# nginx -s stop
优雅的退出服务,处理完当前的请求退出
[root@anonymous nginx-1.20.1]# nginx -s quit
重新打开日志文件,原日志文件要提前备份改名
[root@anonymous nginx-1.20.1]# nginx -s reopen
重新加载配置文件
[root@anonymous nginx-1.20.1]# nginx -s reload

重新打开日志文件,用用做日志切割,定期执行。
[root@anonymous nginx-1.20.1]# nginx
[root@anonymous nginx-1.20.1]# ls /usr/local/nginx/logs/
access.log  error.log  nginx.pid
[root@anonymous nginx-1.20.1]# mv /usr/local/nginx/logs/access.log{,.bak}
[root@anonymous nginx-1.20.1]# ls /usr/local/nginx/logs/
access.log.bak  error.log  nginx.pid
[root@anonymous nginx-1.20.1]# nginx -s reopen
[root@anonymous nginx-1.20.1]# ls /usr/local/nginx/logs/
access.log  access.log.bak  error.log  nginx.pid

启动时指定配置文件:
[root@anonymous nginx-1.20.1]# mkdir /data/
[root@anonymous nginx-1.20.1]# cp /usr/local/nginx/conf/nginx.conf /data/
 修改用户名为Bob,工作进程数为3
[root@anonymous nginx-1.20.1]# vim /data/nginx.conf

user  bob;
worker_processes  3;

[root@anonymous nginx-1.20.1]# ps -ef |grep nginx
root       5156      1  0 05:36 ?        00:00:00 nginx: master process nginx -c /data/nginx.conf
bob        5157   5156  0 05:36 ?        00:00:00 nginx: worker process
bob        5158   5156  0 05:36 ?        00:00:00 nginx: worker process
bob        5159   5156  0 05:36 ?        00:00:00 nginx: worker process
root       5161   1676  0 05:36 pts/0    00:00:00 grep --color=auto nginx



make clean:重新预编译时,通常执行这条命令删除上次的编译文件
make build:编译,默认参数,可省略build参数
make install:安装
make modules:编译模块
make upgrade:在线升级

这篇关于nginx的安装的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!