CentOS7安装nginx

1.安装先决条件:

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

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

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

[root@node2 ~]# yum install yum-utils
...
依赖关系解决

=================================================================================
 Package               架构          版本                    源             大小
=================================================================================
正在安装:
 yum-utils             noarch        1.1.31-45.el7           cnetos        119 k
为依赖而安装:
 libxml2-python        x86_64        2.9.1-6.el7_2.3         cnetos        247 k
 python-chardet        noarch        2.2.1-1.el7_1           cnetos        227 k
 python-kitchen        noarch        1.1.1-5.el7             cnetos        267 k

事务概要
=================================================================================
安装  1 软件包 (+3 依赖软件包)

...

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

[root@node2 ~]# vi /etc/yum.repos.d/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

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

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

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

4.安装nginx

[root@node2 ~]# yum install nginx
...
依赖关系解决

=================================================================================
 Package     架构         版本                        源                    大小
=================================================================================
正在安装:
 nginx       x86_64       1:1.17.2-1.el7.ngx          nginx-mainline       767 k

事务概要
=================================================================================
安装  1 软件包
...

5.查看版本信息

[root@node2 ~]# nginx -v
nginx version: nginx/1.17.2

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

[root@node2 ~]# whereis nginx        #用于查找二进制文件、源代码文件和man手册页
nginx: /usr/sbin/nginx /usr/lib64/nginx /etc/nginx /usr/share/nginx /usr/share/man/man8/nginx.8.gz


[root@node2 ~]# chown -R  nginx:nginx /etc/nginx/        #修改所有拥有者和所属组的nginx的配置文件

[root@node2 ~]# chown -R  nginx:nginx /usr/share/nginx/html   #修改所有拥有者和所属组的nginx的页面存放位置

7.配置文件

[root@node2 ~]# vi /etc/nginx/nginx.conf 


user  nginx;              #设置用户
worker_processes  4;      #工作进程数,一般设置为cpu核心数

error_log  /var/log/nginx/error.log warn;  #制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg
pid        /var/run/nginx.pid;            #指定nginx进程运行文件存放地址


events {
    worker_connections  1024;   ##最大连接数,默认为512
}


http {
    include       /etc/nginx/mime.types;      #文件扩展名与文件类型映射表
    default_type  application/octet-stream;   #默认文件类型,默认为text/plain

    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;  #combined为日志格式的默认值

    sendfile        on;   #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
    #tcp_nopush     on; 

    keepalive_timeout  65; #连接超时时间,默认为75s,可以在http,server,location块。

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;

server {
    listen       800;                        #监听端口
    server_name  localhost;                  #监听地址 

    location / {                              #首页
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;  #错误页
        location = /50x.html {
        root   /usr/share/nginx/html;
    }
 }

}

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

[root@node2 ~]# systemctl -l status nginx.service    #启动nginx失败,端口被占用
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since 二 2019-08-13 19:15:08 CST; 46s ago
     Docs: http://nginx.org/en/docs/
  Process: 2361 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=1/FAILURE)

8月 13 19:15:06 node2 nginx[2361]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
8月 13 19:15:06 node2 nginx[2361]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
8月 13 19:15:07 node2 nginx[2361]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
8月 13 19:15:07 node2 nginx[2361]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
8月 13 19:15:08 node2 nginx[2361]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
8月 13 19:15:08 node2 nginx[2361]: nginx: [emerg] still could not bind()
8月 13 19:15:08 node2 systemd[1]: nginx.service: control process exited, code=exited status=1
8月 13 19:15:08 node2 systemd[1]: Failed to start nginx - high performance web server.
8月 13 19:15:08 node2 systemd[1]: Unit nginx.service entered failed state.
8月 13 19:15:08 node2 systemd[1]: nginx.service failed.
[root@node2 ~]# cd /etc/nginx/conf.d/
[root@node2 conf.d]# vi default.conf
server {
    listen       800;            #修改监听端口
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/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 {

    # 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;
    #}
}

 8.启动nginx服务

[root@node2 ~]# systemctl start nginx               #启动服务

[root@node2 ~]# netstat -antp | grep 800            #查看端口
tcp        0      0 0.0.0.0:800             0.0.0.0:*               LISTEN      2374/nginx: master  

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

  参考网站地址:http://nginx.org/en/linux_packages.html#RHEL-CentOS
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路来了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值