返回

Ubuntu系统中轻松安装Nginx服务

前端

前言

Nginx (读作“Engine X”) 是一款高性能的 HTTP 和反向代理服务器,以其高性能、稳定性和功能丰富而受到广泛欢迎。它可以处理高负载的并发连接,并提供各种功能,如负载均衡、动静分离、缓存等。在本文中,我们将向您展示如何在 Ubuntu 系统中安装和配置 Nginx 服务。

安装 Nginx

  1. 更新系统包
sudo apt update
  1. 安装 Nginx
sudo apt install nginx
  1. 检查 Nginx 是否安装成功
sudo systemctl status nginx

配置 Nginx

  1. 创建虚拟主机配置
sudo nano /etc/nginx/sites-available/example.com.conf

在文件中添加以下内容:

server {
    listen 80;
    server_name example.com;

    root /var/www/example.com;
    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.html;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    }
}
  1. 启用虚拟主机配置
sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/
  1. 检查 Nginx 配置是否正确
sudo nginx -t
  1. 重启 Nginx 服务
sudo systemctl restart nginx

测试 Nginx

在浏览器中输入您的域名或 IP 地址,如果出现“Welcome to nginx!”的页面,则表示 Nginx 已成功安装和配置。

结语

以上就是如何在 Ubuntu 系统中安装和配置 Nginx 的步骤。如果您有任何问题或建议,欢迎在下方评论区留言。