CentOS 6.5安裝Apache

一、Apache的特色html

功能強大、配置簡單、速度快、應用普遍、性能穩定可靠,並可作代理服務器或負載均衡來使用

二、Apache的應用場合node

使用Apache運行靜態HTML網頁、圖片(處理靜態小文件能力不及Nginx)。
使用Apache結合PHP引擎運行PHP、Perl、Python等程序
使用Apache結合Tomcat/Resion運行JSP,JAVA等程序
使用Apache做代理、負載均衡、rewrite規則過濾等等。

 

安裝Apachelinux

一、確認主機名、檢查是否已經存在httpd、若是已經存在先卸載(使用rpm -e  --nodeps)web

[root@httpd /]# hostname httpd
[root@httpd /]# rpm -qa httpd
rpm -e --nodeps 加 rpm -qa httpd 查看到的包名
rpm命令-qa參數
-q 查詢的意思
-a 全部軟件包
-e 移除的意思
--nodeps 不作軟件間的依賴檢查
更過參數使用man rpm 或 rpm --help查看

二、下載httpd軟件包apache

[root@localhost /]# wget http://mirror.bit.edu.cn/apache/httpd/httpd-2.2.31.tar.gz
--2016-08-26 20:07:22--  http://mirror.bit.edu.cn/apache/httpd/httpd-2.2.31.tar.gz
Resolving mirror.bit.edu.cn... 202.204.80.77, 2001:da8:204:2001:250:56ff:fea1:22
Connecting to mirror.bit.edu.cn|202.204.80.77|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 7583841 (7.2M) [application/octet-stream]
Saving to: 「httpd-2.2.31.tar.gz」

100%[==================================================>] 7,583,841   1.26M/s   in 5.0s    

2016-08-26 20:07:27 (1.46 MB/s) - 「httpd-2.2.31.tar.gz」 saved [7583841/7583841]

[root@localhost /]# ll httpd-2.2.31.tar.gz
-rw-r--r--. 1 root root 7583841 Aug 26 20:10 httpd-2.2.31.tar.gzbootstrap

三、解壓vim

[root@localhost httpd-2.2.31]# tar zxvf httpd-2.2.31.tar.gz 
[root@localhost /]# cd httpd-2.2.31
[root@localhost httpd-2.2.31]# ls
ABOUT_APACHE  CHANGES        httpd.dsp       libhttpd.dep  NOTICE            server
acinclude.m4  config.layout  httpd.mak       libhttpd.dsp  NWGNUmakefile     srclib
Apache.dsw    configure      httpd.spec      libhttpd.mak  os                support
build         configure.in   include         LICENSE       README            test
BuildAll.dsp  docs           INSTALL         Makefile.in   README.platforms  VERSIONING
BuildBin.dsp  emacs-style    InstallBin.dsp  Makefile.win  README-win32.txt
buildconf     httpd.dep      LAYOUT          modules       ROADMAP
[root@localhost httpd-2.2.31]# ls INSTALL README    #遇到不熟悉的軟件是可參考這兩個文件
INSTALL  README
[root@localhost httpd-2.2.31]# less INSTALL 



  APACHE INSTALLATION OVERVIEW

  Quick Start - Unix
  ------------------

  For complete installation documentation, see [ht]docs/manual/install.html or
  http://httpd.apache.org/docs/2.2/install.html

     $ ./configure --prefix=PREFIX
     $ make
     $ make install
     $ PREFIX/bin/apachectl start

     NOTES: * Replace PREFIX with the filesystem path under which 
              Apache should be installed.  A typical installation
              might use "/usr/local/apache2" for PREFIX (without the
              quotes).

            * If you are a developer who will be linking your code with
              Apache or using a debugger to step through server code,
              ./configure's --with-included-apr option may be advantageous,
              as it removes the possibility of version or compile-option
              mismatches with APR and APR-util code.  (Many OSes now
              include their own version of APR and APR-util.)

            * If you are a developer building Apache directly from
              Subversion, you will need to run ./buildconf before running
              configure. This script bootstraps the build environment and
              requires Python as well as GNU autoconf and libtool. If you
              build Apache from a release tarball, you don't have to run
              buildconf.
[root@localhost httpd-2.2.31]# less README




                          Apache HTTP Server

  What is it?
  -----------

  The Apache HTTP Server is a powerful and flexible HTTP/1.1 compliant
  web server.  Originally designed as a replacement for the NCSA HTTP
  Server, it has grown to be the most popular web server on the
  Internet.  As a project of the Apache Software Foundation, the
  developers aim to collaboratively develop and maintain a robust,
  commercial-grade, standards-based server with freely available
  source code.

  The Latest Version
  ------------------

  Details of the latest version can be found on the Apache HTTP
  server project page under <http://httpd.apache.org/>.

  Documentation
  -------------

四、編譯、安裝瀏覽器

[root@localhost httpd-2.2.31]# yum -y install gcc
[root@localhost httpd-2.2.31]# ./configure --prefix=/usr/local/httpd-2.2.31 --enable-deflate --enable-expires --enable-headers --enable-modules=most --enable-so --with-mpm=worker --enable-rewrite

