在CentOS上部署基于Apache2的網站,可以按照以下步驟進行:
首先,確保你的CentOS系統是最新的。然后,安裝Apache2。
sudo yum update -y
sudo yum install httpd -y
安裝完成后,啟動Apache2服務并設置開機自啟。
sudo systemctl start httpd
sudo systemctl enable httpd
確保防火墻允許HTTP(端口80)和HTTPS(端口443)流量。
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
創建一個目錄來存放你的網站文件。
sudo mkdir -p /var/www/html/yourwebsite.com
sudo chown -R apache:apache /var/www/html/yourwebsite.com
編輯Apache的虛擬主機配置文件。
sudo vi /etc/httpd/conf/httpd.conf
在文件末尾添加以下內容:
<VirtualHost *:80>
ServerAdmin webmaster@yourwebsite.com
DocumentRoot /var/www/html/yourwebsite.com
ServerName yourwebsite.com
ServerAlias www.yourwebsite.com
ErrorLog /var/log/httpd/yourwebsite.com-error.log
CustomLog /var/log/httpd/yourwebsite.com-access.log combined
</VirtualHost>
保存并退出編輯器。
使配置生效。
sudo systemctl restart httpd
確保你的域名解析指向你的服務器IP地址。你可以在你的DNS提供商的管理界面中添加一個A記錄,指向你的服務器IP地址。
在瀏覽器中輸入你的域名(例如 http://yourwebsite.com),你應該能夠看到你的網站。
如果你想啟用HTTPS,可以使用Let’s Encrypt免費獲取SSL證書。
sudo yum install certbot python2-certbot-apache -y
sudo certbot --apache -d yourwebsite.com -d www.yourwebsite.com
按照提示完成SSL證書的安裝和配置。
你可以安裝和配置一些緩存模塊來提高網站性能,例如 mod_cache 和 mod_expires。
sudo yum install mod_cache mod_expires -y
然后在Apache配置文件中添加以下內容:
<IfModule mod_cache.c>
<IfModule mod_cache_disk.c>
CacheRoot /var/cache/httpd
CacheEnable disk /
CacheDirLevels 2
CacheDirLength 1
</IfModule>
</IfModule>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/html "access plus 1 week"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
</IfModule>
保存并重啟Apache服務。
通過以上步驟,你應該能夠在CentOS上成功部署一個基于Apache2的網站。