Heim  >  Artikel  >  Datenbank  >  So erstellen Sie einen robusten Ubuntu-Webserver mit Apache, MySQL, PHP und virtuellen Hosts

So erstellen Sie einen robusten Ubuntu-Webserver mit Apache, MySQL, PHP und virtuellen Hosts

Linda Hamilton
Linda HamiltonOriginal
2024-09-28 18:07:30257Durchsuche

How to Create a Robust Ubuntu Web Server Using Apache, MySQL, PHP, and Virtual Hosts

使用完整的 Ubuntu 設定指南輕鬆部署和管理您的 Web 開發環境

Web 伺服器的重要性

網頁伺服器是任何網站的支柱,作為向全球用戶提供內容的平台。網路伺服器的效率和可靠性對於您在線業務的成功至關重要。

Ubuntu、Apache、MySQL、PHP 與虛擬主機概述

本文將指導您在Ubuntu上建立一個完整的Web伺服器環境,使用Apache作為Web伺服器,MySQL作為資料庫伺服器,PHP作為腳本語言。我們還將介紹虛擬主機的建立和配置,它允許您在單一伺服器上運行多個網站。

文章目的

本指南的目的是提供在 Ubuntu 上設定強大的 Web 伺服器的詳細逐步流程,專為初學者和進階使用者量身定制。

初步設定

選出正確的硬體

在開始軟體安裝之前,確保您的硬體足以完成您將要執行的任務非常重要。根據預期負載考慮 CPU 功率、RAM 和儲存容量等因素。

安裝 Ubuntu 伺服器

  • 從 Ubuntu 官方網站下載最新版本的 Ubuntu Server。
  • 建立可啟動 USB 隨身碟並在​​您的電腦上安裝 Ubuntu Server。
  • 依照螢幕上的指示完成安裝。

更新與升級 Ubuntu

安裝 Ubuntu 後,必須更新和升級系統以確保所有軟體包都是最新的。

sudo apt update
sudo apt upgrade

安裝 Apache

了解 Apache Web 伺服器

Apache 是使用最廣泛的 Web 伺服器之一,以其穩健性、靈活性和廣泛的模組支援而聞名。
安裝 Apache 的步驟

使用以下指令安裝 Apache :

sudo apt install apache2

啟動並啟用 Apache

啟動 Apache 服務並使其開啟:

sudo systemctl start apache2
sudo systemctl enable apache2

驗證 Apache 安裝

要驗證 Apache 是否正在執行,請使用下列指令:

sudo systemctl status apache2

安裝 MySQL

了解 MySQL 資料庫伺服器

MySQL 是一個強大的關聯式資料庫管理系統,用於儲存和管理網站和應用程式的資料。

安裝 MySQL 的步驟

使用指令安裝MySQL:

sudo apt install mysql-server

確保 MySQL 安裝的安全性

要保護您的 MySQL 安裝,請執行安全腳本:

sudo mysql_secure_installation

依照指示設定 root 密碼、刪除匿名使用者並保護資料庫。

測試 MySQL 功能

登入 MySQL shell 以確保其正常運作:

sudo mysql -u root -p

安裝 PHP

了解 PHP 腳本語言

PHP 是一種流行的伺服器端腳本語言,用於 Web 開發。它特別適合創建動態內容並與資料庫互動。
安裝 PHP 的步驟

使用以下指令安裝 PHP:

新增 Ondrej PHP PPA,它總是提供最新的穩定 PHP 版本:

sudo add-apt-repository ppa:ondrej/php
sudo apt update

安裝最新的 PHP 版本:

sudo apt install php libapache2-mod-php

安裝常用 PHP 擴充:

sudo apt install php-mbstring php-mysql php-curl php-cli php-dev php-imagick php-soap php-zip php-xml php-imap php-xmlrpc php-gd php-opcache php-intl

重新啟動 Apache

sudo systemctl restart apache2

*為 Laravel 安裝 Composer *

更新套件管理器

首先,請確保您的系統已更新:

sudo apt update

安裝所需的依賴項

確保安裝了curl和php-cli:

sudo apt install curl php-cli unzip

下載並安裝 Composer

執行以下命令在 Ubuntu 上安裝 Composer :

curl -sS https://getcomposer.org/installer -o composer-setup.php
php composer-setup.php --install-dir=/usr/local/bin --filename=composer

驗證安裝

檢查Composer是否安裝成功:

composer --version

設定虛擬主機

虛擬主機說明

虛擬主機可讓您在單一伺服器上託管多個網域。每個網域都可以有自己單獨的配置,包括文檔根目錄、日誌檔案等。

為網站建立目錄結構

為您的新網站建立目錄:

sudo mkdir /var/www/

設定適當的權限

Ensure the correct ownership and permissions :

sudo chown -R $USER:$USER /var/www/
sudo chmod -R 777 /var/www/

Creating a Virtual Host File

Create a configuration file for your site :

sudo nano /etc/apache2/sites-available/000-default.conf

Add the following configuration:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerAlias *
    UseCanonicalName Off
    VirtualDocumentRoot /var/www/%0

    <Directory "/var/www">
        AllowOverride All
        Require all granted
        Options Indexes FollowSymLinks
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Enabling the New Virtual Host

Enable the new site and test the configuration:

sudo a2ensite 000-default.conf
sudo apache2ctl configtest

Restarting Apache

Restart Apache to apply the changes:

sudo systemctl restart apache2

Editing the Hosts File

Map your domain to the local server by editing the hosts file:

sudo nano /etc/hosts

Add the following line:

127.0.0.1       demo

Das obige ist der detaillierte Inhalt vonSo erstellen Sie einen robusten Ubuntu-Webserver mit Apache, MySQL, PHP und virtuellen Hosts. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:Zeitaufwändiges SQL-TrackingNächster Artikel:Zeitaufwändiges SQL-Tracking