共計(jì) 3304 個(gè)字符,預(yù)計(jì)需要花費(fèi) 9 分鐘才能閱讀完成。
這篇文章主要介紹了 Ubuntu 中如何安裝與配置 Apache 的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇 Ubuntu 中如何安裝與配置 Apache 文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。
1. 安裝
Ubuntu20.04 中 Apache 軟件包為 apache2。
運(yùn)行下面的命令來(lái)升級(jí)軟件包索引,并且安裝 Apache
sudo apt updatesudo apt install apache2
當(dāng)安裝過(guò)程完成,Apache 服務(wù)將會(huì)被自動(dòng)啟動(dòng)。
通過(guò)輸入下面的命令,驗(yàn)證 Apache 能否正在運(yùn)行:
sudo systemctl status apache2
2. 配置 2.1 HTTP 和 HTTPS 端口
Apache 監(jiān)聽(tīng)了端口 80(HTTP) 和 443(HTTPS)。你需要在防火墻打開(kāi)那些端口,以便網(wǎng)站服務(wù)器從互聯(lián)網(wǎng)上是可以訪問(wèn)的。
假設(shè)你正在使用 UFW, 你可以通過(guò)啟用 Apache Full 配置,它包含了這兩個(gè)端口的規(guī)則:
sudo ufw allow Apache Full
2.2 apache2 默認(rèn)的幾個(gè)配置文件:
/etc/apache2/apache2.conf
是主要配置文件 (這個(gè)文件的末尾可以看到,include 了其它所有的配置文件)。
/etc/apache2/ports.conf
始終包含在主配置文件中。它用于確定傳入連接的偵聽(tīng)端口,默認(rèn)為 80,我們一般都會(huì)重新配置新的端口。
/etc/apache2/sites-enabled,/etc/apache2/conf-enabled,/etc/apache2/mods-enabled
其它配置文件目錄。
/var/www/html
apache2 的默認(rèn) web 目錄:(在 /etc/apache2/sites-enabled/000-default.conf 里可以看到這個(gè) DocumentRoot /var/www/html 配置)
/etc/apache2/envvars
apache2 的默認(rèn)客戶是 www-data,定義在該文件中。
/etc/apache2/mods-enabled/dir.conf
設(shè)置默認(rèn)主頁(yè)的配置文件
2.3 修改默認(rèn)端口,比方修改為 5000
sudo vim /etc/apache2/ports.conf
找到如下內(nèi)容:
Listen 80 IfModule ssl_module Listen 443 /IfModule IfModule mod_gnutls.c Listen 443 /IfModule
將 80 修改為 5000 就可, 443 為 https 端口,假如有需要也可以修改。
2.4 修改默認(rèn)的網(wǎng)站根目錄
apache2 為了安全起見(jiàn),只允許 /var/www、/usr/share 下面的文件夾被訪問(wèn),假如要指定其它目錄為網(wǎng)站根目錄,需要修改配置文件 /etc/apache2/apache2.conf
sudo vim /etc/apache2/apache2.conf
找到下面的配置片段
Directory / Options FollowSymLinks AllowOverride None Require all denied /Directory Directory /usr/share AllowOverride None Require all granted /Directory Directory /var/www/ Options Indexes FollowSymLinks AllowOverride None Require all granted /Directory
在配置下面增加指定的目錄,比方 /mnt/www
Directory /mnt/www/ Options Indexes FollowSymLinks AllowOverride None Require all granted /Directory
重啟服務(wù)使修改生效
sudo systemctl reload apache2
2.5 配置一個(gè)虛擬主機(jī)
Apache 默認(rèn)啟動(dòng)了一個(gè)虛擬主機(jī)。所有域名都指向服務(wù)器 IP 地址,匹配了默認(rèn)的虛擬主機(jī)。假如你只托管一個(gè)簡(jiǎn)單的網(wǎng)站,你需要將網(wǎng)站內(nèi)容上傳到 /var/www/html, 并且編輯虛擬主機(jī)配置文件 /etc/apache2/sites-enabled/000-default.conf。
假如想搭建更多網(wǎng)站,需要為每一個(gè)網(wǎng)站創(chuàng)立一個(gè)虛擬主機(jī)配置。
以 example.com 為例,第一步就是創(chuàng)立根目錄文件夾:
# 指定網(wǎng)站根目錄為 /mnt/www/example.com , -p 參數(shù)的作用為遞歸創(chuàng)立目錄,即便上級(jí)目錄不存在,會(huì)按目錄層級(jí)自動(dòng)創(chuàng)立目錄 sudo mkdir -p /mnt/www/example.com
在網(wǎng)站根目錄下創(chuàng)立一個(gè) index.html 文件來(lái)測(cè)試站點(diǎn):
!DOCTYPE html html head meta charset= utf-8 title 測(cè)試站點(diǎn) /title /head body h1 恭喜!假如看到這個(gè)頁(yè)面,說(shuō)明訪問(wèn)成功啦!/h1 /body /html
apache2 的默認(rèn)客戶為 www-data , 修改網(wǎng)站根文件夾的客戶歸屬,避免權(quán)限問(wèn)題:
sudo chown -R www-data: /mnt/www/example.com
下一步就是為 example.com 創(chuàng)立一個(gè)虛擬主機(jī)配置 (最佳實(shí)踐就是將每一個(gè)虛擬主機(jī)配置存儲(chǔ)成一個(gè)獨(dú)立的文件)。
Apache 虛擬主機(jī)配置文件存儲(chǔ)在 /etc/apache2/sites-available 目錄, 標(biāo)準(zhǔn)命名是使用域名來(lái)命名配置文件。
sudo touch /etc/apache2/sites-available/example.com.conf
編輯配置,增加以下內(nèi)容:
VirtualHost *:80 ServerName example.com ServerAlias www.example.com ServerAdmin webmaster@example.com DocumentRoot /mnt/www/example.com Directory /mnt/www/example.com Options -Indexes +FollowSymLinks AllowOverride All /Directory ErrorLog ${APACHE_LOG_DIR}/example.com-error.log CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined /VirtualHost
Apache 默認(rèn)不會(huì)讀取 /etc/apache2/sites-available 文件夾下的配置文件,需要將它們鏈接到 /etc/apache2/sites-enabled 文件夾,使用 a2ensite 創(chuàng)立一個(gè)鏈接,激活虛擬主機(jī)配置:
sudo a2ensite example.com
測(cè)試配置文件,能否有語(yǔ)法錯(cuò)誤:
sudo apachectl configtest
假如沒(méi)有任何錯(cuò)誤,你將會(huì)看到下面的輸出:
Syntax OK
重啟 Apache 服務(wù),使修改生效:
sudo systemctl reload apache2
最終,在瀏覽器中打開(kāi) http://example.com,看看能否已經(jīng)可以成功訪問(wèn)。
3. 常用命令
# 啟動(dòng) apache2 服務(wù) sudo systemctl start apache2# 關(guān)閉 apache2 服務(wù) sudo systemctl stop apache2# 重啟 apache2 服務(wù)# 查看 apache2 狀態(tài)
sudo systemctl reload apache2
sudo systemctl status apache2
關(guān)于“Ubuntu 中如何安裝與配置 Apache”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“Ubuntu 中如何安裝與配置 Apache”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注丸趣 TV 行業(yè)資訊頻道。