CentOS7安装nginx

释放双眼,带上耳机,听听看~!

1.安装先决条件:

1.1.安装nginx前,查看是否关闭防火墙和selinux安全 


1
2
3
4
5
6
7
8
9
1[root@node2 ~]# getenforce
2Permissive
3[root@node2 ~]# systemctl status firewalld
4● firewalld.service - firewalld - dynamic firewall daemon
5   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
6   Active: inactive (dead)
7     Docs: man:firewalld(1)
8
9

1.2 .安装yum-utils,这是一个与yum集成的实用程序集合,通过多种方式扩展其本机特性


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
1[root@node2 ~]# yum install yum-utils
2...
3依赖关系解决
4
5=================================================================================
6 Package               架构          版本                    源             大小
7=================================================================================
8正在安装:
9 yum-utils             noarch        1.1.31-45.el7           cnetos        119 k
10为依赖而安装:
11 libxml2-python        x86_64        2.9.1-6.el7_2.3         cnetos        247 k
12 python-chardet        noarch        2.2.1-1.el7_1           cnetos        227 k
13 python-kitchen        noarch        1.1.1-5.el7             cnetos        267 k
14
15事务概要
16=================================================================================
17安装  1 软件包 (+3 依赖软件包)
18
19...
20
21

 2.编辑/etc/yum.repos.d/nginx.repo,设置yum存储库


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1[root@node2 ~]# vi /etc/yum.repos.d/nginx.repo
2[nginx-stable]
3name=nginx stable repo
4baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
5gpgcheck=1
6enabled=1
7gpgkey=https://nginx.org/keys/nginx_signing.key
8
9[nginx-mainline]
10name=nginx mainline repo
11baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
12gpgcheck=1
13enabled=0
14gpgkey=https://nginx.org/keys/nginx_signing.key
15
16

3.是否使用主线nginx包,这里就使用最新版本的nginx包,默认情况下,使用稳定nginx包的存储库


1
2
3
1[root@node2 ~]# yum-config-manager --enable nginx-mainline
2
3

4.安装nginx


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1[root@node2 ~]# yum install nginx
2...
3依赖关系解决
4
5=================================================================================
6 Package     架构         版本                        源                    大小
7=================================================================================
8正在安装:
9 nginx       x86_64       1:1.17.2-1.el7.ngx          nginx-mainline       767 k
10
11事务概要
12=================================================================================
13安装  1 软件包
14...
15

5.查看版本信息


1
2
3
4
1[root@node2 ~]# nginx -v
2nginx version: nginx/1.17.2
3
4

6.更改所拥有者和所属组


1
2
3
4
5
6
7
8
1[root@node2 ~]# whereis nginx        #用于查找二进制文件、源代码文件和man手册页
2nginx: /usr/sbin/nginx /usr/lib64/nginx /etc/nginx /usr/share/nginx /usr/share/man/man8/nginx.8.gz
3
4
5[root@node2 ~]# chown -R  nginx:nginx /etc/nginx/        #修改所有拥有者和所属组的nginx的配置文件
6
7[root@node2 ~]# chown -R  nginx:nginx /usr/share/nginx/html   #修改所有拥有者和所属组的nginx的页面存放位置
8

