Never too late to learn.

0%

CentOS 8 安装 配置 Nginx

本想把域名解析到阿里ecs上,结果浏览器输入域名之后,提示需要备案…

domain.jpg

嗯…好吧,我去备案还不行吗。而当我在阿里云上做备案准备时…

aliyun.jpg

??? 域名无法备案,而不备案又不让解析到国内服务器,这就是传说中的死锁?

无奈只好将域名解析到海外服务器,才有了这篇记录

环境

OS: CentOS 8

找nginx安装包

sudo yum search nginx
sudo yum list nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@centos-8 ~]# sudo yum search nginx
Last metadata expiration check: 0:42:37 ago on Sun 02 Feb 2020 02:30:34 AM UTC.
================================================================================= Name Exactly Matched: nginx =================================================================================
nginx.x86_64 : A high performance web server and reverse proxy server
================================================================================ Name & Summary Matched: nginx ================================================================================
nginx-mod-mail.x86_64 : Nginx mail modules
nginx-mod-stream.x86_64 : Nginx stream modules
collectd-nginx.x86_64 : Nginx plugin for collectd
nginx-mod-http-perl.x86_64 : Nginx HTTP perl module
nginx-mod-http-xslt-filter.x86_64 : Nginx XSLT module
nginx-mod-http-image-filter.x86_64 : Nginx HTTP image filter module
nginx-filesystem.noarch : The basic directory layout for the Nginx server
pcp-pmda-nginx.x86_64 : Performance Co-Pilot (PCP) metrics for the Nginx Webserver
nginx-all-modules.noarch : A meta package that installs all available Nginx modules
[root@centos-8 ~]# sudo yum list nginx
Last metadata expiration check: 0:42:53 ago on Sun 02 Feb 2020 02:30:34 AM UTC.
Available Packages
nginx.x86_64 1:1.14.1-9.module_el8.0.0+184+e34fea82 AppStream

获取要安装的nginx的版本信息

sudo yum info nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@centos-8 ~]# sudo yum info nginx
Last metadata expiration check: 0:43:14 ago on Sun 02 Feb 2020 02:30:34 AM UTC.
Available Packages
Name : nginx
Epoch : 1
Version : 1.14.1
Release : 9.module_el8.0.0+184+e34fea82
Arch : x86_64
Size : 570 k
Source : nginx-1.14.1-9.module_el8.0.0+184+e34fea82.src.rpm
Repo : AppStream
Summary : A high performance web server and reverse proxy server
URL : http://nginx.org/
License : BSD
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.

安装Nginx

sudo yum install nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
[root@centos-8 ~]# sudo yum install nginx
Last metadata expiration check: 0:46:56 ago on Sun 02 Feb 2020 02:30:34 AM UTC.
Dependencies resolved.
===============================================================================================================================================================================================
Package Arch Version Repository Size
===============================================================================================================================================================================================
Installing:
nginx x86_64 1:1.14.1-9.module_el8.0.0+184+e34fea82 AppStream 570 k
Installing dependencies:
gd x86_64 2.2.5-6.el8 AppStream 144 k
jbigkit-libs x86_64 2.1-14.el8 AppStream 55 k
libXpm x86_64 3.5.12-7.el8 AppStream 58 k
libjpeg-turbo x86_64 1.5.3-10.el8 AppStream 156 k
libtiff x86_64 4.0.9-15.el8 AppStream 188 k
libwebp x86_64 1.0.0-1.el8 AppStream 273 k
nginx-all-modules noarch 1:1.14.1-9.module_el8.0.0+184+e34fea82 AppStream 23 k
nginx-filesystem noarch 1:1.14.1-9.module_el8.0.0+184+e34fea82 AppStream 24 k
nginx-mod-http-image-filter x86_64 1:1.14.1-9.module_el8.0.0+184+e34fea82 AppStream 35 k
nginx-mod-http-perl x86_64 1:1.14.1-9.module_el8.0.0+184+e34fea82 AppStream 45 k
nginx-mod-http-xslt-filter x86_64 1:1.14.1-9.module_el8.0.0+184+e34fea82 AppStream 33 k
nginx-mod-mail x86_64 1:1.14.1-9.module_el8.0.0+184+e34fea82 AppStream 64 k
nginx-mod-stream x86_64 1:1.14.1-9.module_el8.0.0+184+e34fea82 AppStream 85 k
libxslt x86_64 1.1.32-3.el8 BaseOS 249 k
perl-Carp noarch 1.42-396.el8 BaseOS 30 k
perl-Errno x86_64 1.28-416.el8 BaseOS 76 k
perl-Exporter noarch 5.72-396.el8 BaseOS 34 k
perl-File-Path noarch 2.15-2.el8 BaseOS 38 k
perl-IO x86_64 1.38-416.el8 BaseOS 141 k
perl-PathTools x86_64 3.74-1.el8 BaseOS 90 k
perl-Scalar-List-Utils x86_64 3:1.49-2.el8 BaseOS 68 k
perl-Socket x86_64 4:2.027-3.el8 BaseOS 59 k
perl-Text-Tabs+Wrap noarch 2013.0523-395.el8 BaseOS 24 k
perl-Unicode-Normalize x86_64 1.25-396.el8 BaseOS 82 k
perl-constant noarch 1.33-396.el8 BaseOS 25 k
perl-interpreter x86_64 4:5.26.3-416.el8 BaseOS 6.3 M
perl-libs x86_64 4:5.26.3-416.el8 BaseOS 1.6 M
perl-macros x86_64 4:5.26.3-416.el8 BaseOS 72 k
perl-parent noarch 1:0.237-1.el8 BaseOS 20 k
perl-threads x86_64 1:2.21-2.el8 BaseOS 61 k
perl-threads-shared x86_64 1.58-2.el8 BaseOS 48 k
Enabling module streams:
nginx 1.14

