欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

CentOS 7安装nginx

程序员文章站 2024-02-21 09:45:10
...

1 配置yum本地nginx仓库

执行命令vim /etc/yum.repos.d/nginx.repo编辑ngnix.repo

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
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

说明:

name= #一个描述,随意。
baseurl= #设置资源库的地址
gpkcheck=0 表示对从这个源下载的rpm包不进行校验 , 1是校验;
enable=1 表示启用这个源 , 0是禁用。

2 启用配置

若启用主线:

yum-config-manager --enable nginx-mainline

若启用稳定版:

yum-config-manager --enable nginx-stable

3 执行安装

yum install ngnix

4 查看安装位置&版本

安装位置

whereis nginx

版本

 nginx -v

5 nginx配置文件

配置文件:/etc/nginx/nginx.conf
默认配置文件:/etc/nginx/default.conf

vim /etc/nginx/nginx.conf

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进程运行文件存放地址
 
daemon on;	#配置是否后台启动,如需取消,则不配置
 
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;
    }
 }
 
}

6 启动等nginx命令

启动nginx

systemctl start nginx

停止nginx

systemctl stop nginx

查看nginx启动状态

systemctl status nginx
参考文献

https://blog.csdn.net/MacWx/article/details/98174136
https://blog.csdn.net/qq_41709494/article/details/99455441

相关标签: 服务器运维