首页 后端开发 php教程 带有虚拟主机的 PHP Apache 项目

带有虚拟主机的 PHP Apache 项目

Oct 06, 2024 am 06:08 AM

PHP Apache project with Virtual Host

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:

  1. -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.

  2. 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.

  3. /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:

  1. 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.

  2. The second number (5) gives the group read (4) and execute (1) permissions, but not write permissions, for a total of 5.

  3. 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.
  1. 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.
  2. 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).
  3. 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.
  1. ErrorLog ${APACHE_LOG_DIR}/php_error.log
  2. 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.

  3. CustomLog ${APACHE_LOG_DIR}/php_access.log combined

  4. 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中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

<🎜>:泡泡胶模拟器无穷大 - 如何获取和使用皇家钥匙
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系统,解释
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆树的耳语 - 如何解锁抓钩
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
<🎜>掩盖:探险33-如何获得完美的色度催化剂
2 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Java教程
1677
14
CakePHP 教程
1430
52
Laravel 教程
1333
25
PHP教程
1278
29
C# 教程
1257
24
说明PHP中的安全密码散列(例如,password_hash,password_verify)。为什么不使用MD5或SHA1? 说明PHP中的安全密码散列(例如,password_hash,password_verify)。为什么不使用MD5或SHA1? Apr 17, 2025 am 12:06 AM

在PHP中,应使用password_hash和password_verify函数实现安全的密码哈希处理,不应使用MD5或SHA1。1)password_hash生成包含盐值的哈希,增强安全性。2)password_verify验证密码,通过比较哈希值确保安全。3)MD5和SHA1易受攻击且缺乏盐值,不适合现代密码安全。

PHP类型提示如何起作用,包括标量类型,返回类型,联合类型和无效类型? PHP类型提示如何起作用,包括标量类型,返回类型,联合类型和无效类型? Apr 17, 2025 am 12:25 AM

PHP类型提示提升代码质量和可读性。1)标量类型提示:自PHP7.0起,允许在函数参数中指定基本数据类型,如int、float等。2)返回类型提示:确保函数返回值类型的一致性。3)联合类型提示:自PHP8.0起,允许在函数参数或返回值中指定多个类型。4)可空类型提示:允许包含null值,处理可能返回空值的函数。

PHP和Python:解释了不同的范例 PHP和Python:解释了不同的范例 Apr 18, 2025 am 12:26 AM

PHP主要是过程式编程,但也支持面向对象编程(OOP);Python支持多种范式,包括OOP、函数式和过程式编程。PHP适合web开发,Python适用于多种应用,如数据分析和机器学习。

在PHP和Python之间进行选择:指南 在PHP和Python之间进行选择:指南 Apr 18, 2025 am 12:24 AM

PHP适合网页开发和快速原型开发,Python适用于数据科学和机器学习。1.PHP用于动态网页开发,语法简单,适合快速开发。2.Python语法简洁,适用于多领域,库生态系统强大。

PHP和Python:深入了解他们的历史 PHP和Python:深入了解他们的历史 Apr 18, 2025 am 12:25 AM

PHP起源于1994年,由RasmusLerdorf开发,最初用于跟踪网站访问者,逐渐演变为服务器端脚本语言,广泛应用于网页开发。Python由GuidovanRossum于1980年代末开发,1991年首次发布,强调代码可读性和简洁性,适用于科学计算、数据分析等领域。

为什么要使用PHP?解释的优点和好处 为什么要使用PHP?解释的优点和好处 Apr 16, 2025 am 12:16 AM

PHP的核心优势包括易于学习、强大的web开发支持、丰富的库和框架、高性能和可扩展性、跨平台兼容性以及成本效益高。1)易于学习和使用,适合初学者;2)与web服务器集成好,支持多种数据库;3)拥有如Laravel等强大框架;4)通过优化可实现高性能;5)支持多种操作系统;6)开源,降低开发成本。

PHP和框架:现代化语言 PHP和框架:现代化语言 Apr 18, 2025 am 12:14 AM

PHP在现代化进程中仍然重要,因为它支持大量网站和应用,并通过框架适应开发需求。1.PHP7提升了性能并引入了新功能。2.现代框架如Laravel、Symfony和CodeIgniter简化开发,提高代码质量。3.性能优化和最佳实践进一步提升应用效率。

PHP的影响:网络开发及以后 PHP的影响:网络开发及以后 Apr 18, 2025 am 12:10 AM

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

See all articles