Transaction Summary
===============================================================================================================================================================================================
Install 32 Packages

Total download size: 11 M
Installed size: 27 M
Is this ok [y/N]: y

输入y,确认下载安装

启动Nginx

允许开机启动

sudo systemctl enable nginx

1
2
[root@centos-8 ~]# sudo systemctl enable nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.

启动nginx服务

sudo systemctl start nginx

常用启动/停止/重启/重载/状态命令

sudo systemctl start/stop/restart/reload/status nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@centos-8 ~]# sudo systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2020-02-02 03:18:23 UTC; 25min ago
Process: 9466 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 9464 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 9463 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Main PID: 9468 (nginx)
Tasks: 2 (limit: 5092)
Memory: 6.9M
CGroup: /system.slice/nginx.service
├─9468 nginx: master process /usr/sbin/nginx
└─9469 nginx: worker process

Feb 02 03:18:23 centos-8 systemd[1]: Starting The nginx HTTP and reverse proxy server...
Feb 02 03:18:23 centos-8 nginx[9464]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Feb 02 03:18:23 centos-8 nginx[9464]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Feb 02 03:18:23 centos-8 systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Feb 02 03:18:23 centos-8 systemd[1]: Started The nginx HTTP and reverse proxy server.

使用firewall-cmd打开80,443端口

sudo firewall-cmd –permanent –zone=public –add-service=http –add-service=https
sudo firewall-cmd –reload
sudo firewall-cmd –list-services –zone=public

1
2
3
4
5
6
[root@centos-8 ~]# sudo firewall-cmd --permanent --zone=public --add-service=http --add-service=https
success
[root@centos-8 ~]# sudo firewall-cmd --reload
success
[root@centos-8 ~]# sudo firewall-cmd --list-services --zone=public
cockpit dhcpv6-client http https ssh

测试

sudo ss -tulpn

ip a

curl -I http://xx.xx.xx.xx
curl http://xx.xx.xx.xx

配置

CentOS 8 Nginx 配置文件目录 – /etc/nginx/
Nginx全局配置文件 – /etc/nginx/nginx.conf
TCP ports opened by Nginx – 80 (HTTP), 443 (HTTPS)
Nginx文件根目录 – /usr/share/nginx/html

sudo vi /etc/nginx/nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
[root@centos-8 ~]# sudo cat /etc/nginx/nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
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;

# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;

server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;

# Load configuration files for the default server block.
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 {
}
}

# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers PROFILE=SYSTEM;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# 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 {
# }
# }

}

可以上传html/css/js和图像文件到/usr/share/nginx/html/
编写自定义的配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
[root@centos-8 ~]# sudo cat /etc/nginx/nginx.conf.default 

#user nobody;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 1024;
}


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;

server {
listen 80;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root html;
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;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;

# location / {
# root html;
# index index.html index.htm;
# }
#}


# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;

# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;

# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;

# location / {
# root html;
# index index.html index.htm;
# }
#}

}

参考

How to install and use Nginx on CentOS 8

Coffee? ☕