当前位置:   article > 正文

WEB服务及Apache 服务的搭建与配置——基于域名 端口 Ip多方式访问_apache ip访问

apache ip访问
1、WEB服务简介
# 目前最主流的三个Web服务器是Apache、Nginx、 IIS。
- WEB服务器一般指网站服务器,可以向浏览器等Web客户端提供网站的访问,让全世界浏览。
- WEB服务器也称为WWW(WORLD WIDE WEB)服务器,主要功能是提供网上信息浏览服务。
- WEB服务器是一种被动程序只有当Internet上运行其他计算机中的浏览器发出的请求时,服务器才会响应
2、WEB 服务协议
# WEB 服务应用层使用HTTP协议。
# HTML(标准通用标记语言)格式的文件。
# 浏览器通过统一资源定位器(URL)去访问web服务。
# 为了解决HTTP协议的这一缺陷,需要使用另一种协议:安全套接字层超文本传输协议HTTPS。为了数据传输的安全,HTTPS在HTTP的基础上加入了SSL协议,SSL依靠证书来验证服务器的身份,并为浏览器和服务器之间的通信加密。
# WEB服务采用的是浏览器/服务器结构
web服务器只能解析静态页面。 动态页面:只要和数据库进行连接的都属于动态页面,比如java写的代码,PHP的代码,python的代码。
web服务器:apache nginx IIS  #端口全部为80!https为443端口
前端页面:静态元素: .html .img js css swf 配合:apache、nginx.
后端页面:动态元素:根据不同的开发语言: .php .jsp .py  配合:java、php、python
​专门解析php代码的web中间件(web容器)--php-fpm端口9000
专门解析java代码的web中间件--tomcat(8080).
专门解析python代码的web中间件 ---uwsgi(5000)
​SQL
数据库:mysql、mariadb
3.Apache 服务的搭建与配置
Apache 介绍

Apache HTTP Server(简称Apache)是Apache软件基金会的一个开放源码的网页服务器,Apache是世界使用排名第一的Web服务器软件。它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一。

Apache的主程序名叫httpd。
一、apache安装
  1. [root@qfedu.com ~]# systemctl stop firewalld
  2. [root@qfedu.com ~]# systemctl disable firewalld
  3. [root@qfedu.com ~]# setenforce 0
  4. [root@qfedu.com ~]# yum install -y httpd
  5. [root@qfedu.com ~]# systemctl start httpd
  6. [root@qfedu.com ~]# netstat -lntp | grep 80 #查看apache端口
  7. tcp6 0 0 :::80 :::* LISTEN 2776/httpd
  8. #端口80.可以改
  9. index.html:默认访问网站的主页名称
  10. 默认发布网站的目录:/var/www/html

1.apache目录介绍

