Linux与云计算——第二阶段Linux服务器架设
第七章:网站WEB服务器架设—Nginx(上)
安装Nginx的LNMP环境
[1] 安装 Nginx.
[root@server ~]# yum -y install nginx
[2] 完成基础配置.
[root@server ~]# vim /etc/nginx/nginx.conf
# line 38: 修改主机名
server_name www.example.com;
[root@server ~]# systemctl start nginx
[root@server ~]# systemctl enable nginx
[3] 使用客户端访问可以看到默认页面.
虚拟主机
虚拟主机配置.
[1] Configure Nginx.
[root@server ~]# vim /etc/nginx/conf.d/virtual.host.conf
# 创建一个新文件
server {
listen 80;
server_name www.virtual.com;
location / {
root /usr/share/nginx/virtual.host;
index index.html index.htm;
}
}
[root@server ~]# mkdir /usr/share/nginx/virtual.host
[root@server ~]# systemctl restart nginx
[2] 创建一个测试页面
[root@server ~]# vi /usr/share/nginx/virtual.host/index.html
<html>
<body>
<div style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;">
Nginx Virtual Host Test Page
</div>
</body>
</html>
用户目录
[1] 配置Nginx.
[root@server ~]# vim /etc/nginx/nginx.conf
# 添加到 "server" 章节
location ~ ^/~(.+?)(/.*)?$ {
alias /home/$1/public_html$2;
index index.html index.htm;
autoindex on;
}
[root@server ~]# systemctl restart nginx
[2] 创建一个测试页面.
[jeffrey@server ~]$ chmod 711 /home/cent
[jeffrey@server ~]$ mkdir ~/public_html
[jeffrey@server ~]$ chmod 755 ~/public_html
[jeffrey@server ~]$ vi ~/public_html/index.html
<html>
<body>
<div style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;">
Nginx UserDir Test Page
</div>
</body>
</html>
SSL
[1] 创建证书文件
[2] 配置Nginx
[root@server ~]# vim /etc/nginx/nginx.conf
# 添加到"server" 部分
server {
listen 80 default_server;
listen [::]:80 default_server;
listen 443 ssl;
server_name www.example.com;
root /usr/share/nginx/html;
ssl_certificate /etc/pki/tls/certs/server.crt;
ssl_certificate_key /etc/pki/tls/certs/server.key;
[root@server ~]# systemctl restart nginx
[3] 使用https访问.
2.5 基础认证
[1] 在本实验中,访问/auth-basic将要求进行身份验证。
[root@server ~]# yum -y install httpd-tools
[root@server ~]# vim /etc/nginx/nginx.conf
# 添加到 "server" 部分
location /auth-basic {
auth_basic "Basic Auth";
auth_basic_user_file "/etc/nginx/.htpasswd";
}
[root@server ~]# htpasswd -c /etc/nginx/.htpasswd jeffrey
[root@client ~]# systemctl restart nginx
[2] 通过客户端访问对应目录时候需要提供身份认证。
转载于:https://blog.51cto.com/11840455/1835313