ubuntu Apache安装设置

  • 安装Apache2
sudo apt-get install apache2 
  • 编辑/etc/apache2/apache2.conf(新安装的系统可能在/etc/apache2/conf.d/charset)文件,在改动之前,请先将该配置文件做个备份。以便在出错的时候可以恢复。将
AddDefaultCharset ISO-8859-1

改为

AddDefaultCharset GB2312

当然如果你网站的编码用的是UTF-8的话,也可以改为

AddDefaultCharset UTF-8

这样的话,我们就不会每次打开网页都是乱码了!

  • 增加安全模块,以保障Apache服务的正常运行,现在我们安装mod-security。(可选)

a. 首先安装libapache2-mod-security包(7.10之后的系统已经没有此包,可以跳过此步骤)

sudo apt-get install libapache2-mod-security

a. 该模块默认是没激活的,我们可以在/etc/apache2/mods-available目录下看到有mod-security.load文件,但在/etc/apache2/mods-enabled目录却没有它的软链接。我们现在激活它:

$sudo a2enmod mod-security
$sudo cp /usr/share/doc/libapache2-mod-security/examples/httpd2.conf.example-full /etc/apache2/mods-available/mod-security.conf
$sudo cp /etc/apache2/mods-available/mod-security.conf /etc/apache2/mods-available/mod-security.conf.orig
$sudo vi /etc/apache2/mods-available/mod-security.conf

==== mod-security.conf 文件内容开始====
# 检测内容长度以避免堆溢出攻击
SecFilterForceByteRange 32 254 =>SecFilterForceByteRange 32 126

# debug设置
SecFilterDebugLevel 9 =>SecFilterDebugLevel 0

# 设置缺省的动作
SecFilterDefaultAction "deny,log,status:499" =>SecFilterDefaultAction "deny,log,status:404"

# 把设置传递给子目录
SecFilterInheritance Off

# Redirect user on filter match
# 当匹配sh的时候,重新定向到一个特殊的警告页面,该页面是自行编写的,写些警告的话让攻击者知难而退,该段先不要生效,等到相关配置配好之后再失效不迟。记住在配好之后要使之生效。
#SecFilter sh redirect:http://localhost/hack/warning.htm

