ubuntu下使用php和apache

 os:Ubuntu 20.04.3 LTS

安装php:

sudo apt install php7.4

此时会连同apache也一同安装了,直接访问:http://your_ip/,即可看到默认的页面,可以看到一些有意思的东西:

The configuration layout for an Apache2 web server installation on Ubuntu systems is as follows:

/etc/apache2/
|-- apache2.conf
|       `--  ports.conf
|-- mods-enabled
|       |-- *.load
|       `-- *.conf
|-- conf-enabled
|       `-- *.conf
|-- sites-enabled
|       `-- *.conf
          
  • apache2.conf is the main configuration file. It puts the pieces together by including all remaining configuration files when starting up the web server.
  • ports.conf is always included from the main configuration file. It is used to determine the listening ports for incoming connections, and this file can be customized anytime.
  • Configuration files in the mods-enabled/conf-enabled/ and sites-enabled/ directories contain particular configuration snippets which manage modules, global configuration fragments, or virtual host configurations, respectively.
  • They are activated by symlinking available configuration files from their respective *-available/ counterparts. These should be managed by using our helpers a2enmod, a2dismod, a2ensite, a2dissite, and a2enconf, a2disconf . See their respective man pages for detailed information.
  • The binary is called apache2. Due to the use of environment variables, in the default configuration, apache2 needs to be started/stopped with /etc/init.d/apache2 or apache2ctlCalling /usr/bin/apache2 directly will not work with the default configuration.
Document Roots

By default, Ubuntu does not allow access through the web browser to any file apart of those located in /var/wwwpublic_html directories (when enabled) and /usr/share (for web applications). If your site is using a web document root located elsewhere (such as in /srv) you may need to whitelist your document root directory in /etc/apache2/apache2.conf.

The default Ubuntu document root is /var/www/html. You can make your own virtual hosts under /var/www. This is different to previous releases which provides better security out of the box.

Reporting Problems

Please use the ubuntu-bug tool to report bugs in the Apache2 package with Ubuntu. However, check existing bug reports before reporting a new bug.

Please report bugs specific to modules (such as PHP and others) to respective packages, not to the web server itself.


apache2的配置文件目录为/etc/apache2,需要在sites-available内复制000-default.conf,命名为dev.conf,目录地址为/var/www/html/dev/,我们在该目录下创建一个test.php,文件内容为<?php phpinfo(); ?>,

dev.conf的内容和000-default.conf基本一致,只是DocumentRoot地址为/var/www/html/dev

然后在sites-enabled内做以一个dev.conf的软连接,为什么在site-enabled内做软连接呢?因为apache.conf内使用的是sites-enabled文件夹下的内容:


# Include generic snippets of statements

IncludeOptional conf-enabled/*.conf


# Include the virtual host configurations:

IncludeOptional sites-enabled/*.conf



在sites-enabled内做过软链接之后,重启apache2:
sudo /etc/init.d/apache2 restart

然后在访问http://your_ip/dev/test.php,即可看到phpinfo输出的信息