在 Linux 上安装 Nginx

戳底部名牌,一起玩耍

1. 准备工作

更新系统包

确保你的系统是最新的,并且安装了必要的依赖项:

  • 基于 Debian 的系统(如 Ubuntu)

    sudo apt update && sudo apt upgrade -y
    
  • 基于 Red Hat 的系统(如 CentOS 或 Fedora)

    对于 CentOS 7 及更早版本:

    sudo yum update -y
    

    对于 Fedora 和 CentOS 8 及更高版本:

    sudo dnf update -y
    
2. 安装 Nginx

使用官方仓库安装

大多数现代 Linux 发行版自带了 Nginx 的稳定版本,可以直接通过包管理器安装:

  • 基于 Debian 的系统(如 Ubuntu)

    sudo apt install nginx
    
  • 基于 Red Hat 的系统(如 CentOS 或 Fedora)

    对于 CentOS 7 及更早版本:

    sudo yum install epel-release
    sudo yum install nginx
    

    对于 Fedora 和 CentOS 8 及更高版本:

    sudo dnf install nginx
    

从源码编译安装(可选)

如果你需要特定的功能或最新的特性,可以从源码编译安装 Nginx。首先,下载最新版本的源代码并解压:

cd /usr/local/src
wget http://nginx.org/download/nginx-<version>.tar.gz
tar -zxf nginx-<version>.tar.gz
cd nginx-<version>

然后根据需求配置编译选项,例如启用 SSL 支持、HTTP2 等功能:

./configure --prefix=/usr/local/nginx \
            --sbin-path=/usr/sbin/nginx \
            --conf-path=/etc/nginx/nginx.conf \
            --error-log-path=/var/log/nginx/error.log \
            --http-log-path=/var/log/nginx/access.log \
            --pid-path=/var/run/nginx.pid \
            --with-http_ssl_module \
            --with-http_v2_module \
            --with-http_realip_module \
            --with-http_stub_status_module \
            --with-http_gzip_static_module \
            --with-file-aio \
            --with-threads
make
sudo make install
3. 启动并验证 Nginx

安装完成后,启动 Nginx 服务,并将其设置为开机自启:

  • 基于 Systemd 的系统

    sudo systemctl start nginx
    sudo systemctl enable nginx
    
  • 非 Systemd 系统(如较老的 CentOS 版本)

    sudo service nginx start
    sudo chkconfig nginx on
    

检查 Nginx 是否正确运行,可以通过浏览器访问服务器 IP 地址或域名来查看默认欢迎页面。你也可以使用命令行工具如 curl 来测试:

curl http://localhost

你应该会看到类似下面的输出,显示 Nginx 默认的欢迎信息:

DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!title>
...
html>
4. 配置 Nginx

Nginx 的主配置文件通常位于 /etc/nginx/nginx.conf。此外,每个站点的具体配置一般存放在 /etc/nginx/sites-available/ 目录下,而符号链接则放置在 /etc/nginx/sites-enabled/ 中以激活它们。

基本配置示例

以下是一个简单的 Nginx 配置文件示例,用于托管静态网站:

server {
    listen 80;
    server_name example.com www.example.com;

    root /var/www/html;
    index index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }

    error_log /var/log/nginx/example.error.log;
    access_log /var/log/nginx/example.access.log;
}

启用 HTTPS

为了提供安全连接,建议配置 SSL/TLS。你可以使用 Let’s Encrypt 免费获取证书,并使用 Certbot 工具自动完成配置:

sudo apt install certbot python3-certbot-nginx  # 对于基于 Debian 的系统
# 或者
sudo yum install certbot python3-certbot-nginx   # 对于基于 Red Hat 的系统

sudo certbot --nginx -d example.com -d www.example.com

Certbot 将引导你完成证书申请过程,并自动修改 Nginx 配置以启用 HTTPS。它还会设置定时任务来自动续订证书。

性能优化

根据你的应用场景和硬件条件,考虑对 Nginx 进行性能调优。这可能涉及到调整 worker 进程数、最大并发连接数、缓存策略等参数。请参考官方文档获取更多信息。

5. 设置防火墙规则

如果你的服务器上有防火墙(例如 UFW 或 firewalld),请确保开放 Nginx 默认使用的端口(80 和 443)。对于 UFW:

sudo ufw allow 'Nginx Full'  # 允许 HTTP 和 HTTPS 流量

对于 firewalld:

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

戳底部名牌,一起玩耍

你可能感兴趣的:(linux,nginx,运维)