nginx是一个高性能的http服务器端软件,
安装nginx通常会使用如下两种方法:
方法一、通过操作系统的包管理器进行安装。
方法二、在官方网站下载源代码,编译安装。
方法一和二的优缺点
方法一的缺点就是:我们在安装nginx时,无法自定义安装哪些"模块"。"模块"是个什么东西呢?说到"模块"就要提到一个概念了,nginx的结构是高度模块化的,不同的模块,拥有不同的功能,所以,如果我们想要在使用nginx时能够使用某些特定的功能,则必须要安装对应的模块才行,但是通过"方法一"安装的nginx包都是预先编译好的,哪些模块已经编译进去,哪些模块没有编译进去都是已经确定了的,所以,使用方法一的缺点就是没有办法自定义安装相应的模块,不过话说回来,通过"方法一"安装nginx以后,一些常用的,基本的模块也都是包含的,所以,我们在刚开始使用nginx时,可以使用这种方法快速方便的安装nginx。
而方法二的优缺点正好与方法一的优缺点相对应,方法二的优点就是我们可以自定义,哪些模块需要安装,哪些模块不需要安装,定制化比较高,如果想要使用第三方模块,则必须使用编译安装的方式,缺点就是编译安装,没有通过包管理器方便快捷,升级时也相对没有那么方便。
方法一:
[root@linuxprobe ~]# yum install -y nginx
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Last metadata expiration check: 1:37:02 ago on Sat 09 Jan 2021 04:36:47 PM CST.
Dependencies resolved.
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
nginx x86_64 1:1.14.1-8.module+el8+2505+fe936cef localREPO_APP 569 k
Installing dependencies:
nginx-all-modules
noarch 1:1.14.1-8.module+el8+2505+fe936cef localREPO_APP 23 k
nginx-filesystem
noarch 1:1.14.1-8.module+el8+2505+fe936cef localREPO_APP 24 k
nginx-mod-http-image-filter
x86_64 1:1.14.1-8.module+el8+2505+fe936cef localREPO_APP 34 k
nginx-mod-http-perl
x86_64 1:1.14.1-8.module+el8+2505+fe936cef localREPO_APP 45 k
nginx-mod-http-xslt-filter
x86_64 1:1.14.1-8.module+el8+2505+fe936cef localREPO_APP 33 k
nginx-mod-mail x86_64 1:1.14.1-8.module+el8+2505+fe936cef localREPO_APP 64 k
nginx-mod-stream
x86_64 1:1.14.1-8.module+el8+2505+fe936cef localREPO_APP 85 k
Transaction Summary
================================================================================
Install 8 Packages
Total size: 878 k
Installed size: 2.0 M
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Running scriptlet: nginx-filesystem-1:1.14.1-8.module+el8+2505+fe936cef 1/8
Installing : nginx-filesystem-1:1.14.1-8.module+el8+2505+fe936cef 1/8
Installing : nginx-mod-http-image-filter-1:1.14.1-8.module+el8+25 2/8
Running scriptlet: nginx-mod-http-image-filter-1:1.14.1-8.module+el8+25 2/8
Installing : nginx-mod-http-perl-1:1.14.1-8.module+el8+2505+fe936 3/8
Running scriptlet: nginx-mod-http-perl-1:1.14.1-8.module+el8+2505+fe936 3/8
Installing : nginx-mod-http-xslt-filter-1:1.14.1-8.module+el8+250 4/8
Running scriptlet: nginx-mod-http-xslt-filter-1:1.14.1-8.module+el8+250 4/8
Installing : nginx-mod-mail-1:1.14.1-8.module+el8+2505+fe936cef.x 5/8
Running scriptlet: nginx-mod-mail-1:1.14.1-8.module+el8+2505+fe936cef.x 5/8
Installing : nginx-all-modules-1:1.14.1-8.module+el8+2505+fe936ce 6/8
Installing : nginx-1:1.14.1-8.module+el8+2505+fe936cef.x86_64 7/8
Running scriptlet: nginx-1:1.14.1-8.module+el8+2505+fe936cef.x86_64 7/8
Installing : nginx-mod-stream-1:1.14.1-8.module+el8+2505+fe936cef 8/8
Running scriptlet: nginx-mod-stream-1:1.14.1-8.module+el8+2505+fe936cef 8/8
Verifying : nginx-1:1.14.1-8.module+el8+2505+fe936cef.x86_64 1/8
Verifying : nginx-all-modules-1:1.14.1-8.module+el8+2505+fe936ce 2/8
Verifying : nginx-filesystem-1:1.14.1-8.module+el8+2505+fe936cef 3/8
Verifying : nginx-mod-http-image-filter-1:1.14.1-8.module+el8+25 4/8
Verifying : nginx-mod-http-perl-1:1.14.1-8.module+el8+2505+fe936 5/8
Verifying : nginx-mod-http-xslt-filter-1:1.14.1-8.module+el8+250 6/8
Verifying : nginx-mod-mail-1:1.14.1-8.module+el8+2505+fe936cef.x 7/8
Verifying : nginx-mod-stream-1:1.14.1-8.module+el8+2505+fe936cef 8/8
Installed products updated.
Installed:
nginx-1:1.14.1-8.module+el8+2505+fe936cef.x86_64
nginx-all-modules-1:1.14.1-8.module+el8+2505+fe936cef.noarch
nginx-filesystem-1:1.14.1-8.module+el8+2505+fe936cef.noarch
nginx-mod-http-image-filter-1:1.14.1-8.module+el8+2505+fe936cef.x86_64
nginx-mod-http-perl-1:1.14.1-8.module+el8+2505+fe936cef.x86_64
nginx-mod-http-xslt-filter-1:1.14.1-8.module+el8+2505+fe936cef.x86_64
nginx-mod-mail-1:1.14.1-8.module+el8+2505+fe936cef.x86_64
nginx-mod-stream-1:1.14.1-8.module+el8+2505+fe936cef.x86_64
Complete!
查看下载安装的版本信息
[root@linuxprobe ~]# rpm -qi nginx
Name : nginx
Epoch : 1
Version : 1.14.1
Release : 8.module+el8+2505+fe936cef
Architecture: x86_64
Install Date: Sat 09 Jan 2021 06:13:51 PM CST
Group : System Environment/Daemons
Size : 1734589
License : BSD
Signature : RSA/SHA256, Fri 14 Dec 2018 09:46:48 AM CST, Key ID 199e2f91fd431d51
Source RPM : nginx-1.14.1-8.module+el8+2505+fe936cef.src.rpm
Build Date : Fri 14 Dec 2018 09:08:40 AM CST
Build Host : x86-vm-06.build.eng.bos.redhat.com
Relocations : (not relocatable)
Packager : Red Hat, Inc. <http://bugzilla.redhat.com/bugzilla>
Vendor : Red Hat, Inc.
URL : http://nginx.org/
Summary : A high performance web server and reverse proxy server
Description :
Nginx is a web server and a reverse proxy server for HTTP, SMTP, POP3 and
IMAP protocols, with a strong focus on high concurrency, performance and low
memory usage.
[root@linuxprobe ~]#
查看yum安装的各个配置文件的目录结构
[root@linuxprobe ~]# rpm -ql nginx
/etc/logrotate.d/nginx
/etc/nginx/fastcgi.conf
/etc/nginx/fastcgi.conf.default
/etc/nginx/fastcgi_params
/etc/nginx/fastcgi_params.default
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/mime.types.default
/etc/nginx/nginx.conf
/etc/nginx/nginx.conf.default
/etc/nginx/scgi_params
/etc/nginx/scgi_params.default
/etc/nginx/uwsgi_params
/etc/nginx/uwsgi_params.default
/etc/nginx/win-utf
/usr/bin/nginx-upgrade
/usr/lib/.build-id
/usr/lib/.build-id/da
/usr/lib/.build-id/da/69b2987b23fef9703639bab74a4a68f6fcfa6f
/usr/lib/systemd/system/nginx.service
/usr/lib64/nginx/modules
/usr/sbin/nginx
/usr/share/doc/nginx
/usr/share/doc/nginx/CHANGES
/usr/share/doc/nginx/README
/usr/share/doc/nginx/README.dynamic
/usr/share/licenses/nginx
/usr/share/licenses/nginx/LICENSE
/usr/share/man/man3/nginx.3pm.gz
/usr/share/man/man8/nginx-upgrade.8.gz
/usr/share/man/man8/nginx.8.gz
/usr/share/nginx/html/404.html
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html
/usr/share/nginx/html/nginx-logo.png
/usr/share/nginx/html/poweredby.png
/usr/share/vim/vimfiles/ftdetect/nginx.vim
/usr/share/vim/vimfiles/indent/nginx.vim
/usr/share/vim/vimfiles/syntax/nginx.vim
/var/lib/nginx
/var/lib/nginx/tmp
/var/log/nginx
[root@linuxprobe ~]#
二进制编译安装
下载压缩包
wget http://nginx.org/download/nginx-1.14.2.tar.gz
解压
tar -xvf nginx-1.14.2.tar.gz
安装依赖插件
yum install -y gcc-c++
yum install -y pcre-devel
yum install -y zlib-devel
yum install -y openssl-devel
yum install -y libxslt-devel
yum install -y GeoIP-devel
yum install -y perl-ExtUtils-Embed
创建目录并移动目录
mkdir -p /nginx
mv /root/nginx-1.14.2/* /nginx/
现在开始执行configure 脚本
./configure --prefix=/nginx --with-file-aio --with-http_auth_request_module --with-http_ssl_module --with-http_v2_module
或者使用更多的参数
./configure --user=www --group=www --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/usr/local/nginx/logs/error.log --http-log-path=/usr/local/nginx/logs/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/lock/subsys/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre
编译 make
安装 make install
查看nginx的目录结构
[root@myserver_2 nginx]# pwd
/nginx
[root@myserver_2 nginx]# ll
total 744
drwxr-xr-x 6 1001 1001 4096 Jan 9 17:21 auto
-rw-r--r-- 1 1001 1001 288742 Dec 4 2018 CHANGES
-rw-r--r-- 1 1001 1001 440121 Dec 4 2018 CHANGES.ru
drwxr-xr-x 2 1001 1001 4096 Jan 9 17:49 conf
-rwxr-xr-x 1 1001 1001 2502 Dec 4 2018 configure
drwxr-xr-x 4 1001 1001 72 Jan 9 17:21 contrib
drwxr-xr-x 2 1001 1001 40 Jan 9 17:21 html
-rw-r--r-- 1 1001 1001 1397 Dec 4 2018 LICENSE
-rw-r--r-- 1 root root 336 Jan 9 17:39 Makefile
drwxr-xr-x 2 1001 1001 21 Jan 9 17:21 man
drwxr-xr-x 3 root root 4096 Jan 9 17:40 objs
-rw-r--r-- 1 1001 1001 49 Dec 4 2018 README
drwxr-xr-x 2 root root 19 Jan 9 17:41 sbin
drwxr-xr-x 9 1001 1001 91 Jan 9 17:21 src
[root@myserver_2 nginx]#
yum安装的默认配置和二进制编译安装的配置文件有区别
[root@linuxprobe nginx]# egrep -v '#|^$' nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
二进制编译安装的配置文件
[root@myserver_2 nginx]# egrep -v '^$|#' conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
[root@myserver_2 nginx]#
默认启动方式有区别
yum 安装的可以在任意目录下nginx
[root@linuxprobe ~]# nginx
[root@linuxprobe ~]# ps -ef|grep nginx
root 64882 1 0 18:31 ? 00:00:00 nginx: master process nginx
nginx 64883 64882 0 18:31 ? 00:00:00 nginx: worker process
nginx 64884 64882 0 18:31 ? 00:00:00 nginx: worker process
root 64886 58588 0 18:31 pts/1 00:00:00 grep --color=auto nginx
[root@linuxprobe ~]#
二进制默认启动当时要指定路径下
启动报错(nginx: [alert] could not open error log file: open())
[root@myserver_2 sbin]# ./nginx
nginx: [alert] could not open error log file: open() "/nginx/logs/error.log" failed (2: No such file or directory)
2021/01/09 18:33:13 [emerg] 23594#0: open() "/nginx/logs/access.log" failed (2: No such file or directory)
创建目录即可
[root@myserver_2 sbin]# mkdir -p /nginx/logs/
[root@myserver_2 sbin]# ./nginx
[root@myserver_2 sbin]# ps -ef|grep nginx
root 23605 1 0 18:34 ? 00:00:00 nginx: master process ./nginx
nobody 23606 23605 0 18:34 ? 00:00:00 nginx: worker process
root 23608 17737 0 18:34 pts/1 00:00:00 grep --color=auto nginx
[root@myserver_2 sbin]# pwd
/nginx/sbin
[root@myserver_2 sbin]#
二进程安装之后也可以直接nginx启动,但是要在配置环境变量
通过yum源安装nginx后,nignx二进制文件默认会放在/usr/sbin/目录中,而这个目录正好属于默认环境变量PATH变量中的一部分,所以,如果你通过编译安装的方式安装了nginx,也可以将nginx的安装目录中的/sbin目录配置到PATH环境变量中,以便在任何路径中都可以直接使用nginx命令,以我的编译安装环境作为示例,步骤如下:
1、创建/etc/profile.d/nginx.sh文件,以便写入环境变量配置。
2、在/etc/profile.d/nginx.sh文件中写入如下内容:
nginx_home=/nginx
PATH=$nginx_home/sbin:$PATH
export PATH
3、使用如下命令重载刚才的配置
# source /etc/profile
nginx常用的命令:
不加选项:启动nginx服务
-h或-?选项:查看nginx命令帮助
-v选项:查看nginx版本信息
-V选项:查看nginx编译信息
-c选项:启动nginx时指定主配置文件
-t或-T选项:测试nginx.conf文件是否存在语法错误
-s选项:向正在运行的nginx主进程发送信号,信号的可用值有stop, quit, reopen, reload