7.配置文件


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
1[root@node2 ~]# vi /etc/nginx/nginx.conf
2
3
4user  nginx;              #设置用户
5worker_processes  4;      #工作进程数,一般设置为cpu核心数
6
7error_log  /var/log/nginx/error.log warn;  #制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg
8pid        /var/run/nginx.pid;            #指定nginx进程运行文件存放地址
9
10
11events {
12    worker_connections  1024;   ##最大连接数,默认为512
13}
14
15
16http {
17    include       /etc/nginx/mime.types;      #文件扩展名与文件类型映射表
18    default_type  application/octet-stream;   #默认文件类型,默认为text/plain
19
20    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
21                      '$status $body_bytes_sent "$http_referer" '
22                      '"$http_user_agent" "$http_x_forwarded_for"';   #自定义格式
23
24    access_log  /var/log/nginx/access.log  main;  #combined为日志格式的默认值
25
26    sendfile        on;   #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
27    #tcp_nopush     on;
28
29    keepalive_timeout  65; #连接超时时间,默认为75s,可以在http,server,location块。
30
31    #gzip  on;
32
33    include /etc/nginx/conf.d/*.conf;
34
35server {
36    listen       800;                        #监听端口
37    server_name  localhost;                  #监听地址
38
39    location / {                              #首页
40        root   /usr/share/nginx/html;
41        index  index.html index.htm;
42    }
43    error_page   500 502 503 504  /50x.html;  #错误页
44        location = /50x.html {
45        root   /usr/share/nginx/html;
46    }
47 }
48
49}
50
51

7.1.端口被占用,需要修改端口 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
1[root@node2 ~]# systemctl -l status nginx.service    #启动nginx失败,端口被占用
2● nginx.service - nginx - high performance web server
3   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
4   Active: failed (Result: exit-code) since 二 2019-08-13 19:15:08 CST; 46s ago
5     Docs: http://nginx.org/en/docs/
6  Process: 2361 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=1/FAILURE)
7
88月 13 19:15:06 node2 nginx[2361]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
98月 13 19:15:06 node2 nginx[2361]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
108月 13 19:15:07 node2 nginx[2361]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
118月 13 19:15:07 node2 nginx[2361]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
128月 13 19:15:08 node2 nginx[2361]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
138月 13 19:15:08 node2 nginx[2361]: nginx: [emerg] still could not bind()
148月 13 19:15:08 node2 systemd[1]: nginx.service: control process exited, code=exited status=1
158月 13 19:15:08 node2 systemd[1]: Failed to start nginx - high performance web server.
168月 13 19:15:08 node2 systemd[1]: Unit nginx.service entered failed state.
178月 13 19:15:08 node2 systemd[1]: nginx.service failed.
18
19

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
1[root@node2 ~]# cd /etc/nginx/conf.d/
2[root@node2 conf.d]# vi default.conf
3server {
4    listen       800;            #修改监听端口
5    server_name  localhost;
6
7    #charset koi8-r;
8    #access_log  /var/log/nginx/host.access.log  main;
9
10    location / {
11        root   /usr/share/nginx/html;
12        index  index.html index.htm;
13    }
14
15    #error_page  404              /404.html;
16
17    # redirect server error pages to the static page /50x.html
18    #
19    error_page   500 502 503 504  /50x.html;
20    location = /50x.html {
21
22    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
23    #
24    #location ~ \.php$ {
25    #    proxy_pass   http://127.0.0.1;
26    #}
27
28    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
29    #
30    #location ~ \.php$ {
31    #    root           html;
32    #    fastcgi_pass   127.0.0.1:9000;
33    #    fastcgi_index  index.php;
34    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
35    #    include        fastcgi_params;
36    #}
37
38    # deny access to .htaccess files, if Apache's document root
39    # concurs with nginx's one
40    #
41    #location ~ /\.ht {
42    #    deny  all;
43    #}
44}
45
46
47

 8.启动nginx服务


1
2
3
4
5
6
1[root@node2 ~]# systemctl start nginx               #启动服务
2
3[root@node2 ~]# netstat -antp | grep 800            #查看端口
4tcp        0      0 0.0.0.0:800             0.0.0.0:*               LISTEN      2374/nginx: master  
5
6

9.用IP:端口,查看网页

CentOS7安装nginx


1
2
1  
2

给TA打赏
共{{data.count}}人
人已打赏
安全运维

WordPress网站专用docker容器环境带Waf

2020-7-18 20:04:44

安全运维

运维安全-Gitlab管理员权限安全思考

2021-9-19 9:16:14

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索