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