Linux进阶之Web服务器

Linux 是运行 Web 服务器的理想操作系统。通过配置 Apache 和 Nginx,可以实现网站的托管、虚拟主机配置、反向代理以及 SSL/HTTPS 支持等功能。
本章将详细讲解如何安装与配置 Apache 和 Nginx,并重点介绍虚拟主机与 HTTPS 配置。


一、Apache Web 服务器

1、Apache 简介

Apache 是一种流行的开源 Web 服务器软件,支持多种功能模块,具有高度可扩展性。


2、安装 Apache

在 Ubuntu 上安装

sudo apt update
sudo apt install apache2 -y

验证安装
启动 Apache 服务:

sudo systemctl start apache2
sudo systemctl enable apache2

在浏览器中访问 http://<服务器IP>,应显示默认的 Apache 欢迎页面。


3、配置虚拟主机

概念
虚拟主机允许在同一服务器上托管多个独立的网站。

创建虚拟主机配置

sudo nano /etc/apache2/sites-available/example.com.conf

添加以下内容:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com

    <Directory /var/www/example.com>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
    CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>

启用配置

sudo mkdir -p /var/www/example.com
sudo a2ensite example.com.conf
sudo systemctl reload apache2

测试虚拟主机
/var/www/example.com 下创建一个 index.html,然后通过 http://example.com 访问。


4、启用反向代理

安装所需模块

sudo a2enmod proxy proxy_http
sudo systemctl restart apache2

配置反向代理
在虚拟主机配置文件中添加:

ProxyPass / http://127.0.0.1:8080/
ProxyPassReverse / http://127.0.0.1:8080/

保存并重启 Apache:

sudo systemctl restart apache2

5、启用 HTTPS 与 SSL 配置

安装 Let’s Encrypt 工具

sudo apt install certbot python3-certbot-apache

获取免费 SSL 证书

sudo certbot --apache -d example.com -d www.example.com

Certbot 会自动生成 HTTPS 配置,用户可通过 https://example.com 访问网站。


二、Nginx Web 服务器

1、Nginx 简介

Nginx 是一个高性能 Web 服务器和反向代理服务器,以其高并发和低资源占用著称。


2、安装 Nginx

在 Ubuntu 上安装

sudo apt update
sudo apt install nginx -y

验证安装
启动 Nginx 服务:

sudo systemctl start nginx
sudo systemctl enable nginx

在浏览器中访问 http://<服务器IP>,应显示默认的 Nginx 欢迎页面。


3、配置虚拟主机

创建虚拟主机配置

sudo nano /etc/nginx/sites-available/example.com

添加以下内容:

server {
    listen 80;
    server_name example.com www.example.com;

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

    location / {
        try_files $uri $uri/ =404;
    }

    error_log /var/log/nginx/example.com-error.log;
    access_log /var/log/nginx/example.com-access.log;
}

启用配置

sudo mkdir -p /var/www/example.com
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo systemctl reload nginx

测试虚拟主机
/var/www/example.com 下创建一个 index.html,然后通过 http://example.com 访问。


4、启用反向代理

配置反向代理
在虚拟主机配置文件中添加:

location / {
    proxy_pass http://127.0.0.1:8080/;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
}

保存并重启 Nginx:

sudo systemctl restart nginx

5、启用 HTTPS 与 SSL 配置

安装 Let’s Encrypt 工具

sudo apt install certbot python3-certbot-nginx

获取免费 SSL 证书

sudo certbot --nginx -d example.com -d www.example.com

Certbot 会自动生成 HTTPS 配置,用户可通过 https://example.com 访问网站。


三、小结

通过本章学习,你掌握了以下内容:

  1. 安装与配置 Apache 和 Nginx:包括虚拟主机和反向代理功能。
  2. 启用 HTTPS:使用 Let’s Encrypt 配置免费 SSL 证书。

通过灵活运用 Apache 和 Nginx,可以搭建高效、安全的 Web 服务,支持复杂的负载均衡与反向代理场景。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

人间酒中仙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值