Projek PHP Apache dengan Hos Maya

Mary-Kate Olsen
Lepaskan: 2024-10-06 06:08:03
asal
280 orang telah melayarinya

PHP Apache project with Virtual Host

创建项目目录

首先,为您的项目创建一个目录。例如,让我们创建一个名为 php 的目录:


sudo mkdir /var/www/html/php


Salin selepas log masuk

创建 PHP 测试文件

在项目目录中创建一个index.php文件:


echo "<?php phpinfo(); " | sudo tee /var/www/html/php/index.php


Salin selepas log masuk

设置目录权限

设置适当的权限,以便 Apache 可以访问该目录:


sudo chown -R www-data:www-data /var/www/html/php 
sudo chmod -R 755 /var/www/html/php


Salin selepas log masuk

命令 sudo chown -R www-data:www-data /var/www/html/php 执行以下操作:

  1. -R:该选项表示应该递归执行操作,即不仅对指定目录执行操作,还对其所有子目录和文件执行操作。

  2. www-data:www-data:指定将成为文件新所有者的用户和组都是 www-data。这是 Linux 系统上的常见用户和组,用作 Apache 和 Nginx 等 Web 服务器的默认用户。

  3. /var/www/php:这是要更改属性的目录的路径。

命令 sudo chmod -R 755 /var/www/html/php 执行以下操作:

755:这是一种权限模式:

  1. 第一个数字 (7) 为文件(或目录)的所有者提供读 (4)、写 (2) 和执行 (1) 权限,总共 7 个。

  2. 第二个数字 (5) 授予该组读取 (4) 和执行 (1) 权限,但不提供写入权限,总共 5 个。

  3. 第三个数字 (5) 还授予其他用户读取 (4) 和执行 (1) 权限,但不授予写入权限,总共 5 个。

综上所述,这个命令改变了 /var/www/html/php 内所有文件和目录的权限,让所有者拥有完全的控制权(读、写、执行),而组和其他用户只有读的权限和执行权限。这在 Web 服务器环境中很常见,以确保服务器可以访问必要的文件而不影响安全性。

创建虚拟主机配置文件

为您的虚拟主机创建一个新的配置文件。该文件应与 php 项目同名:


sudo your_editor /etc/apache2/sites-available/php.conf


Salin selepas log masuk

添加虚拟主机配置

将以下配置添加到文件中:


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


Salin selepas log masuk

我们来分析一下代码的各个部分:

    • 这是侦听端口 80 的虚拟主机配置块的开始,该端口是 HTTP 的默认端口。星号 (*) 表示虚拟主机将接受来自任何 IP 地址的连接。
  1. ServerAdmin webmaster@localhost

    • 定义服务器管理员的电子邮件地址。该电子邮件地址可用于错误消息或用户需要联系管理员时。
  2. 服务器名称 php.info

    • 指定应用此设置的服务器的名称。在这种情况下,虚拟主机将响应 php.info 的请求。重要的是该名称正确解析为服务器的 IP(通常在 /etc/hosts 文件或 DNS 中配置)。
  3. 文档根目录 /var/www/html/php

    • 定义包含当有人访问 ServerName 时将提供服务的文件的目录(在本例中为 php.info)。在此示例中,文件位于 /var/www/html/php.
    • 此块指定指定目录的特定设置。此块中的设置会影响 Apache 针对该特定目录的行为。

允许覆盖全部

  • 它允许 /var/www/html/php/ 目录中的 .htaccess 文件覆盖 Apache 的设置。这意味着开发人员可以使用 .htaccess 文件来配置规则,例如重定向或访问控制,而无需编辑主 Apache 配置文件。

要求全部授予

  • 允许所有用户访问该目录。这意味着任何人都可以不受限制地访问 /var/www/html/php/ 中的文件。
  1. 错误日志 ${APACHE_LOG_DIR}/php_error.log
  2. 指定此虚拟主机的 Apache 错误日志文件的路径。 ${APACHE_LOG_DIR} 是一个变量,通常在 Apache 主配置文件中设置,指向存储日志的目录。这里,与该虚拟主机相关的错误将记录在 php_error.log 文件中。

  3. 自定义日志 ${APACHE_LOG_DIR}/php_access.log 组合

  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


Salin selepas log masuk

Activate the Rewrite Module (if necessary)

If you need to use .htaccess or URL rewrites, activate the Apache rewrite module:


sudo a2enmod rewrite


Salin selepas log masuk

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


Salin selepas log masuk

Add the following line to the end of the file:


<p>127.0.0.1   php.info</p>

Salin selepas log masuk




Restart the Apache

Restart Apache for the changes to take effect:


<p>sudo systemctl restart apache2</p>

Salin selepas log masuk




Accessing the Project

You can now access your project in the browser by typing http://php.info.

Atas ialah kandungan terperinci Projek PHP Apache dengan Hos Maya. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

sumber:dev.to
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!