apache的工作目录:
conf   存储配置文件
conf.d 存储配置子文件
logs   存储日志 
modules 存储模块
run    存储Pid文件,存放的pid号码。是主进程号
  1. 认识主配置文件:
  2. [root@ailiyun ~]# -r -i.bak "/#.*/d" conf/httpd.conf #删除#号内容
  3. # vim /etc/httpd/conf/httpd.conf
  4. ServerRoot "/etc/httpd" #定义工作目录
  5. Listen 80 #监听端口
  6. Listen 192.168.2.8:80 指定监听的本地网卡 可以修改
  7. User apache # 子进程的用户,有可能被人改称www账户
  8. Group apache # 子进程的组
  9. ServerAdmin root@localhost # 设置管理员邮件地址
  10. DocumentRoot "/var/www/html" # 发布网站的默认目录,想改改这里。
  11. IncludeOptional conf.d/*.conf # 包含conf.d目录下的所有*.conf配置文件
  12. # 设置DocumentRoot指定目录的属性
  13. <Directory "/var/www/html"> # 网站容器开始标识
  14. Options Indexes FollowSymLinks # 找不到主页时,链接到网站目录以外,如测试页面
  15. AllowOverride None # 对网站设置特殊属性:none不设置特殊属性,all允许
  16. Require all granted # granted表示允许所有人访问,denied表示拒绝所有人访问
  17. </Directory> # 容器结束
  18. DirectoryIndex index.html # 定义主页文件,会自动访问该文件。
二、访问控制

1.准备测试页面

[root@qfedu.com ~]# echo test1 > /var/www/html/index.html #编写测试文件

2.访问控制测试

可以直接编辑apache主配置文件

1.默认允许所有主机访问
[root@qfedu.com ~]# vim /etc/httpd/conf/httpd.conf

[root@qfedu.com ~]# systemctl restart httpd

2.只拒绝一部分客户端访问:
[root@qfedu.com ~]# vim /etc/httpd/conf/httpd.conf
​
访问网站服务器反回的状态码:403 没有权限访问
200:表示访问网站成功

[root@qfedu.com ~]# systemctl restart httpd

  1. [root@test ~]# curl -I http://192.168.153.144 #用另外一台机器测试访问成功
  2. HTTP/1.1 200 OK
  3. Date: Thu, 06 Aug 2020 20:40:37 GMT
  4. Server: Apache/2.4.6 (CentOS)
  5. Last-Modified: Thu, 06 Aug 2020 20:12:02 GMT
  6. ETag: "6-5ac3b1a02ac4f"
  7. Accept-Ranges: bytes
  8. Content-Length: 6
  9. Content-Type: text/html; charset=UTF-8
在Linux中curl是一个利用URL规则在命令行下工作的文件传输工具,它支持文件的上传和下载,是综合传输工具,习惯称url为下载工具。
-o:指定下载路径
-I:查看服务器的响应信息
3.拒绝所有人
[root@qfedu.com ~]# vim /etc/httpd/conf/httpd.conf

[root@qfedu.com ~]# systemctl restart httpd

  1. [root@test ~]# curl -I http://192.168.153.144
  2. HTTP/1.1 403 Forbidden
  3. Date: Thu, 06 Aug 2020 20:38:00 GMT
  4. Server: Apache/2.4.6 (CentOS)
  5. Content-Type: text/html; charset=iso-8859-1
修改默认网站发布目录
  1. [root@qfedu.com ~]# vim /etc/httpd/conf/httpd.conf
  2. 119 DocumentRoot "/www" # 修改网站根目录为/www
  3. 131 <Directory "/www"> # 把这个也对应的修改为/www
  4. [root@qfedu.com ~]# mkdir /www #创建定义的网站发布目录
  5. [root@qfedu.com ~]# echo "这是新修改的网站根目录/www" > /www/index.html #创建测试页面
  6. [root@qfedu.com ~]# systemctl restart httpd #重启服务

三、虚拟主机
虚拟主机:将多个网站放在一台服务器上。web服务器都可以实现。
三种:基于域名 基于端口 基于Ip
  1. 1.基于域名
  2. [root@qfedu.com ~]# cd /etc/httpd/conf.d/
  3. [root@qfedu.com conf.d]# vim test.conf #创建配置文件
  4. <VirtualHost *:80> #指定虚拟主机端口,*代表监听本机所有ip,也可以指定ip
  5. DocumentRoot /soso #指定发布网站目录,自己定义
  6. ServerName www.soso666.com #指定域名,可以自己定义
  7. <Directory "/soso/">
  8. AllowOverride None #设置目录的特性,不设置目录的特性
  9. Require all granted #允许所有人访问
  10. </Directory>
  11. </VirtualHost>
  12. <VirtualHost *:80>
  13. DocumentRoot /soho
  14. ServerName test.soso666.com
  15. <Directory "/soho/">
  16. AllowOverride None
  17. Require all granted
  18. </Directory>
  19. </VirtualHost>
  20. [root@qfedu.com ~]# mkdir /soso #创建发布目录
  21. [root@qfedu.com ~]# mkdir /soho
  22. [root@qfedu.com ~]# echo qianfen > /soso/index.html #创建测试页面
  23. [root@qfedu.com ~]# echo qfedu > /soho/index.html
  24. [root@qfedu.com ~]# systemctl restart httpd

配置域名解析:

在wind电脑上面打开C:\Windows\System32\drivers\etc\hosts文件。可以用管理员身份打开

测试访问

基于端口

[root@qfedu.com ~]# vim /etc/httpd/conf/httpd.conf  ---添加

2.基于端口

  1. [root@qfedu.com ~]# vim /etc/httpd/conf.d/test.conf
  2. <VirtualHost *:80>
  3. DocumentRoot /soso
  4. ServerName www.soso666.com
  5. <Directory "/soso/">
  6. AllowOverride None
  7. Require all granted
  8. </Directory>
  9. </VirtualHost>
  10. <VirtualHost *:81> #修改端口
  11. DocumentRoot /soho
  12. ServerName test.soso666.com
  13. <Directory "/soho/">
  14. AllowOverride None
  15. Require all granted
  16. </Directory>
  17. </VirtualHost>
  18. [root@qfedu.com ~]# systemctl restart httpd
注意:域名解析并没有变

访问:www.soso666.com

访问: test.soso666.com:81

3.基于IP


加ip    ip addr add 10.36.181.183/24 dev ens33
删除ip  ip addr del 10.36.181.183/24 dev ens33
  1. [root@qfedu.com ~]# vim /etc/httpd/conf.d/test.conf
  2. <VirtualHost *:80>
  3. DocumentRoot /soso
  4. ServerName www.soso666.com
  5. <Directory "/soso/">
  6. AllowOverride None
  7. Require all granted
  8. </Directory>
  9. </VirtualHost>
  10. <VirtualHost *:81> #修改端口
  11. DocumentRoot /soho
  12. ServerName test.soso666.com
  13. <Directory "/soho/">
  14. AllowOverride None
  15. Require all granted
  16. </Directory>
  17. </VirtualHost>
  18. [root@qfedu.com ~]# systemctl restart httpd

可以配置域名解析,也可以不用配域名解析

Nginx 服务的搭建与配置
Nginx介绍

Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,由俄罗斯的程序设计师Igor Sysoev所开发,其特点是占有内存少,并发能力强。事实上nginx的并发能力确实在同类型的网页服务器中表现较好。

安装 Nginx

1.2Nginx基本使用

获取Nginx
Nginx的官方主页: http://nginx.org
关闭防火墙关闭selinux

  1. [root@qfedu.com ~]# systemctl stop firewalld #关闭防火墙
  2. [root@qfedu.com ~]# systemctl disable firewalld #开机关闭防火墙
  3. [root@qfedu.com ~]# setenforce 0 #临时关闭selinux
  4. [root@qfedu.com ~]# getenforce #查看selinux状态
​
Nginx安装:
Yum方式:
  1. [root@qfedu.com ~]# cd /etc/yum.repos.d/
  2. [root@qfedu.com yum.repos.d]# vi nginx.repo #编写nginx的yum源
  3. [nginx]
  4. name=nginx
  5. baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
  6. gpgcheck=0
  7. enabled=1
  8. [root@qfedu.com yum.repos.d]# yum clean all
  9. [root@qfedu.com yum.repos.d]# yum makecache
  10. [root@qfedu.com ~]# yum install -y nginx #安装nginx
  11. [root@qfedu.com ~]# systemctl start nginx #启动
  12. [root@qfedu.com ~]# systemctl restart nginx #重启
  13. [root@qfedu.com ~]# systemctl enable nginx #开机启动
  14. [root@qfedu.com ~]# systemctl stop nginx #关闭
1.查看nginx状态

  1. [root@qfedu.com ~]# ps aux | grep nginx
  2. root 3927 0.0 0.0 46384 968 ? Ss 18:46 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
  3. nginx 3928 0.0 0.1 46792 1932 ? S 18:46 0:00 nginx: worker process
  4. root 3932 0.0 0.0 112660 968 pts/1 R+ 18:47 0:00 grep --color=auto nginx
2.查看nginx端口

  1. [root@qfedu.com ~]# netstat -lntp | grep 80
  2. tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3927/nginx: master
  3. #注意:nginx默认端口为80
3.测试主页是否可以访问:
  1. [root@qfedu.com ~]# curl -I http://127.0.0.1
  2. HTTP/1.1 200 OK
  3. Server: nginx/1.16.1
  4. Date: Sat, 16 Nov 2019 10:49:48 GMT
  5. Content-Type: text/html
  6. Content-Length: 635
  7. Last-Modified: Fri, 11 Oct 2019 06:45:33 GMT
  8. Connection: keep-alive
  9. ETag: "5da0250d-27b"
  10. Accept-Ranges: bytes

常见的组合方式
LNMP (Linux + Nginx + MySQL/Mariadb + PHP)  #php-fpm进程,这个组合是公司用的最多的组合
LAMP (Linux + Apache + MySQL/Mariadb + PHP)
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号