如何在Ubuntu 18.04 LTS中设置nginx服务器块
在我们之前的教程中,我们讨论了如何在Ubuntu 18.04 LTS中配置Apache虚拟主机。
今天,在本教程中,我们将学习在Ubuntu 18.04中设置Nginx Server块。
服务器块类似于Apache中的虚拟主机,可在单个服务器上托管多个/域。
我的测试盒IP地址是192.168.225.24,主机名是ubuntuserver。
Ubuntu 18.04 LTS的设置nginx服务器块
确保我们已将Ubuntu系统更新为最新版本。
$sudo apt-get update
1.安装nginx web服务器
要在Ubuntu上安装nginx webserver,请运行:
$sudo apt-get install nginx
一旦安装了nginx,测试是否通过浏览浏览器中的nginx测试页而无法使用。
打开Web浏览器并将其指向http://ip_address或者http://localhost。
我们应该看到如下页面。
好的! nginx webserver正在工作!
2.为每个主机创建Web目录
我将创建两个服务器块,即theitroad1.lan和theitroad2.lan。
让我们创建一个用于First Server块theitroad1.lan的目录。
存储服务器块的数据需要此目录。
为此,请输入:
$sudo mkdir -p /var/www/html/theitroad1.lan/public_html
同样,为第二个服务器块INITOAD2.LAN创建一个目录,如下所示。
$sudo mkdir -p /var/www/html/theitroad2.lan/public_html
以上两个目录由root用户拥有。
我们需要将所有权更改为常规用户。
为此,运行:
$sudo chown -R $USER:$USER /var/www/html/theitroad1.lan/public_html
$sudo chown -R $USER:$USER /var/www/html/theitroad2.lan/public_html
其中$用户指的是当前登录的用户。
接下来,将读取权限设置为nginx根目录:/var/www/html /使用命令:
$sudo chmod -R 755 /var/www/html/
我们这样做是因为我们已经为每个服务器块创建了一个单独的目录,用于存储其数据。
因此,我们将nginx根目录制作,只为root用户的所有用户只读。
我们已经创建了所需目录,用于存储每个服务器块的数据,设置正确的权限。
现在,是时候创建一些将从每个服务器块提供服务的示例页面了。
3.为每个主机创建演示网页
让我们为theitroad1.lan站点创建一个示例页面。
为此,运行:
$sudo vi /var/www/html/theitroad1.lan/public_html/index.html
添加以下行:
<html> <head> <title>www.theitroad.lan</title> </head> <body> <h1>Hello, This is a test page for theitroad1.lan website</h1> </body> </html>
保存并关闭文件。
同样,为theitroad2.lan站点创建一个示例页面:
$sudo vi /var/www/html/theitroad2.lan/public_html/index.html
添加以下行:
<html> <head> <title>www.theitroad.lan</title> </head> <body> <h1>Hello, This is a test page for theitroad2.lan website</h1> </body> </html>
保存并关闭文件。
4.为每个主机创建配置文件
接下来,我们需要为每个服务器块创建配置文件。
首先,让我们为theitroad1.Lan执行此操作。
将默认服务器块配置文件的内容复制到如下所示的新服务器块文件。
$sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/theitroad1.lan.conf
$sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/theitroad2.lan.conf
接下来,编辑theitroad.lan1.conf文件:
$sudo vi /etc/nginx/sites-available/theitroad1.lan.conf
在server_name和root指令中进行必要的更改以与第一域名匹配。
所有更改都以下面的粗体字母显示。
# 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/theitroad1.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 theitroad1.lan www.theitroad1.lan; location/{ # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/=404; }
保存并关闭文件。
接下来,编辑theitroad2.lan.conf文件:
$sudo vi /etc/nginx/sites-available/theitroad2.lan.conf
在server_name和root指令中进行必要的更改,以反映到第二个域名。
所有更改都以粗体字母显示。
# 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/theitroad2.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 theitroad2.lan www.theitroad2.lan; location/{ # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/=404; }
正如我们在上面的配置中发现的那样,我更改了一个。
我删除了listen80中的default_server行;听[::]:80;指令在第二个服务器块(onitoroad2.lan)文件中。
因为,我们已经在第一台服务器块中使用了default_server行,因此我们从第二个文件中删除了它以避免冲突。
保存/关闭文件。
5.启用nginx服务器块
进行必要的更改后,禁用默认服务器块配置文件,并启用所有新创建的服务器块配置文件,如下所示。
$sudo rm /etc/nginx/sites-enabled/default
$sudo ln -s /etc/nginx/sites-available/theitroad1.lan.conf /etc/nginx/sites-enabled/
$sudo ln -s /etc/nginx/sites-available/theitroad2.lan.conf /etc/nginx/sites-enabled/
重新启动nginx服务以生效更改。
$sudo systemctl restart nginx
我们在nginx中成功配置了服务器块。
让我们继续检查他们是否正在工作。
6.测试nginx服务器块
在任何编辑器中打开/etc/hosts文件:
$sudo vi /etc/hosts
将所有虚拟/域一个逐个添加一个。
[...] 192.168.225.24 theitroad1.lan 192.168.225.24 theitroad2.lan [...]
请注意,如果要从任何远程系统访问服务器块,则必须在每个远程系统的/etc/hosts文件中添加上述行。
保存并关闭文件。
打开Web浏览器并将其指向http://theitroad1.lan或者http://theitroad2.lan。