在我们之前的指南中,我们讨论了如何 配置 Apache Ubuntu 18.04 LTS 中的虚拟主机. 今天,在本指南中,我们将学习在 Ubuntu 18.04 中设置 Nginx 服务器块。 服务器块类似于虚拟主机 Apache 这可以帮助您在单个服务器上托管多个网站/域。 我的测试盒IP地址是 192.168.225.24 主机名是 Ubuntu服务器.
在 Ubuntu 18.04 LTS 中设置 Nginx 服务器块
确保您已将 Ubuntu 系统更新到最新版本。
$ sudo apt-get update
1. 安装 Nginx 网络服务器
要在 Ubuntu 上安装 Nginx 网络服务器,请运行:
$ sudo apt-get install nginx
安装好 Nginx 后,通过浏览器中的 nginx 测试页面来测试它是否工作。
打开您的网络浏览器并将其指向 https://IP_Address 或者 https://localhost. 您应该会看到如下所示的页面。
好的! Nginx 网络服务器已启动并正常工作!
2.为每个主机创建web目录
我将创建两个 Server 块,即 ostechnix1.lan 和 ostechnix2.lan.
让我们为第一个服务器块 ostechnix1.lan 创建一个目录。 该目录是存储我们的服务器块数据所必需的。
为此,请输入:
$ sudo mkdir -p /var/www/html/ostechnix1.lan/public_html
同样,为第二个服务器块 ostechnix2.lan 创建一个目录,如下所示。
$ sudo mkdir -p /var/www/html/ostechnix2.lan/public_html
以上两个目录为root用户所有。 我们需要将所有权更改为普通用户。
为此,请运行:
$ sudo chown -R $USER:$USER /var/www/html/ostechnix1.lan/public_html
$ sudo chown -R $USER:$USER /var/www/html/ostechnix2.lan/public_html
这里, $USER 指当前登录的用户。
接下来,使用命令设置对 Nginx 根目录的读取权限,即 /var/www/html/:
$ sudo chmod -R 755 /var/www/html/
我们这样做是因为我们已经为每个服务器块创建了一个单独的目录来存储它们的数据。 所以我们将 Nginx 根目录设置为只读,除了 root 用户之外的所有用户。
我们已经创建了存储每个服务器块数据所需的目录,设置了适当的权限。 现在,是时候创建一些将从每个服务器块提供的示例页面了。
3.为每个主机创建演示网页
让我们为 ostechnix1.lan 站点创建一个示例页面。 为此,请运行:
$ sudo vi /var/www/html/ostechnix1.lan/public_html/index.html
在其中添加以下行:
<html> <head> <title>www.ostechnix.lan</title> </head> <body> <h1>Hello, This is a test page for ostechnix1.lan website</h1> </body> </html>
Save 和 close 文件。
同样,为 ostechnix2.lan 站点创建一个示例页面:
$ sudo vi /var/www/html/ostechnix2.lan/public_html/index.html
在其中添加以下行:
<html> <head> <title>www.ostechnix.lan</title> </head> <body> <h1>Hello, This is a test page for ostechnix2.lan website</h1> </body> </html>
Save 和 close 文件。
4.为每个主机创建配置文件
接下来,我们需要为每个服务器块创建配置文件。 首先,让我们为 ostechnix1.lan 站点执行此操作。
将默认服务器块配置文件的内容复制到新的服务器块文件中,如下所示。
$ sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/ostechnix1.lan.conf
$ sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/ostechnix2.lan.conf
接下来,编辑 ostechnix.lan1.conf 文件:
$ sudo vi /etc/nginx/sites-available/ostechnix1.lan.conf
进行必要的更改 服务器名称 和 根 与第一个域名匹配的指令。 所有更改均以下面的粗体字显示。
# Default server configuration # server { listen 80 default_server; listen [::]:80 default_server; # SSL configuration # # listen 443 ssl default_server; # listen [::]:443 ssl default_server; # # Note: You should disable gzip for SSL traffic. # See: https://bugs.debian.org/773332 # # Read up on ssl_ciphers to ensure a secure configuration. # See: https://bugs.debian.org/765782 # # Self signed certs generated by the ssl-cert package # Don't use them in a production server! # # include snippets/snakeoil.conf; root /var/www/html/ostechnix1.lan/public_html; # Add index.php to the list if you are using PHP index index.html index.htm index.nginx-debian.html; server_name ostechnix1.lan www.ostechnix1.lan; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/ =404; }
Save 和 close 文件。
接下来,编辑 ostechnix2.lan.conf 文件:
$ sudo vi /etc/nginx/sites-available/ostechnix2.lan.conf
进行必要的更改 服务器名称 和 根 反映到第二个域名的指令。 所有更改均以粗体字显示。
# Default server configuration # server { listen 80; listen [::]:80; # SSL configuration # # listen 443 ssl default_server; # listen [::]:443 ssl default_server; # # Note: You should disable gzip for SSL traffic. # See: https://bugs.debian.org/773332 # # Read up on ssl_ciphers to ensure a secure configuration. # See: https://bugs.debian.org/765782 # # Self signed certs generated by the ssl-cert package # Don't use them in a production server! # # include snippets/snakeoil.conf; root /var/www/html/ostechnix2.lan/public_html; # Add index.php to the list if you are using PHP index index.html index.htm index.nginx-debian.html; server_name ostechnix2.lan www.ostechnix2.lan; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/ =404; }
正如您在上面的配置中可能注意到的那样,我又做了一项更改。 我删除了 默认服务器 中的线 听 80 ; 和 听 [::]:80 ; 第二个服务器块(ostechnix2.lan)文件中的指令。 因为,我们已经使用 默认服务器 第一个服务器块中的行,因此我们将其从第二个文件中删除以避免冲突。
Save/close 文件。
5.启用Nginx服务器块
进行必要的更改后,禁用默认的服务器块配置文件,并启用所有新创建的服务器块配置文件,如下所示。
$ sudo rm /etc/nginx/sites-enabled/default
$ sudo ln -s /etc/nginx/sites-available/ostechnix1.lan.conf /etc/nginx/sites-enabled/
$ sudo ln -s /etc/nginx/sites-available/ostechnix2.lan.conf /etc/nginx/sites-enabled/
重新启动 Nginx 服务以使更改生效。
$ sudo systemctl restart nginx
就是这样。 我们已经在 Nginx 中成功配置了服务器块。 让我们继续检查它们是否正常工作。
6.测试Nginx Server块
在任何编辑器中打开 /etc/hosts 文件:
$ sudo vi /etc/hosts
如下所示,一一添加您的所有虚拟网站/域。
[...] 192.168.225.24 ostechnix1.lan 192.168.225.24 ostechnix2.lan [...]
请注意,如果您想从任何远程系统访问服务器块,您必须在每个远程系统的 /etc/hosts 文件中添加上述行。 Save 和 close 文件。
打开您的网络浏览器并将其指向 https://ostechnix1.lan 或者 https://ostechnix2.lan.
ostechnix1.lan 测试页面:
ostechnix2.lan 测试页面:
恭喜! 您现在可以访问您的所有网站/域。 从现在开始,您可以上传数据并从不同的网站提供数据。
感谢您的光临!
帮助我们帮助您:
- 订阅我们的电子邮件通讯: 立即注册
- 支持 OSTechNix: 通过贝宝捐款
- 下载免费的电子书和视频: TradePub 上的 OSTechNix
- 联系我们: 红迪网 | Facebook | Twitter | 领英 | RSS订阅
祝你有美好的一天!!