帶有虛擬主機的 PHP Apache 項目
Create the Project Directory
First, create a directory for your project. For example, let's create a directory called php:
sudo mkdir /var/www/html/php
Create a PHP Test File
create an index.php file in your project directory:
echo "<?php phpinfo(); " | sudo tee /var/www/html/php/index.php
Set Directory Permissions
Set the appropriate permissions so that Apache can access the directory:
sudo chown -R www-data:www-data /var/www/html/php sudo chmod -R 755 /var/www/html/php
The command sudo chown -R www-data:www-data /var/www/html/php performs the following actions:
-R: This option indicates that the operation should be performed recursively, i.e. not only on the specified directory, but also on all its subdirectories and files.
www-data:www-data: Specifies that the user and group that will become the new owners of the files are both www-data. This is a common user and group on Linux systems that serves as the default user for web servers such as Apache and Nginx.
/var/www/php: This is the path of the directory whose properties are being changed.
The command sudo chmod -R 755 /var/www/html/php performs the following actions:
755: This is a permission mode:
The first number (7) gives the owner of the file (or directory) read (4), write (2) and execute (1) permissions, for a total of 7.
The second number (5) gives the group read (4) and execute (1) permissions, but not write permissions, for a total of 5.
The third number (5) also gives other users read (4) and execute (1) permissions, but not write permissions, for a total of 5.
In summary, this command changes the permissions of all files and directories within /var/www/html/php, allowing the owner to have full control (read, write and execute), while the group and other users only have read and execute permissions. This is common in web server environments to ensure that the server can access the necessary files without compromising security.
Create the Virtual Host Configuration File
Create a new configuration file for your Virtual Host. The file should have the same name as the php project:
sudo your_editor /etc/apache2/sites-available/php.conf
Add the Virtual Host Configuration
Add the following configuration to the file:
<VirtualHost *:80> ServerAdmin webmaster@localhost ServerName php.info DocumentRoot /var/www/html/php <Directory /var/www/html/php/> AllowOverride All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/php_error.log CustomLog ${APACHE_LOG_DIR}/php_access.log combined </VirtualHost>
Let's analyze each part of the code:
-
- This is the beginning of the configuration block for a Virtual Host that listens on port 80, which is the default port for HTTP. The asterisk (*) indicates that the Virtual Host will accept connections from any IP address.
-
ServerAdmin webmaster@localhost
- Defines the e-mail address of the server administrator. This e-mail address can be used in error messages or when a user needs to contact the administrator.
-
ServerName php.info
- Specifies the name of the server to which this setting applies. In this case, the Virtual Host will respond to requests for php.info. It is important that this name resolves correctly to the server's IP (usually configured in the /etc/hosts file or in DNS).
-
DocumentRoot /var/www/html/php
- Defines the directory containing the files that will be served when someone accesses the ServerName (in this case, php.info). In this example, the files are located in /var/www/html/php.
-
- This block specifies specific settings for the directory indicated. The settings within this block affect Apache's behavior for that specific directory.
AllowOverride All
- It allows .htaccess files inside the /var/www/html/php/ directory to override Apache's settings. This means that developers can use .htaccess files to configure rules, such as redirects or access control, without having to edit the main Apache configuration file.
Require all granted
- Allows all users to have access to the directory. This means that anyone can access the files inside /var/www/html/php/ without restrictions.
- ErrorLog ${APACHE_LOG_DIR}/php_error.log
Specifies the path to the Apache error log file for this Virtual Host. The ${APACHE_LOG_DIR} is a variable that is usually set in the main Apache configuration file, pointing to the directory where the logs are stored. Here, errors related to this Virtual Host will be recorded in the php_error.log file.
CustomLog ${APACHE_LOG_DIR}/php_access.log combined
Defines the path to the Apache access log file for this Virtual Host. Like ErrorLog, this also uses the ${APACHE_LOG_DIR} variable. The combined format records information about requests, including the client's IP address, the time of the request, the HTTP method, the URL requested, the status code and the user agent.
Enable the Virtual Host
Enable the new Virtual Host with the command:
sudo a2ensite php.conf
Activate the Rewrite Module (if necessary)
If you need to use .htaccess or URL rewrites, activate the Apache rewrite module:
sudo a2enmod rewrite
Add Server Name to Hosts
To access your project using the server name you defined (php.info), add an entry in the /etc/hosts file:
sudo your_editor /etc/hosts
Add the following line to the end of the file:
<p>127.0.0.1 php.info</p>
Restart the Apache
Restart Apache for the changes to take effect:
<p>sudo systemctl restart apache2</p>
Accessing the Project
You can now access your project in the browser by typing http://php.info.
以上是帶有虛擬主機的 PHP Apache 項目的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

在PHP中,應使用password_hash和password_verify函數實現安全的密碼哈希處理,不應使用MD5或SHA1。1)password_hash生成包含鹽值的哈希,增強安全性。 2)password_verify驗證密碼,通過比較哈希值確保安全。 3)MD5和SHA1易受攻擊且缺乏鹽值,不適合現代密碼安全。

PHP類型提示提升代碼質量和可讀性。 1)標量類型提示:自PHP7.0起,允許在函數參數中指定基本數據類型,如int、float等。 2)返回類型提示:確保函數返回值類型的一致性。 3)聯合類型提示:自PHP8.0起,允許在函數參數或返回值中指定多個類型。 4)可空類型提示:允許包含null值,處理可能返回空值的函數。

PHP主要是過程式編程,但也支持面向對象編程(OOP);Python支持多種範式,包括OOP、函數式和過程式編程。 PHP適合web開發,Python適用於多種應用,如數據分析和機器學習。

PHP適合網頁開發和快速原型開發,Python適用於數據科學和機器學習。 1.PHP用於動態網頁開發,語法簡單,適合快速開發。 2.Python語法簡潔,適用於多領域,庫生態系統強大。

PHP起源於1994年,由RasmusLerdorf開發,最初用於跟踪網站訪問者,逐漸演變為服務器端腳本語言,廣泛應用於網頁開發。 Python由GuidovanRossum於1980年代末開發,1991年首次發布,強調代碼可讀性和簡潔性,適用於科學計算、數據分析等領域。

PHP的核心優勢包括易於學習、強大的web開發支持、豐富的庫和框架、高性能和可擴展性、跨平台兼容性以及成本效益高。 1)易於學習和使用,適合初學者;2)與web服務器集成好,支持多種數據庫;3)擁有如Laravel等強大框架;4)通過優化可實現高性能;5)支持多種操作系統;6)開源,降低開發成本。

PHP在現代化進程中仍然重要,因為它支持大量網站和應用,並通過框架適應開發需求。 1.PHP7提升了性能並引入了新功能。 2.現代框架如Laravel、Symfony和CodeIgniter簡化開發,提高代碼質量。 3.性能優化和最佳實踐進一步提升應用效率。

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip
