温馨提示×

CentOS镜像如何配置Web服务器

小樊
50
2025-08-12 22:22:18
栏目: 云计算

以下是基于CentOS镜像配置Web服务器的步骤(以Apache和Nginx为例):

一、基础环境准备

  1. 安装CentOS系统

    • 使用CentOS镜像文件通过虚拟机或物理机安装系统,安装过程中可配置网络、分区等。
    • 更新系统:sudo yum update -y
  2. 安装Web服务器软件

    • Apache
      sudo yum install httpd -y  
      sudo systemctl start httpd  
      sudo systemctl enable httpd  
      
    • Nginx
      sudo yum install epel-release -y  # 安装EPEL仓库(若需最新版Nginx)  
      sudo yum install nginx -y  
      sudo systemctl start nginx  
      sudo systemctl enable nginx  
      

二、配置Web服务器

1. Apache配置

  • 创建虚拟主机
    编辑配置文件 /etc/httpd/conf.d/虚拟主机名.conf,添加以下内容:
    <VirtualHost *:80>  
        ServerName example.com  
        DocumentRoot "/var/www/html/example.com"  
        ErrorLog "/var/log/httpd/example.com-error.log"  
        CustomLog "/var/log/httpd/example.com-access.log" combined  
    </VirtualHost>  
    
  • 设置目录权限
    sudo mkdir -p /var/www/html/example.com  
    sudo chown -R apache:apache /var/www/html/example.com  
    sudo chmod -R 755 /var/www/html  
    

2. Nginx配置

  • 创建服务器块
    编辑配置文件 /etc/nginx/conf.d/虚拟主机名.conf,添加以下内容:
    server {  
        listen 80;  
        server_name example.com;  
        root /var/www/html/example.com;  
        index index.html index.htm;  
        location / {  
            try_files $uri $uri/ =404;  
        }  
    }  
    
  • 设置目录权限
    sudo mkdir -p /var/www/html/example.com  
    sudo chown -R nginx:nginx /var/www/html/example.com  
    sudo chmod -R 755 /var/www/html  
    

三、开放防火墙与测试

  1. 允许HTTP/HTTPS流量
    sudo firewall-cmd --permanent --add-service=http  
    sudo firewall-cmd --permanent --add-service=https  
    sudo firewall-cmd --reload  
    
  2. 测试访问
    • 通过浏览器输入服务器IP或域名,查看默认页面或虚拟主机页面是否正常显示。

四、可选配置

  • 安装SSL证书:使用Let’s Encrypt免费证书,配置HTTPS(以Apache为例):
    sudo certbot --apache  
    
  • 配置负载均衡/高可用:可结合Nginx反向代理或Docker实现多服务器负载均衡。

以上步骤基于CentOS默认镜像,根据实际需求调整软件版本或配置参数即可。

0