checking
for zlib location... not found checking whether to enable mod_deflate... configure: error: mod_deflate has been requested but can not be built due to prerequisite failures
解決
缺乏
zlib-devel
[root@localhost httpd-2.2.31]# yum -y install zlib zlib-devel
從新
[root@localhost httpd
-2.2.31]# ./configure --prefix=/usr/local/httpd-2.2.31 --enable-deflate --enable-expires --enable-headers --enable-modules=most --enable-so --with-mpm=worker --enable-rewrite

[root@localhost httpd-2.2.31]# make
[root@localhost httpd-2.2.31]# make install
[root@localhost httpd-2.2.31]# echo $?
0
 

五、優化路徑服務器

[root@localhost httpd-2.2.31]# ln -s /usr/local/httpd-2.2.31/bin/* /usr/local/bin/

六、檢查app

[root@localhost httpd-2.2.31]# /usr/local/bin/apachectl -t
httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
Syntax OK

[root@localhost httpd-2.2.31]# cd /usr/local/httpd-2.2.31/conf/
[root@localhost conf]# pwd
/usr/local/httpd-2.2.31/conf

[root@localhost conf]# vim httpd.conf

ServerName www.httpd.com:80

[root@httpd conf]# /usr/local/bin/apachectl -t
Syntax OK

七、啓動服務

[root@localhost conf]# /usr/local/bin/apachectl start
[root@localhost conf]# lsof -i :80
COMMAND   PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
httpd   14904   root    4u  IPv6  80734      0t0  TCP *:http (LISTEN)
httpd   14906 daemon    4u  IPv6  80734      0t0  TCP *:http (LISTEN)
httpd   14907 daemon    4u  IPv6  80734      0t0  TCP *:http (LISTEN)
httpd   14908 daemon    4u  IPv6  80734      0t0  TCP *:http (LISTEN)
[root@localhost conf]# ps -ef | grep httpd
root     14904     1  0 21:00 ?        00:00:00 /usr/local/httpd-2.2.31/bin/httpd -k start
daemon   14905 14904  0 21:00 ?        00:00:00 /usr/local/httpd-2.2.31/bin/httpd -k start
daemon   14906 14904  0 21:00 ?        00:00:00 /usr/local/httpd-2.2.31/bin/httpd -k start
daemon   14907 14904  0 21:00 ?        00:00:00 /usr/local/httpd-2.2.31/bin/httpd -k start
daemon   14908 14904  0 21:00 ?        00:00:00 /usr/local/httpd-2.2.31/bin/httpd -k start
root     15006  1098  0 21:01 pts/0    00:00:00 grep httpd


[root@localhost conf]# cp /usr/local/httpd-2.2.31/bin/apachectl /etc/init.d/httpd
[root@localhost conf]# vim /etc/init.d/httpd

#chkconfig: 35 85 15

[root@localhost conf]# chkconfig --add httpd

八、測試

檢查防火牆是否關閉

[root@localhost httpd-2.2.31]# /etc/init.d/iptables stop
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Unloading modules:                               [  OK  ]

打開瀏覽器訪問http://192.168.161.131/

若是訪問不了It works 頁面、排查

一、iptables防火牆和selinux是否關閉(在生產環境中容許80端口訪問,而不是關閉防火牆)

[root@localhost httpd-2.2.31]# /etc/init.d/iptables stop
root@localhost httpd-2.2.31]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT

關閉selinux

 

[root@localhost httpd-2.2.31]# setenforce 0  #臨時關閉
[root@localhost httpd-2.2.31]# cat /etc/selinux/config | grep SELINUX=enforcing SELINUX=enforcing [root@localhost httpd-2.2.31]# sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config [root@localhost httpd-2.2.31]# cat /etc/selinux/config | grep SELINUX= # SELINUX= can take one of these three values: SELINUX=disabled

二、檢查httpd端口80

[root@localhost httpd-2.2.31]# netstat -nlt | grep 80
tcp        0      0 :::80                       :::*                        LISTEN      

三、查看是否http進程

[root@localhost httpd-2.2.31]# ps -ef | grep http
root     14904     1  0 21:00 ?        00:00:00 /usr/local/httpd-2.2.31/bin/httpd -k start
daemon   14905 14904  0 21:00 ?        00:00:00 /usr/local/httpd-2.2.31/bin/httpd -k start
daemon   14906 14904  0 21:00 ?        00:00:00 /usr/local/httpd-2.2.31/bin/httpd -k start
daemon   14907 14904  0 21:00 ?        00:00:00 /usr/local/httpd-2.2.31/bin/httpd -k start
daemon   14908 14904  0 21:00 ?        00:00:00 /usr/local/httpd-2.2.31/bin/httpd -k start
daemon   15071 14904  0 21:03 ?        00:00:00 /usr/local/httpd-2.2.31/bin/httpd -k start
root     15147  1098  0 21:28 pts/0    00:00:00 grep http

