IT Knowledge Base

~ Freedom is the right of all sentient beings ~

發佈日期:

分類:

, , ,

如何在Ubuntu 22.04下‧安裝Apache、PHP、MariaDB (LAMP)及phpMyAdmin

01. 多年前,第一次玩LAMP時,是自己設定Apache,再安裝PHP及MySQL。多年後,為求方便的關係,改用了LAMPP (XAMPP的Linux程式),在設定、更新上真的方便很多。

02. 來到今天,因為想要在Ubuntu加裝另一程式關係,在LAMPP環境試了很多次也不成功下。就試一下改回第一天的LAMP設定。

03. 今天就做一個簡單的記錄。首先,先安裝Apache2、PHP 8.1及MariaDB程式。

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install apache2

sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get install php8.1
sudo apt install php8.1-common php8.1-mysql php8.1-xml php8.1-xmlrpc php8.1-curl php8.1-gd php8.1-imagick php8.1-cli php8.1-dev php8.1-imap php8.1-mbstring php8.1-opcache php8.1-soap php8.1-zip php8.1-intl
sudo systemctl restart apache2

sudo apt install mariadb-server
sudo mysql_secure_installation

04. 設定Apache2 port 80存取位置。因為port 80會用來作更新SSL證書用,所以我不會關閉此埠位,而是直接設定導向至https網站。

sudo nano /etc/apache2/sites-enabled/000-default.conf
<VirtualHost *:80>
ServerName example.com
ServerAdmin web@example.com
DocumentRoot /var/www
RewriteEngine on
RewriteCond %{SERVER_NAME} = example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

04. 設定Apache2 port 443存取位置。設定VirtualHost,在Directory設定ErrorDocument。因為會用到phpMyAdmin關係,所以用Alias,令用戶可以存取DocumentRoot另外位置,當然亦用到Require ip選項,令指定人士才能存取phpMyAdmin位置。

sudo nano /etc/apache2/sites-enabled/000-default-le-ssl.conf
<VirtualHost *:443>
ServerName example.com
ServerAdmin web@example.com
ServerAlias example.com
DocumentRoot /var/www
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
</VirtualHost>

<Directory "/var/www">
Options FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
ErrorDocument 403 /error_handle/403.php
ErrorDocument 404 /error_handle/404.php
ErrorDocument 500 /error_handle/500.htm
</Directory>

Alias /phpmyadmin "/etc/phpmyadmin"
<Directory "/opt/phpmyadmin">
AllowOverride AuthConfig Limit
Require ip 123.123.123.123
ErrorDocument 403 /apache_error_handle/403.php
ErrorDocument 404 /apache_error_handle/404.php
ErrorDocument 500 /apache_error_handle/500.htm
</Directory>

05. 順便記錄一下其他檔案位置。

php - /usr/bin
apache2.conf - /etc/apache2
php.ini - /etc/php/8.1/apache2
mods available - /etc/apache2/mods-available
mods enabled - /etc/apache2/mods-enabled
sites available - /etc/apache2/sites-available
sites enabled - /etc/apache2/sites-enabled

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *