Web主要两部分:前端和服务器。
配置mysql和Apache使用wampserver,快速便捷的配置。当然,不止有Apache支持此类服务,还有Nginx,Tomcat,Jboss等
主要配置两个东西,一个是本地测试,一个是服务器。
目的是在浏览器中输入local.myphp.com能访问到我们的网站。
localhost文件地址:c://Windows/System32/drivers/etc/hosts
Apache的配置由httpd.conf文件配置,因此下面的配置指令都是在httpd.conf文件中修改。
httpd.conf文件位置:wampserver安装目录下bin->apache->apache2.4.9->conf
主站点的配置(基本配置)
(1) 基本配置:
ServerRoot "/mnt/software/apache2"
#你的apache软件安装的位置。其它指定的目录如果没有指定绝对路径,则目录是相对于该目录。
PidFile logs/httpd.pid #第一个httpd进程(所有其他进程的父进程)的进程号文件位置。
Listen 80 #服务器监听的端口号。
ServerName www.clusting.com:80 #主站点名称(网站的主机名)。
ServerAdmin admin@clusting.com #管理员的邮件地址。
DocumentRoot "/mnt/web/clusting" #主站点的网页存储位置。
以下是对主站点的目录进行访问控制:
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
在上面这段目录属性配置中,主要有下面的选项:
Options:配置在特定目录使用哪些特性,常用的值和基本含义如下:
ExecCGI: 在该目录下允许执行CGI脚本。
FollowSymLinks: 在该目录下允许文件系统使用符号连接。
Indexes: 当用户访问该目录时,如果用户找不到DirectoryIndex指定的主页文件(例如index.html),则返回该目录下的文件列表给用户。
SymLinksIfOwnerMatch: 当使用符号连接时,只有当符号连接的文件拥有者与实际文件的拥有者相同时才可以访问。
其它可用值和含义请参阅:http://www.clusting.com/Apache/ApacheManual/mod/core.html#options
AllowOverride:允许存在于.htaccess文件中的指令类型(.htaccess文件名是可以改变的,其文件名由AccessFileName指令决定):
None: 当AllowOverride被设置为None时。不搜索该目录下的.htaccess文件(可以减小服务器开销)。
All: 在.htaccess文件中可以使用所有的指令。
其他的可用值及含义(如:Options FileInfo AuthConfig Limit等),请参看: http://www.clusting.com/Apache/ApacheManual/mod/core.html#AllowOverride
Order:控制在访问时Allow和Deny两个访问规则哪个优先:
Allow:允许访问的主机列表(可用域名或子网,例如:Allow from 192.168.0.0/16)。
Deny:拒绝访问的主机列表。
更详细的用法可参看:http://www.clusting.com/Apache/ApacheManual/mod/mod_access.html#order
DirectoryIndex index.html index.htm index.php #主页文件的设置(本例将主页文件设置为:index.html,index.htm和index.php)
(2) 服务器的优化 (MPM: Multi-Processing Modules)
apache2主要的优势就是对多处理器的支持更好,在编译时同过使用--with-mpm选项来决定apache2的工作模式。如果知道当前的apache2使用什么工作机制,可以通过httpd -l命令列出apache的所有模块,就可以知道其工作方式:
prefork:如果httpd -l列出prefork.c,则需要对下面的段进行配置:
StartServers 5 #启动apache时启动的httpd进程个数。
MinSpareServers 5 #服务器保持的最小空闲进程数。
MaxSpareServers 10 #服务器保持的最大空闲进程数。
MaxClients 150 #最大并发连接数。
MaxRequestsPerChild 1000 #每个子进程被请求服务多少次后被kill掉。0表示不限制,推荐设置为1000。
在该工作模式下,服务器启动后起动5个httpd进程(加父进程共6个,通过ps -ax|grep httpd命令可以看到)。当有用户连接时,apache会使用一个空闲进程为该连接服务,同时父进程会fork一个子进程。直到内存中的空闲进程达到MaxSpareServers。该模式是为了兼容一些旧版本的程序。我缺省编译时的选项。
worker:如果httpd -l列出worker.c,则需要对下面的段进行配置:
StartServers 2 #启动apache时启动的httpd进程个数。
MaxClients 150 #最大并发连接数。
MinSpareThreads 25 #服务器保持的最小空闲线程数。
MaxSpareThreads 75 #服务器保持的最大空闲线程数。
ThreadsPerChild 25 #每个子进程的产生的线程数。
MaxRequestsPerChild 0 #每个子进程被请求服务多少次后被kill掉。0表示不限制,推荐设置为1000。
该模式是由线程来监听客户的连接。当有新客户连接时,由其中的一个空闲线程接受连接。服务器在启动时启动两个进程,每个进程产生的线程数是固定的(ThreadsPerChild决定),因此启动时有50个线程。当50个线程不够用时,服务器自动fork一个进程,再产生25个线程。
perchild:如果httpd -l列出perchild.c,则需要对下面的段进行配置:
NumServers 5 #服务器启动时启动的子进程数
StartThreads 5 #每个子进程启动时启动的线程数
MinSpareThreads 5 #内存中的最小空闲线程数
MaxSpareThreads 10 #最大空闲线程数
MaxThreadsPerChild 2000 #每个线程最多被请求多少次后退出。0不受限制。
MaxRequestsPerChild 10000 #每个子进程服务多少次后被重新fork。0表示不受限制。
该模式下,子进程的数量是固定的,线程数不受限制。当客户端连接到服务器时,又空闲的线程提供服务。 如果空闲线程数不够,子进程自动产生线程来为新的连接服务。该模式用于多站点服务器。
(3) HTTP返头回信息配置:
ServerTokens Prod #该参数设置http头部返回的apache版本信息,可用的值和含义如下:
Prod:仅软件名称,例如:apache
Major:包括主版本号,例如:apache/2
Minor:包括次版本号,例如:apache/2.0
Min:仅apache的完整版本号,例如:apache/ 2.0.54
OS:包括操作系统类型,例如:apache/2.0.54(Unix)
Full:包括apache支持的模块及模块版本号,例如:Apache/2.0.54 (Unix) mod_ssl/2.0.54 OpenSSL/0.9.7g
ServerSignature Off #在页面产生错误时是否出现服务器版本信息。推荐设置为Off
(4) 持久性连接设置
KeepAlive On #开启持久性连接功能。即当客户端连接到服务器,下载完数据后仍然保持连接状态。
MaxKeepAliveRequests 100 #一个连接服务的最多请求次数。
KeepAliveTimeout 30 #持续连接多长时间,该连接没有再请求数据,则断开该连接。缺省为15秒。
别名设置
对于不在DocumentRoot指定的目录内的页面,既可以使用符号连接,也可以使用别名。别名的设置如下:
Alias /download/ "/var/www/download/" #访问时可以输入:http://www.custing.com/download/
#对该目录进行访问控制设置
Options Indexes MultiViews
AllowOverride AuthConfig
Order allow,deny
Allow from all
CGI设置
ScriptAlias /cgi-bin/ "/mnt/software/apache2/cgi-bin/" # 访问时可以:http://www.clusting.com/cgi-bin/ 。但是该目录下的CGI脚本文件要加可执行权限!
#设置目录属性
AllowOverride None
Options None
Order allow,deny
Allow from all
个人主页的设置 (public_html)
UserDir public_html (间用户的主页存储在用户主目录下的public_html目录下 URL http://www.clusting.com/~bearzhang/file.html 将读取 /home/bearzhang/public_html/file.html 文件)
chmod 755 /home/bearzhang #使其它用户能够读取该文件。
UserDir /var/html (the URL http://www.clusting.com/~bearzhang/file.html 将读取 /var/html/bearzhang/file.html)
UserDir /var/www/*/docs (the URL http://www.clusting.com/~bearzhang/file.html 将读取 /var/www/bearzhang/docs/file.html)
日志的设置
(1)错误日志的设置
ErrorLog logs/error_log #日志的保存位置
LogLevel warn #日志的级别
显示的格式日下:
[Mon Oct 10 15:54:29 2005] [error] [client 192.168.10.22] access to /download/ failed, reason: user admin not allowed access
(2)访问日志设置
日志的缺省格式有如下几种:
LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"" combined
LogFormat "%h %l %u %t "%r" %>s %b" common #common为日志格式名称
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
CustomLog logs/access_log common
格式中的各个参数如下:
%h --客户端的ip地址或主机名
%l --The 这是由客户端 identd 判断的RFC 1413身份,输出中的符号 "-" 表示此处信息无效。
%u --由HTTP认证系统得到的访问该网页的客户名。有认证时才有效,输出中的符号 "-" 表示此处信息无效。
%t --服务器完成对请求的处理时的时间。
"%r" --引号中是客户发出的包含了许多有用信息的请求内容。
%>s --这个是服务器返回给客户端的状态码。
%b --最后这项是返回给客户端的不包括响应头的字节数。
"%{Referer}i" --此项指明了该请求是从被哪个网页提交过来的。
"%{User-Agent}i" --此项是客户浏览器提供的浏览器识别信息。
下面是一段访问日志的实例:
192.168.10.22 - bearzhang [10/Oct/2005:16:53:06 +0800] "GET /download/ HTTP/1.1" 200 1228
192.168.10.22 - - [10/Oct/2005:16:53:06 +0800] "GET /icons/blank.gif HTTP/1.1" 304 -
192.168.10.22 - - [10/Oct/2005:16:53:06 +0800] "GET /icons/back.gif HTTP/1.1" 304 -
各参数的详细解释,请参阅:http://www.clusting.com/Apache/ApacheManual/logs.html
用户认证的配置
(1)in the httpd.conf:
AccessFileName .htaccess
.........
Alias /download/ "/var/www/download/"
Options Indexes
AllowOverride AuthConfig
(2) create a password file:
/usr/local/apache2/bin/htpasswd -c /var/httpuser/passwords bearzhang
(3)onfigure the server to request a password and tell the server which users are allowed access.
vi /var/www/download/.htaccess:
AuthType Basic
AuthName "Restricted Files"
AuthUserFile /var/httpuser/passwords
Require user bearzhang
#Require valid-user #all valid user
虚拟主机的配置
(1)基于IP地址的虚拟主机配置
Listen 80
DocumentRoot /www/example1
ServerName www.example1.com
DocumentRoot /www/example2
ServerName www.example2.org
(2) 基于IP和多端口的虚拟主机配置
Listen 172.20.30.40:80
Listen 172.20.30.40:8080
Listen 172.20.30.50:80
Listen 172.20.30.50:8080
DocumentRoot /www/example1-80
ServerName www.example1.com
DocumentRoot /www/example1-8080
ServerName www.example1.com
DocumentRoot /www/example2-80
ServerName www.example1.org
DocumentRoot /www/example2-8080
ServerName www.example2.org
(3)单个IP地址的服务器上基于域名的虚拟主机配置:
# Ensure that Apache listens on port 80
Listen 80
# Listen for virtual host requests on all IP addresses
NameVirtualHost *:80
DocumentRoot /www/example1
ServerName www.example1.com
ServerAlias example1.com. *.example1.com
# Other directives here
DocumentRoot /www/example2
ServerName www.example2.org
# Other directives here
(4)在多个IP地址的服务器上配置基于域名的虚拟主机:
Listen 80
# This is the "main" server running on 172.20.30.40
ServerName server.domain.com
DocumentRoot /www/mainserver
# This is the other address
NameVirtualHost 172.20.30.50
DocumentRoot /www/example1
ServerName www.example1.com
# Other directives here ...
DocumentRoot /www/example2
ServerName www.example2.org
# Other directives here ...
(5)在不同的端口上运行不同的站点(基于多端口的服务器上配置基于域名的虚拟主机):
Listen 80
Listen 8080
NameVirtualHost 172.20.30.40:80
NameVirtualHost 172.20.30.40:8080
ServerName www.example1.com
DocumentRoot /www/domain-80
ServerName www.example1.com
DocumentRoot /www/domain-8080
ServerName www.example2.org
DocumentRoot /www/otherdomain-80
ServerName www.example2.org
DocumentRoot /www/otherdomain-8080
(6)基于域名和基于IP的混合虚拟主机的配置:
Listen 80
NameVirtualHost 172.20.30.40
DocumentRoot /www/example1
ServerName www.example1.com
DocumentRoot /www/example2
ServerName www.example2.org
DocumentRoot /www/example3
ServerName www.example3.net
SSL加密的配置
首先在配置之前先来了解一些基本概念:
证书的概念:首先要有一个根证书,然后用根证书来签发服务器证书和客户证书,一般理解:服务器证书和客户证书是平级关系。SSL必须安装服务器证书来认证。 因此:在此环境中,至少必须有三个证书:根证书,服务器证书,客户端证书。 在生成证书之前,一般会有一个私钥,同时用私钥生成证书请求,再利用证书服务器的根证来签发证书。
SSL所使用的证书可以自己生成,也可以通过一个商业性CA(如Verisign 或 Thawte)签署证书。
签发证书的问题:如果使用的是商业证书,具体的签署方法请查看相关销售商的说明;如果是知己签发的证书,可以使用openssl自带的CA.sh脚本工具。
如果不为单独的客户端签发证书,客户端证书可以不用生成,客户端与服务器端使用相同的证书。
(1) conf/ssl.conf 配置文件中的主要参数配置如下:
Listen 443
SSLPassPhraseDialog buildin
#SSLPassPhraseDialog exec:/path/to/program
SSLSessionCache dbm:/usr/local/apache2/logs/ssl_scache
SSLSessionCacheTimeout 300
SSLMutex file:/usr/local/apache2/logs/ssl_mutex
# General setup for the virtual host
DocumentRoot "/usr/local/apache2/htdocs"
ServerName www.example.com:443
ServerAdmin you@example.com
ErrorLog /usr/local/apache2/logs/error_log
TransferLog /usr/local/apache2/logs/access_log
SSLEngine on
SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL
SSLCertificateFile /usr/local/apache2/conf/ssl.crt/server.crt
SSLCertificateKeyFile /usr/local/apache2/conf/ssl.key/server.key
CustomLog /usr/local/apache2/logs/ssl_request_log "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x "%r" %b"
(2) 创建和使用自签署的证书:
a.Create a RSA private key for your Apache server
/usr/local/openssl/bin/openssl genrsa -des3 -out /usr/local/apache2/conf/ssl.key/server.key 1024
b. Create a Certificate Signing Request (CSR)
/usr/local/openssl/bin/openssl req -new -key /usr/local/apache2/conf/ssl.key/server.key -out /usr/local/apache2/conf/ssl.key/server.csr
c. Create a self-signed CA Certificate (X509 structure) with the RSA key of the CA
/usr/local/openssl/bin/openssl req -x509 -days 365 -key /usr/local/apache2/conf/ssl.key/server.key -in /usr/local/apache2/conf/ssl.key/server.csr -out /usr/local/apache2/conf/ssl.crt/server.crt
/usr/local/openssl/bin/openssl genrsa 1024 -out server.key
/usr/local/openssl/bin/openssl req -new -key server.key -out server.csr
/usr/local/openssl/bin/openssl req -x509 -days 365 -key server.key -in server.csr -out server.crt
(3) 创建自己的CA(认证证书),并使用该CA来签署服务器的证书。
mkdir /CA
cd /CA
cp openssl-0.9.7g/apps/CA.sh /CA
./CA.sh -newca
openssl genrsa -des3 -out server.key 1024
openssl req -new -key server.key -out server.csr
cp server.csr newreq.pem
./CA.sh -sign
cp newcert.pem /usr/local/apache2/conf/ssl.crt/server.crt
cp server.key /usr/local/apache2/conf/ssl.key/
原文链接:https://www.cnblogs.com/hh1137054072/p/7612643.html
Apache服务的安装
yum源安装:
安装httpd主程序: yum -y install httpd
在/var/www/html 目录下写入我们自己的主页 index.html
开启httpd程序: systemctl start httpd
防火墙开放http服务 firewall-cmd --add-service=http ,关闭SElinux: setenforce=0
查看apache版本号:httpd -v
目录文件
程序目录:/usr/sbin/httpd
默认网站主页存放目录: /var/www/html/
日志文件存放目录:/var/log/httpd/
主配置文件:/etc/httpd/conf/httpd.conf
从配置文件:/etc/httpd/conf.d/
主配置文件 /etc/httpd/conf/httpd.conf 的一些参数的意义
ServerRoot “/etc/httpd“ //服务配置文件目录
PidFile run/httpd.pid //PID文件
Listen 80 //默认监听端口
Include conf.modules.d/*.conf //包含模块目录的配置文件
User apache //启动用户
Group apache //启动组
ServerAdmin root@localhost //管理员邮箱
ServerName www.example.com:80 //域名主机名
DocumentRoot “/var/www/html“ //默认主页存放目录
DirectoryIndex index.html index.html.var //索引文件
//规定网站根目录的位置
AllowOverride None
# Allow open access:
Require all granted
ErrorLog logs/error_log //错误日志
CustomLog logs/access_log combined //访问日志
AddDefaultCharset UTF-8 //默认字符集
IncludeOptional conf.d/*.conf //包含了 conf.d/*.conf 的配置文件
DirectoryIndex index.html //默认索引
扩展配置文件: /etc/httpd/conf.d/ , 我们可以在这个目录下新建一些我们自己定义的配置文件,比如基于ip的虚拟主机,基于端口的虚拟主机,基于域名的虚拟主机等等
源码包安装:
源码安装之前先安装编译工具: yum -y install gcc gcc-c++ make
源码包: httpd-xxxx.tar.bz2
解压,然后进入解压目录, ./configure 编译, make && make install 安装即可
进入安装目录的 bin 目录下, ./httpd 启动
目录文件:
安装完成后,软件默认安装在 /usr/loca/apache2/ 下面,也可以编译的时候自定义安装目录
可执行文件目录:/usr/local/apache2/bin/
网站主页目录:/usr/local/apache2/htdocs/
配置文件目录:/usr/local/apache2/conf/
日志文件目录:/usr/local/apache2/logs/
Apache中添加对php的支持
安装php的支持包 yum -y install php ,然后重启 httpd 服务即可
如果想让Apache识别默认的index.php文件,需要修改配置文件 /etc/httpd/conf/httpd.conf 中,在如下位置加入 index.php
DirectoryIndex index.php index.html
Apache中添加php对mysql数据库的支持
安装php-mysql的包即可:yum -y install php-mysql
Apache服务的高级配置
以下不管是基于ip、端口还是基于域名创建的虚拟主机,网站主页目录只能在 /var/www/ 下,如果想更改到其他的任意目录的话,需要修改配置文件 /etc/httpd/conf/httpd.conf
//这里的目录更改为你的网站主页目录
AllowOverride None
# Allow open access:
Require all granted
1:配置基于ip的虚拟主机
可以在一台服务器上配置多个网卡,也可以在一块网卡上配置爱多个ip
我这里在一块网上上配置多个ip: ifconfig eno16777736:1 10.96.10.204
然后在从配置文件中 /etc/httpd/conf.d/ 下创建一个配置文件,我们新建一个 ip.conf 文件,然后加入下面几行
DocumentRoot /var/www/html/web1 //第一个ip的网站的主页面目录
DocumentRoot /var/www/html/web2 //第二个ip的网站的主页面目录
然后在/var/www 下新建 web1和web2目录,在web1和web2目录里面分别放入我们的网站页面。然后重启httpd服务即可。
2. 配置基于端口的虚拟主机
在配置文件中 /etc/httpd/conf.d/ 下创建一个配置文件,我们新建一个 port.conf 文件,然后加入下面几行
Listen 8080
DocumentRoot /var/www/html/web1 //第一个端口的网站主目录
DocumentRoot /var/www/html/web2 //第二个端口的网站主目录
然后在/var/www 下新建 web1和web2目录,在web1和web2目录里面分别放入我们的网站页面。然后重启httpd服务即可。
3.配置基于域名的虚拟主机
在配置文件中 /etc/httpd/conf.d/ 下创建一个配置文件,我们新建一个 domain.conf 文件,然后加入下面几行
NameVirtualHost *:80
DocumentRoot /var/www/web1 //第一个域名的网站主目录
ServerName web1.xie.com
DocumentRoot /var/www/web2 //第二个域名的网站主目录
ServerName web2.xie.com
然后在/var/www 下新建 web1和web2目录,在里面放入我们的网站页面。然后再重启httpd服务即可。
HTTP服务的访问控制
控制对特定主页内容的访问,为相应网站目录添加访问控制
常用访问控制方式分类:
客户机地址限制
用户授权限制
设置客户机地址访问
Order配置项,定义控制顺序 Order allow,deny 先允许后拒绝,默认拒绝 Order deny,allow 先拒绝后允许,默认允许
Allow、Deny配置项,设置允许或拒绝的网段或地址 Deny from IP地址 或者 网段 Allow from IP地址 或者 网段
基于ip的用户访问控制的配置:
在配置文件中 /etc/httpd/conf.d/ 下创建一个配置文件,我们新建一个 ip.conf 文件,然后加入下面几行
// 这个的意思的允许所有用户访问/var/www/html 目录下的网页,但是只允许 10.96.10.204/24这个网段的访问 /var/www/html/test/ 目录下的网页
//本机ip
DocumentRoot /var/www/html //网站主页的目录
//要设置限制的目录
Order allow,deny //匹配了允许的就拒绝所有
Allow from 10.96.10.204/24 //只允许10.96.10.204这个地址访问
基于用户身份认证授权的配置:
访问控制的配置
在配置文件中 /etc/httpd/conf.d/ 下创建一个配置文件,我们新建一个auth.conf 文件,然后加入下面几行
htpasswd -c /etc/httpd/webpasswd tom (第一次加 -c 参数,表示创建文件); htpasswd /etc/httpd/webpasswd jack(第二次不用加 -c参数 ,因为文件已经创建)
重启服务验证
//本机ip
DocumentRoot /var/www/html //主页目录
//设置认证的目录
AuthType Basic //认证的方法:密码
AuthName Password! //弹出登录框
AuthUserFile /etc/httpd/webpasswd //用户名和密码保存的文件
require user tom jack //只允许 tom 和 jack 用户访问
原文:https://blog.csdn.net/qq_36119192/article/details/82768277