Ubuntu下http服务器搭建

时间:2024-02-15 14:57:08

本文章转载于https://www.cnblogs.com/guxiong/p/7201344.html

Linux中搭建HTTP服务器

http服务器采用LAMP

其中L:Linux A:Apache M:MySQL P:PHP

1、配置IP

[[email protected]~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0

BOOTPROTO=static

ONBOOT=yes

HWADDR=00:0c:29:5d:a8:80

IPADDR=192.168.126.133

NETMASK=255.255.255.0

2、配置主机名

[[email protected]~]# cat /etc/sysconfig/network

NETWORKING=yes

NETWORKING_IPV6=yes

HOSTNAME=web.gx.com

3、修改hosts文件

[[email protected]~]# cat /etc/hosts

127.0.0.1               localhost.localdomain localhost

::1             localhost6.localdomain6 localhost6

192.168.10.253  web.gx.com        web

[[email protected]~]# service network restart

[[email protected]~]# chkconfig network on

4、软件包的安装

[[email protected]~]# rpm -q httpd

package httpd is not installed

[[email protected]~]# yum -y install httpd

5、启动服务

[root[email protected]~]# service httpd restart

[[email protected]~]# chkconfig httpd on

试验二:基本HTTP服务器的配置

     Web服务器域名:www.gx.com

     默认首页包括:index.html、index.*

     开启保持连接 

     网站用自己的静态网页musicapp测试

服务器操作:

1、备份主配置文件

[[email protected] ~]# cd /etc/httpd/conf

[[email protected] conf]# cp httpd.conf httpd.conf.bak

2、修改主配置文件

[[email protected] ~]# vim /etc/httpd/conf/httpd.conf

...

 74 KeepAlive On

...

265 ServerName www.gx.com:80

...

391 DirectoryIndex index.html index.php

...

3、启动服务

[[email protected] ~]# service httpd restart

[[email protected] ~]# chkconfig httpd on

将musicapp文件夹中的文件全部拷贝到/var/www/html/下

4.修改客户端hosts文件

192.168.126.133         www.gx.com

[[email protected] ~]# vim /etc/hosts

5.经测试发现通过本机IP192.168.126.133不能访问,网上查找后删除/etc/httpd/conf.d/welcome.conf

6.打开浏览器访问   http://www.gx.com

3、新建authdir站点,只允许某个IP192.168.126.135访问www.gx.com,允许所有人访问www.gx.com/authdir

[[email protected] ~]# mkdir /var/www/html/authdir

[[email protected] ~]# echo "http://www.gx.com/authdir/index.html" >  /var/www/html/authdir/index.html

[[email protected] ~]# vim /etc/httpd/conf/httpd.conf

...

337 <Directory /var/www/html/authdir>

338         Order allow,deny

339         Allow from all

340 </Directory>

[[email protected] ~]# service httpd restart

在不同客户端测试

[[email protected] ~]# tail /var/log/httpd/error_log

 

试验四:HTTP的用户授权

     客户端访问http://www.gx.com/authdir需要输入用户名密码验证

1、修改主配置文件

[[email protected] ~]# vim /etc/httpd/conf/httpd.conf

...

337 <Directory "/var/www/html/authdir">

338         Order allow,deny

339         Allow from all

340         AuthName "Please Input Password!!"

341         AuthType Basic

342         AuthUserFile "/etc/httpd/.vuser"

343         Require valid-user

344 </Directory>

...

2、创建账户密码

[[email protected] ~]# htpasswd -c /etc/httpd/.vuser admin

New password:

Re-type new password:

Adding password for user admin

3、启动服务测试

[[email protected] ~]# service httpd restart

http://www.gx.com/authdir

实验五:HTTP目录别名    

客户端访问http://www.gx.com/baidu时可以访问/var/www/html/baidu.com/bbs下的网页

1、创建测试站点

[[email protected] ~]# mkdir -p /var/www/html/baidu.com/bbs

[[email protected] ~]# cat /var/www/html/baidu.com/bbs/index.html

<html>

<head><title>This is a test Page!!!</title></head>

<body>

<h1>This is bbs.baidu.com test Page!!!</h1>

</body>

</html>

2、修改主配置文件

[[email protected] ~]# tail -n 1 /etc/httpd/conf/httpd.conf

Alias /baidu     "/var/www/html/baidu.com/bbs"

3、启动服务测试

[[email protected] ~]# service httpd restart

http://www.gx.com/baidu

 

实验六:查看默认HTTP使用进程管理方式       更改默认进程管理方式为worker模式

[[email protected] ~]# httpd -l

Compiled in modules:                                   

  core.c

  prefork.c

  http_core.c

  mod_so.c

[[email protected] ~]# cd /usr/sbin/

[[email protected] sbin]# mv httpd httpd.prefork

[[email protected] sbin]# mv httpd.worker httpd

[[email protected] sbin]# service httpd restart

[[email protected] sbin]# httpd -l

Compiled in modules:

  core.c

  worker.c

  http_core.c

  mod_so.c

 

试验七:部署Awstats统计Http访问日志

1、安装软件(软件在/usr/src下)

[[email protected] ~]# cd /usr/src/

[[email protected] src]# tar -zxvf awstats-7.1.tar.gz -C /usr/local/

[[email protected] src]# cd /usr/local/

[[email protected] local]# mv awstats-7.1/ awstats

[[email protected] local]# cd awstats/tools/

[[email protected] tools]# ./awstats_configure.pl

...

Config file path (\'none\' to skip web server setup):

> /etc/httpd/conf/httpd.conf    //输入apache的主配置文件

...

-----> Need to create a new config file ?

Do you want me to build a new AWStats config/profile

file (required if first install) [y/N] ? y   //生成awstats的配置文件

...

Your web site, virtual server or profile name:

> www.gx.com            //输入你的web服务器名字

...

Default: /etc/awstats

Directory path to store config file(s) (Enter for default):

>

...

/usr/local/awstats/tools/awstats_updateall.pl now

Press ENTER to continue...

...

Press ENTER to finish...

2、修改主配置文件

[[email protected] tools]# vim /etc/awstats/awstats.www.gx.com.conf

...

  51 LogFile="/var/log/httpd/access_log"

[[email protected] tools]# mkdir /var/lib/awstats

3、将日志文件导入Awstats

[[email protected] tools]# ./awstats_updateall.pl now

[[email protected] tools]# crontab -l

----------------------------------------------

  输入该命令出现错误提示no crontab for root

  这个问题非常简单,同样在 root 用户下输入 crontab -e 按 Esc 按: wq   回车

  在输入 crontab -l 就没有问题了

  主要原因是由于这个liunx服务器 第一次使用 crontab ,还没有生成对应的文件导致的,执行了 编辑(crontab -e)后 就生成了这个文件

----------------------------------------------

[[email protected] tools]# service crond restart

[[email protected] tools]# chkconfig crond on

4、验证:

http://www.gx.com/awstats/awstats.pl?config=www.gx.com

补充:

通过html代码实现网页跳转功能

[[email protected] tools]# cat /var/www/html/awstats.html

<html>

<head><meta http-equiv=refresh content="0;  url=http://www.gx.com/awstats/awstats.pl? config=www.gx.com">

</head>

<body>

</body>

</html>

验证:

http://www.gx.com/awstats.html 

 

实验八:基于域名的虚拟主机

www.baidu.com   192.168.126.133   baidu网站    

www.sohu.com    192.168.126.133   sohu网站

1、修改主配置文件新建一个配置文件(虚似主机配置专用)

[[email protected] ~]# vim /etc/httpd/conf.d/virt.conf

NameVirtualHost *:80

<VirtualHost *:80>

DocumentRoot /var/www/baidu

ServerName www.baidu.comE

rrorLog logs/www.baidu.com-error_log

CustomLog logs/www.baidu.com-access_log common

</VirtualHost>

<VirtualHost *:80>

DocumentRoot /var/www/sohu

ServerName www.sohu.com

ErrorLog logs/www.sohu.com-error_log

CustomLog logs/www.sohu.com-access_log common

</VirtualHost>

创建网站目录和文件

[[email protected] ~]# mkdir /var/www/{baidu,sohu}

[[email protected] ~]# cat /var/www/baidu/index.html

<h1>www.baidu.com</h1>

[[email protected] ~]# cat /var/www/sohu/index.html

<h1>www.sohu.com</h1>

[[email protected] ~]# service httpd restart

验证:先在客户端修改hosts文件

192.168.126.133         www.baidu.com

192.168.126.133         www.sohu.com

 

实验九:基于端口的虚拟主机    

192.168.126.133:8081              baidu网站    

192.168.126.133:8082              sohu网站

2、修改主配置文件新建一个配置文件(虚似主机配置专用)

[[email protected] ~]# vim /etc/httpd/conf.d/virt.conf

Listen 8081     //**端口

Listen 8082

<VirtualHost *:8081>

DocumentRoot /var/www/baidu

ServerName 192.168.126.133

ErrorLog logs/www.baidu.com-error_log

CustomLog logs/www.baidu.com-access_log common

</VirtualHost>

<VirtualHost *:8082>

DocumentRoot /var/www/sohu

ServerName 192.168.10.253

ErrorLog logs/www.sohu.com-error_log

CustomLog logs/www.sohu.com-access_log common

</VirtualHost>

2、启动服务

[[email protected] ~]# service httpd restart

 

实验十:配置HTTP支持php

1、安装php

[[email protected] ~]# yum -y install php

[[email protected] ~]# cat /var/www/baidu/test.php

<?php                                              //PHP测试页

phpinfo();

?>

[[email protected] ~]# service httpd restart

测试:

http://192.168.126.133:8081/test.php