CentOS 上部署 Apache 的完整步骤
一 准备与安装
sudo yum update -y 与 sudo yum install httpd -ysudo systemctl start httpd 与 sudo systemctl enable httpdsudo systemctl status httpd(应看到 active (running))http://服务器IP,出现 Apache 测试页 表示安装成功。二 防火墙与 SELinux 设置
sudo firewall-cmd --permanent --add-service=http、sudo firewall-cmd --permanent --add-service=https、sudo firewall-cmd --reloadsudo firewall-cmd --list-all(应看到 http/https 在 services 中)sudo chcon -Rt httpd_sys_content_t /var/www/你的站点目录sudo setsebool -P httpd_can_network_connect 1三 虚拟主机与目录结构
sudo mkdir -p /var/www/example.comsudo chown -R apache:apache /var/www/example.com 与 sudo chmod -R 755 /var/www/example.comecho "<html><body><h1>It works!</h1></body></html>" | sudo tee /var/www/example.com/index.html/etc/httpd/conf.d/,后缀 .conf):
<VirtualHost *:80>
ServerAdmin admin@example.com
DocumentRoot /var/www/example.com
ServerName example.com
ServerAlias www.example.com
ErrorLog /var/log/httpd/example.com-error.log
CustomLog /var/log/httpd/example.com-access.log combined
</VirtualHost>
sudo apachectl configtestsudo systemctl reload httpd(或 restart)。四 启用 HTTPS 与自动续期
sudo yum install epel-release -y 与 sudo yum install certbot python2-certbot-apache -ysudo certbot --apache -d example.com -d www.example.comsudo crontab -e0 0 * * * /usr/bin/certbot renew --quietsudo systemctl enable --now certbot-renew.timersudo certbot renew --dry-run。五 性能优化与 PHP 支持
/etc/httpd/conf/httpd.conf 或相应包含文件):
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
sudo yum install mod_cache mod_cache_disk -y<IfModule mod_cache.c>
CacheQuickHandler off
CacheLock on
CacheLockPath /tmp/mod_cache-lock
CacheLockMaxAge 5
CacheIgnoreHeaders Set-Cookie
<IfModule mod_cache_disk.c>
CacheRoot /var/cache/mod_cache_disk
CacheEnable disk /
CacheDirLevels 2
CacheDirLength 1
</IfModule>
</IfModule>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript
sudo yum install php php-fpm -yecho "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php,访问 http://服务器IP/info.php。