centos

apache2在centos上的最佳实践是什么

小樊
50
2025-06-14 09:30:06
栏目: 智能运维

在CentOS上安装和配置Apache2时,遵循以下最佳实践可以帮助确保服务器的稳定性、安全性和性能:

安装Apache2

  1. 更新系统

    sudo yum update -y
    
  2. 安装Apache2

    sudo yum install httpd -y
    
  3. 启动和启用Apache2服务

    sudo systemctl start httpd
    sudo systemctl enable httpd
    
  4. 配置防火墙

    sudo firewall-cmd --permanent --add-service=http
    sudo firewall-cmd --permanent --add-service=https
    sudo firewall-cmd --reload
    

配置Apache2

  1. 配置虚拟主机(可选):

    创建并编辑虚拟主机配置文件,例如在 /etc/httpd/conf.d/ 目录下创建 mywebsite.conf 文件,并添加相应的配置。

    sudo nano /etc/httpd/conf.d/mywebsite.conf
    

    在配置文件中添加以下内容:

    <VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/mywebsite
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
    

    然后,创建网站根目录并设置权限:

    sudo mkdir /var/www/mywebsites
    sudo chown -R apache:apache /var/www/mywebsites
    

    启用虚拟主机并重新加载Apache配置:

    sudo systemctl reload httpd
    
  2. 启用SSL加密

    使用Let’s Encrypt免费获取SSL证书:

    sudo yum install certbot python2-certbot-apache -y
    sudo certbot --apache -d yourdomain.com -d www.yourdomain.com
    
  3. 配置更多选项

    根据需求,可以配置更多Apache选项,如配置重定向、设置虚拟主机别名等。

调优Apache2

  1. 启用KeepAlive

    httpd.conf 中添加以下行来启用KeepAlive:

    KeepAlive On
    MaxKeepAliveRequests 100
    KeepAliveTimeout 5
    
  2. 调整MPM(多处理模块)设置

    根据服务器硬件资源和负载情况,调整MPM设置可以提高并发处理能力。例如,使用 prefork MPM:

    <IfModule mpm_prefork_module>
        StartServers 5
        MinSpareServers 5
        MaxSpareServers 10
        MaxRequestWorkers 150
        MaxConnectionsPerChild 0
    </IfModule>
    
  3. 配置静态文件缓存

    使用 mod_expires 模块来实现静态文件缓存。在 httpd.conf 中添加以下行:

    <IfModule mod_expires.c>
        ExpiresActive On
        ExpiresByType text/css "access plus 30 days"
        ExpiresByType image/jpeg "access plus 1 year"
        ExpiresByType image/png "access plus 1 year"
        ExpiresByType image/gif "access plus 1 year"
        ExpiresByType application/javascript "access plus 30 days"
    </IfModule>
    
  4. 使用压缩技术

    启用Gzip压缩可以减小传输的数据量,提高网站加载速度。在 httpd.conf 中添加以下行来启用Gzip压缩:

    <IfModule mod_deflate.c>
        AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript
    </IfModule>
    
  5. 调整内核参数

    通过调整内核参数,如TCP连接数、文件句柄数、内存管理等,可以提高系统性能。编辑 /etc/sysctl.conf 文件,添加或修改以下参数:

    net.ipv4.tcp_tw_reuse = 1
    net.ipv4.tcp_tw_recycle = 1
    net.ipv4.tcp_fin_timeout = 30
    net.ipv4.tcp_keepalive_time = 1200
    net.ipv4.ip_local_port_range "1024 65535"
    net.ipv4.tcp_max_syn_backlog 8192
    net.core.somaxconn = 1024
    net.core.netdev_max_backlog 2000
    net.ipv4.tcp_max_orphans = 32768
    net.ipv4.tcp_syncookies = 1
    

    使内核配置立马生效:

    sudo sysctl -p
    
  6. 禁用不必要的服务

    禁用不必要的服务和进程,减少系统资源占用。使用以下命令查看当前正在运行的服务,并根据需要禁用不需要的服务:

    systemctl list-unit-files --type=services
    sudo systemctl disable service_name
    

以上步骤提供了在CentOS系统上安装、配置和优化Apache2服务器的全面指南。请根据您的具体需求调整命令和配置。在进行任何重大更改之前,建议在测试环境中进行验证。

0
看了该问题的人还看了