四、在服務器上wget http://192.168.161.131

[root@localhost httpd-2.2.31]# wget http://192.168.161.131
--2016-08-26 21:28:56--  http://192.168.161.131/
Connecting to 192.168.161.131:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 44 [text/html]
Saving to: 「index.html」

100%[======================================>] 44          --.-K/s   in 0s      

2016-08-26 21:28:56 (7.26 MB/s) - 「index.html」 saved [44/44]

[root@localhost httpd-2.2.31]# curl 192.168.161.131

命令

[root@localhost httpd-2.2.31]# /usr/local/bin/apachectl
Usage: /usr/local/httpd-2.2.31/bin/httpd [-D name] [-d directory] [-f file]
                                         [-C "directive"] [-c "directive"]
                                         [-k start|restart|graceful|graceful-stop|stop]
                                         [-v] [-V] [-h] [-l] [-L] [-t] [-T] [-S]
Options:
  -D name            : define a name for use in <IfDefine name> directives
  -d directory       : specify an alternate initial ServerRoot
  -f file            : specify an alternate ServerConfigFile
  -C "directive"     : process directive before reading config files
  -c "directive"     : process directive after reading config files
  -e level           : show startup errors of level (see LogLevel)
  -E file            : log startup errors to file
  -v                 : show version number
  -V                 : show compile settings
  -h                 : list available command line options (this page)
  -l                 : list compiled in modules
  -L                 : list available configuration directives
  -t -D DUMP_VHOSTS  : show parsed settings (currently only vhost settings)
  -S                 : a synonym for -t -D DUMP_VHOSTS
  -t -D DUMP_MODULES : show all loaded modules 
  -M                 : a synonym for -t -D DUMP_MODULES
  -t                 : run syntax check for config files
  -T                 : start without DocumentRoot(s) check

 apache目錄下的命令和文件介紹

[root@localhost httpd-2.2.31]# pwd
/usr/local/httpd-2.2.31
[root@localhost httpd-2.2.31]# ls
bin    cgi-bin  error   icons    lib   man     modules
build  conf     htdocs  include  logs  manual
[root@localhost httpd-2.2.31]# tree bin
bin
├── ab    #apache HTTP服務器性能測試工具、同類軟件jmeter、loadrunner、webbench等
├── apachectl    #apache啓動命令、apachectl是一個腳本
├── apr-1-config
├── apu-1-config
├── apxs      #apxs是一個apache HTTP服務器編譯和安裝擴展模塊的工具、在進行DSO方式編譯模塊時會用到
├── checkgid
├── dbmmanage
├── envvars
├── envvars-std
├── htcacheclean   #清理磁盤緩衝區的命令、須要編譯時指定相關參數纔可以使用。
├── htdbm
├── htdigest
├── htpasswd    #創建和更新基本認證文件
├── httpd    #httpd爲apache的控制程序、apachectl執行時會調用httpd。
├── httxt2dbm
├── logresolve
└── rotatelogs    #apache自帶的日誌輪詢命令

0 directories, 17 files

[root@localhost httpd-2.2.31]# ll conf/
total 92
drwxr-xr-x. 2 root root 4096 Aug 26 20:48 extra  #額外的apache配置文件目錄、httpd-vhosts.conf默認就在此目錄
-rw-r--r--. 1 root root 13646 Aug 26 20:48 httpd.conf  #apache的主配置文件
-rw-r--r--. 1 root root 12958 Aug 26 20:48 magic
-rw-r--r--. 1 root root 53011 Aug 26 20:48 mime.types
drwxr-xr-x. 3 root root 4096 Aug 26 20:48 original

[root@localhost httpd-2.2.31]# ll htdocs/   #編譯安裝時apache的默認站點目錄
total 4
-rw-r--r--. 1 1000 1000 44 Nov 21 2004 index.html   #默認首頁文件

[root@localhost httpd-2.2.31]# ll logs/  #apache默認日誌文件路徑
total 12
-rw-r--r--. 1 root root 454 Aug 29 16:09 access_log  #apache默認訪問日誌
srwx------ 1 daemon root 0 Aug 29 16:09 cgisock.1216
-rw-r--r--. 1 root root 868 Aug 29 16:09 error_log  #apache錯誤日誌文件
-rw-r--r-- 1 root root 5 Aug 29 16:09 httpd.pid  #httpd的pid文件、http進程啓動後、會把全部進程的ID寫到此文件。

[root@localhost httpd-2.2.31]# ll modules/  #apache模塊目錄
total 12
-rw-r--r--. 1 root root 9194 Aug 26 20:47 httpd.exp

apache優化

[root@localhost httpd-2.2.31]# cd conf/
[root@localhost conf]# vim httpd.conf
<Directory "/usr/local/httpd-2.2.31/htdocs">
Options Indexes FollowSymLinks      #若是沒有首頁的狀況下會展現目錄結構、 建議把Indexes刪除 或改成-Indexes 、若是把目錄展現禁用後、沒有首頁的狀況下會報錯403
相關文章
相關標籤/搜索