1 安装软件

1、安装Apache2(2.4版本)

Apache2.2和2.4版本配置方法不同

命令行输入:
sudo apt-get install apache2

2、安装Mysql

命令行输入:
sudo apt-get install mysql-server
sudo apt-get install mysql-client

2 使用Apache搭建web服务器

来源:

1、安装Apache

2、检查是否启动了Apache服务

命令行输入:
systemctl status apache2

出现下图:

ubuntu 重启容器服务 ubuntu重启服务器命令_apache

常用命令

开启、关闭和重启服务器:
/etc/init.d/apache2 start    //启动Apache服务
/etc/init.d/apache2 stop    //停止Apache服务
/etc/init.d/apache2 restart    //重启Apache服务

或:

sudo service apache2 start    //启动Apache服务
sudo service apache2 stop    //停止Apache服务
sudo service apache2 restart    //重启Apache服务

访问Apache服务器

打开浏览器,输入http://127.0.0.1/ (这是主机默认的IP地址)或者如果是云主机就输入云主机的IP地址。当我们看到下面的页面时变说明Apache2服务已经成功在我们的服务器上工作了。

ubuntu 重启容器服务 ubuntu重启服务器命令_linux_02

3、修改Apache网站根目录及默认网页

我们来试着修改web服务器的配置,让我们可以在自己搭建的web服务器上访问到自己规定的数据:

  • 修改根目录

修改/etc/apache2/sites-available/000-default.conf,执行命令:

sudo gedit /etc/apache2/sites-available/000-default.conf

找到并修改000-default.conf中的DocumentRoot /var/www/,DocumentRoot后添加想要修改到的根目录,如下:

ubuntu 重启容器服务 ubuntu重启服务器命令_Apache_03

ServerAdmin webmaster@localhost
DocumentRoot /var/www/html    // 修改其中的“/.../...来确定根目录”

然后修改/etc/apache2/apache2.conf来使服务器能读取目录和文件,执行命令:

sudo gedit /etc/apache2/apache2.conf

找到下图位置

代码可以直接添加而不需要找到下图位置

ubuntu 重启容器服务 ubuntu重启服务器命令_ubuntu 重启容器服务_04


添加上想要更改的根目录的路径,加入如下代码:

<Directory /.../...>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

最后重启Apache服务器:
一定要重启!

sudo service apache2 restart
  • 修改默认网页

修改/etc/apache2/mods-available/dir.conf中的内容
执行命令:

sudo gedit /etc/apache2/mods-available/dir.conf

配置文件原来是:

<IfModule mod_dir.c>
    DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm 
</IfModule>

在其中添加上想要的/wordpress:

<IfModule mod_dir.c>
    DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm /wordpress
</IfModule>

实际上在这里添加文件或目录的意思是,用于我们允许http请求访问到/var/www/html下的文件或目录中的内容的意思。

例如添加了test这个目录:

<IfModule mod_dir.c>
        DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm /test
</IfModule>

再在里面添加个名为index.html的文档,写入“Hello World!”,于是便可通过IP地址+/test/index.html访问到

这样,就能直接通过内网访问服务器了!但是想要访问还需要知道服务器的内网地址


安装网络工具

输入命令:

sudo apt install net-tools

安装完成后,输入命令查询:

ifconfig

得到IP地址

ubuntu 重启容器服务 ubuntu重启服务器命令_Apache_05


图中inet 10.210.38.4即为本机IP地址

Apache默认使用80端口