Nginx 安装&默认配置简介

本文详细介绍了Nginx的三种安装方式:通过yum、mac的brew以及Linux下的编译安装过程。涵盖了依赖包安装、配置文件说明、启动与停止操作等关键信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

一.  yum 安装

二. mac 安装

三. Linux编译安装

1.下载

2.安装先安装nginx依赖的包

gc

PCRE

zlib

openssl

3.上传nginx到linux

4. 解压拷贝

5.安装

进入nginx的目录

创建nginx的安装目录

运行configure

make编译

make install编译安装

6. Nginx的目录说明

7. Nginx的​启动、停止

启动

停止

刷新[当用户修改了conf/nginx.conf]

四. 默认配置说明


 

一.  yum 安装

rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

yum install -y nginx

 

配置文件目录:

/etc/nginx/conf.d

 

二. mac 安装

 

查看: brew search nginx

安装: brew install nginx

nginx安装路径
/usr/local/Cellar/nginx/1.6.2

发布页面:
/usr/local/var/www 
配置文件:
/usr/local/etc/nginx/nginx.conf 

日志路径:
/usr/local/var/log/nginx/error.log

 

三. Linux编译安装

1.下载

http://nginx.org/en/download.html

 

 

 

2.安装先安装nginx依赖的包


gc

      安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc

yum install gcc-c++

PCRE

   PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。

yum install -y pcre pcre-devel

zlib

zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。

yum install -y zlib zlib-devel

openssl

      OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。 
      nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。

yum install -y openssl openssl-devel

3.上传nginx到linux

 

4. 解压拷贝

#把解压nginx-1.16.1.tar.gz包

tar -zxvf nginx-1.16.1.tar.gz

#修改文件夹的名字

mv nginx-1.16.1 nginx

#把nginx拷贝到/usr/local/src里面

cp -r nginx /usr/local/src

 

5.安装

进入nginx的目录

cd /usr/local/src/nginx

 

创建nginx的安装目录

mkdir  /usr/nginx

 

运行configure

./configure --prefix=/usr/nginx    (指定安装目录编译)

make编译

cd /usr/local/src/nginx

make

make install编译安装

cd /usr/local/src/nginx

make  install

 

6. Nginx的目录说明

conf 配置目录 

html静态文件[cdn加速]

logs日志目录

sbin执行文件

 

7. Nginx的​启动、停止

启动

cd /usr/nginx/sbin

./nginx  

停止

nginx -s stop

刷新[当用户修改了conf/nginx.conf]

./nginx -s reload

 

四. 默认配置说明

 


#代表权限
#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;

        #当用户访问 http://ip:80/
        location / {
            root   html;  #当匹配当"/"这个规则时指向 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;
        # 当服务出现 500 错误时 定向位置
        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;
    #    }
    #}

}

 

 

在Ubuntu环境下安装配置Nginx可以通过以下步骤完成: ### 安装Nginx 1. **更新包列表** ```bash sudo apt update ``` 2. **安装Nginx** ```bash sudo apt install nginx ``` 3. **启动Nginx** ```bash sudo systemctl start nginx ``` 4. **设置Nginx开机自启** ```bash sudo systemctl enable nginx ``` ### 配置Nginx 1. **默认配置文件位置** Nginx默认配置文件位于 `/etc/nginx/nginx.conf`。通常,我们会在 `/etc/nginx/sites-available/` 目录下创建新的站点配置文件,并在 `/etc/nginx/sites-enabled/` 目录下创建符号链接来启用站点。 2. **创建新的站点配置文件** ```bash sudo nano /etc/nginx/sites-available/mywebsite ``` 3. **添加基本配置** 在打开的编辑器中,添加以下基本配置: ```nginx server { listen 80; server_name your_domain_or_IP; root /var/www/mywebsite/html; index index.html index.htm; location / { try_files $uri $uri/ =404; } } ``` 4. **保存并关闭文件** 按 `Ctrl + O` 保存文件,然后按 `Ctrl + X` 关闭编辑器。 5. **创建符号链接以启用站点** ```bash sudo ln -s /etc/nginx/sites-available/mywebsite /etc/nginx/sites-enabled/ ``` 6. **测试Nginx配置** 在重新加载Nginx之前,测试配置是否正确: ```bash sudo nginx -t ``` 7. **重新加载Nginx** 如果测试通过,重新加载Nginx以应用更改: ```bash sudo systemctl reload nginx ``` ### 设置防火墙 1. **允许HTTP和HTTPS流量** 如果你使用的是 `ufw` 防火墙,确保允许HTTP和HTTPS流量: ```bash sudo ufw allow 'Nginx Full' ``` ### 验证安装 1. **在浏览器中访问服务器** 打开浏览器,访问你的服务器的IP地址或域名,你应该会看到Nginx默认欢迎页面。 通过以上步骤,你已经成功在Ubuntu环境下安装配置Nginx
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值