# Prevent OS specific keywords
#过滤一些敏感的东西,我们使用*是为了攻击者使用/etc/./passwd来绕开检测
SecFilter /etc/passwd =>SecFilter /etc/*passwd
SecFilter /bin/*sh

# Very crude filters to prevent SQL injection attacks
# 防止SQL插入(SQL Injection)攻击
SecFilter "delete[[[space|]]]+from"
SecFilter "insert[[[space|]]]+into"
SecFilter "select.+from"
SecFilter "select[[[space|]]]+from"
SecFilter "union[[[space|]]]+from"
==== mod-security.conf 文件内容结束====

sudo ln -s /etc/apache2/mods-available/mod-security.conf /etc/apache2/mods-enabled/mod-security.conf

a. 重启Apache2服务即可。

sudo /etc/init.d/apache2 restart

备注:第三步可能会引起部分网站不能正常运行,可以参照着去掉某些限制,由于是安全模块,所以参照的是防火墙的做法,关掉一切不安全的,再根据需要打开必要的。

================================================================================

ubuntu apache2配置

1.apache2.conf 是主配置文件,httpd.conf 用户配置文件
2.虚拟目录在 httpd.conf 中
<VirtualHost *>
DocumentRoot "路径"
ServerName 名称
<Direc×y "路径"> allow from all Options +Indexes </Direc×y>
</VirtualHost>
3.根设置(默认主目录)在 /etc/apache2/sites-available/default
4.重启命令
sudo /etc/init.d/apache2 restart或者
cd /etc/init.d
sudo apache2 -k restart
stop 停止;start 启动5.日志文件在 /var/log/apache2/
<VirtualHost *:80>
ServerName www.kimoqi.com
DocumentRoot /home/vsftpd/kimoqi
</VirtualHost>
<VirtualHost *:80>
ServerName www.arwenedu.com
DocumentRoot /home/vsftpd/wangguan/webapps
</VirtualHost>
<VirtualHost *:80>
ServerName www.arwenedu.org.cn
DocumentRoot /home/vsftpd/wangguan/chem
</VirtualHost>

vi /etc/httpd/conf/httpd.conf

在Windows下,Apache的配置文件通常只有一个,就是httpd.conf。但我在Ubuntu Linux上用apt-get install apache2命令安装了Apache2后,竟然发现它的httpd.conf(位于/etc/apache2目录)是空的!进而发现Ubuntu的 Apache软件包的配置文件并不像Windows的那样简单,它把各个设置项分在了不同的配置文件中,看起来复杂,但仔细想想设计得确实很合理。

严格地说,Ubuntu的Apache(或者应该说Linux下的Apache?我不清楚其他发行版的apache软件包)的配置文件是/etc /apache2/apache2.conf,Apache在启动时会自动读取这个文件的配置信息。而其他的一些配置文件,如httpd.conf等,则 是通过Include指令包含进来。在apache2.conf中可以找到这些Include行:

引用

# Include module configuration:
Include /etc/apache2/mods-enabled/*.load
Include /etc/apache2/mods-enabled/*.conf

# Include all the user configurations:
Include /etc/apache2/httpd.conf

# Include ports listing
Include /etc/apache2/ports.conf
……
# Include generic snippets of statements
Include /etc/apache2/conf.d/

# Include the virtual host configurations:
Include /etc/apache2/sites-enabled/
结合注释,可以很清楚地看出每个配置文件的大体作用。当然,你完全可以把所有的设置放在apache2.conf或者httpd.conf或者任何一个配置文件中。Apache2的这种划分只是一种比较好的习惯。

安装完Apache后的最重要的一件事就是要知道Web文档根目录在什么地方,对于Ubuntu而言,默认的是/var/www。怎么知道的 呢?apache2.conf里并没有DocumentRoot项,httpd.conf又是空的,因此肯定在其他的文件中。经过搜索,发现在/etc /apache2/sites-enabled/000-default中,里面有这样的内容:

引用

NameVirtualHost *
<VirtualHost *>
ServerAdmin webmaster@localhost

DocumentRoot /var/www/
……
这是设置虚拟主机的,对我来说没什么意义。所以我就把apache2.conf里的Include /etc/apache2/sites-enabled/一行注释掉了,并且在httpd.conf里设置DocumentRoot为我的用户目录下的某 个目录,这样方便开发。

再看看/etc/apache2目录下的东西。刚才在apache2.conf里发现了sites-enabled目录,而在/etc /apache2下还有一个sites-available目录,这里面是放什么的呢?其实,这里面才是真正的配置文件,而sites-enabled目 录存放的只是一些指向这里的文件的符号链接,你可以用ls /etc/apache2/sites-enabled/来证实一下。所以,如果apache上配置了多个虚拟主机,每个虚拟主机的配置文件都放在 sites-available下,那么对于虚拟主机的停用、启用就非常方便了:当在sites-enabled下建立一个指向某个虚拟主机配置文件的链 接时,就启用了它;如果要关闭某个虚拟主机的话,只需删除相应的链接即可,根本不用去改配置文件。

mods-available、mods-enabled和上面说的sites-available、sites-enabled类似,这两个目录 是存放apache功能模块的配置文件和链接的。当我用apt-get install php5安装了PHP模块后,在这两个目录里就有了php5.load、php5.conf和指向这两个文件的链接。这种目录结果对于启用、停用某个 Apache模块是非常方便的。

最后一个要说的是ports.conf,这里面设置了Apache使用的端口。如果需要调整默认的端口设置,建议编辑这个文件。或者你嫌它实在多 余,也可以先把apache2.conf中的Include /etc/apache2/ports.conf一行去掉,在httpd.conf里设置Apache端口。

ubuntu里缺省安装的目录结构很有一点不同。在ubuntu中module和 virtual host的配置都有两个目录,一个是available,一个是enabled,available目录是存放有效的内容,但不起作用,只有用ln 连到enabled过去才可以起作用。对调试使用都很方便,但是如果事先不知道,找起来也有点麻烦。

/etc/apache2/sites-available 里放的是VH的配置,但不起作用,要把文件link到 sites-enabled 目录里才行。

  1. <VirtualHost *>  
  2.         ServerName 域名  
  3.   
  4.         DocumentRoot 把rails项目里的public当根目录  
  5.         <Direc×y public根目录>  
  6.                 Options ExecCGI FollowSymLinks  
  7.                 AllowOverride all  
  8.                 allow from all  
  9.                 Order allow,deny  
  10.         </Direc×y>  
  11.         ErrorLog /var/log/apache2/error-域名.log  
  12. </VirtualHost>
原文地址:https://www.cnblogs.com/zhwl/p/2938596.html