First, create a directory for your project. For example, let's create a directory called php:
sudo mkdir /var/www/html/php
create an index.php file in your project directory:
echo "<?php phpinfo(); " | sudo tee /var/www/html/php/index.php
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 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 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:
ServerAdmin webmaster@localhost
ServerName php.info
DocumentRoot /var/www/html/php
AllowOverride All
Require all granted
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 new Virtual Host with the command:
sudo a2ensite php.conf
If you need to use .htaccess or URL rewrites, activate the Apache rewrite module:
sudo a2enmod rewrite
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 Apache for the changes to take effect:
<p>sudo systemctl restart apache2</p>
You can now access your project in the browser by typing http://php.info.
以上是帶有虛擬主機的 PHP Apache 項目的詳細內容。更多資訊請關注PHP中文網其他相關文章!