第1步:安装Apache
Apache在Ubuntu的默认软件库中可用,因此我们将使用传统的软件包管理工具进行安装。我们将从更新本地包索引开始,以反映最新的上游更改。 之后,我们可以安装apache2
包:
sudo apt-get update
sudo apt-get install apache2
第2步:调整防火墙
在我们测试Apache之前,我们需要修改我们的防火墙以允许外部访问默认的Web端口。 假设您遵循先决条件中的说明,您应该配置一个UFW防火墙来限制对您的服务器的访问。在安装过程中,Apache向UFW注册自己,以提供一些应用程序配置文件。 我们可以使用这些配置文件来简化通过我们的防火墙启用或禁用访问Apache的过程。
我们可以通过键入以下内容列出ufw
程序配置文件:
sudo ufw app list
您应该获得应用程序配置文件的列表:
Available applications:
Apache
Apache Full
Apache Secure
OpenSSH
您可以看到,有三种可用于Apache的配置文件:
- Apache :此配置文件仅打开端口80(正常,未加密的Web流量)
- Apache Full :此配置文件打开端口80(正常,未加密的Web流量)和端口443(TLS / SSL加密流量)
- Apache Secure :此配置文件仅打开端口443(TLS / SSL加密流量)
为了我们的目的,我们将通过键入以下方式来允许Apache Full配置文件的传入流量:
sudo ufw allow 'Apache Full'
您可以键入以下内容来验证更改:
sudo ufw status
您应该在显示的输出中看到HTTP流量允许:
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Apache Full ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Apache Full (v6) ALLOW Anywhere (v6)
如果显示status:inactive,请先执行如下命令:
sudo ufw enable
如您所见,配置文件已被激活以允许访问Web服务器。
第3步:检查您的Web服务器
在安装过程结束时,Ubuntu 16.04启动Apache。 Web服务器应该已经启动并运行了。我们可以使用systemd
init系统检查以确保该服务正在运行,方法是输入:
sudo systemctl status apache2
apache2.service - LSB: Apache2 web server
Loaded: loaded (/etc/init.d/apache2; bad; vendor preset: enabled)
Drop-In: /lib/systemd/system/apache2.service.d
└─apache2-systemd.conf
Active: active (running) since Fri 2017-05-19 18:30:10 UTC; 1h 5min ago
Docs: man:systemd-sysv-generator(8)
Process: 4336 ExecStop=/etc/init.d/apache2 stop (code=exited, status=0/SUCCESS)
Process: 4359 ExecStart=/etc/init.d/apache2 start (code=exited, status=0/SUCCESS)
Tasks: 55
Memory: 2.3M
CPU: 4.094s
CGroup: /system.slice/apache2.service
├─4374 /usr/sbin/apache2 -k start
├─4377 /usr/sbin/apache2 -k start
└─4378 /usr/sbin/apache2 -k start
May 19 18:30:09 ubuntu-512mb-nyc3-01 systemd[1]: Stopped LSB: Apache2 web server.
May 19 18:30:09 ubuntu-512mb-nyc3-01 systemd[1]: Starting LSB: Apache2 web server...
May 19 18:30:09 ubuntu-512mb-nyc3-01 apache2[4359]: * Starting Apache httpd web server apache2
May 19 18:30:09 ubuntu-512mb-nyc3-01 apache2[4359]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
May 19 18:30:10 ubuntu-512mb-nyc3-01 apache2[4359]: *
May 19 18:30:10 ubuntu-512mb-nyc3-01 systemd[1]: Started LSB: Apache2 web server
当您拥有服务器的IP时,请将其输入到浏览器的地址栏中:
第4步:配置cgi
原来C++也可以用来写网站,只是需要服务器支持CGI才可以。在网上搜了好多的教程,结果都没有成功。后来,还是硬着头皮看了一下配置文件/etc/apache2/apache2.conf 顺着里面加载的顺序,才弄好的。测试过一切okay.
(1)在服务的要目录下,建立一个文件夹 cgi-bin.路径为 /var/www/cgi-bin. (名字约定是cgi-bin,不能是其他名字,cgi程序都放在这个cgi-bin的目录)
(2)到/etc/apahce2/文件夹下。把/etc/apache2/conf-available/serve-cgi-bin.conf的目录,改成上面新建的cgi-bin目录。
(3)然后,再把/etc/apache2/mods-available/cgi.load(cgid.load) 建软件链接到/etc/apache2/mods-enalbed/cgi.load(cgid.load). 建软链接用 “ln -s 源文件 目标文件”
(4)重启Apache
sudo /etc/init.d/apache2 restart
(5)用C++写下简单的程序。编绎成.cgi格式的。放到cgi-bin目录下面
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char const *argv[])
{
printf("Content-type:text/html\n\n");
printf("<html>\n");
printf("<head><title>welcome to c cgi.</title></head>\n<body>");
printf("hello,world!");
printf("</body></html>\n");
return 0;
}
(6)修改一下访问权限,访问