1. 程式人生 > 實用技巧 >原始碼安裝Apache(httpd)

原始碼安裝Apache(httpd)

【RHEL8】

!!!測試環境我們首關閉防火牆和selinux

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
[root@localhost ~]# setenforce 0  

一、首先安裝Apache安裝環境

[root@localhost ~]# yum install -y make
gcc gcc-c++ expat-devel

二、下載Apache擴充套件包

 //安裝Apache需要下載apr、apr-util、pcre這三個擴充套件包
wget https://mirror.bit.edu.cn/apache/apr/apr-1.6.5.tar.gz

wget https://mirror.bit.edu.cn/apache/apr/apr-util-1.6.1.tar.gz

wget https://netix.dl.sourceforge.net/project/pcre/pcre/8.40/pcre-8.40.tar.gz

三、解壓,預編譯,安裝APR擴充套件包

[root@localhost ~]# tar -zxvf apr-1.6.5.tar.gz
[root@localhost ~]# cd apr-1.6.5/
[root@localhost apr-1.6.5]# ./configure --prefix=/usr/local/apr
...........
configure: creating ./config.status
config.status: creating Makefile
config.status: WARNING:  'Makefile.in' seems to ignore the --datarootdir setting
config.status: creating include/apr.h
config.status: creating build/apr_rules.mk
config.status: creating build/pkg/pkginfo
config.status: creating apr-1-config
config.status: creating apr.pc
config.status: creating test/Makefile
config.status: creating test/internal/Makefile
config.status: creating include/arch/unix/apr_private.h
config.status: executing libtool commands
rm: cannot remove 'libtoolT': No such file or directory
config.status: executing default commands
 //預編譯沒有報錯,說明預編譯成功
[root@localhost apr-1.6.5]# make && make install
............
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
/usr/bin/install -c -m 644 apr.exp /usr/local/apr/lib/apr.exp
/usr/bin/install -c -m 644 apr.pc /usr/local/apr/lib/pkgconfig/apr-1.pc
for f in libtool shlibtool; do \
    if test -f ${f}; then /usr/bin/install -c -m 755 ${f} /usr/local/apr/build-1; fi; \
done
/usr/bin/install -c -m 755 /root/apr-1.6.5/build/mkdir.sh /usr/local/apr/build-1
for f in make_exports.awk make_var_export.awk; do \
    /usr/bin/install -c -m 644 /root/apr-1.6.5/build/${f} /usr/local/apr/build-1; \
done
/usr/bin/install -c -m 644 build/apr_rules.out /usr/local/apr/build-1/apr_rules.mk
/usr/bin/install -c -m 644 /root/apr-1.6.5/build/apr_common.m4 /usr/local/apr/build-1
/usr/bin/install -c -m 644 /root/apr-1.6.5/build/find_apr.m4 /usr/local/apr/build-1
/usr/bin/install -c -m 755 apr-config.out /usr/local/apr/bin/apr-1-config
 //安裝過程沒有報錯,說明安裝成功,然後去/usr/local下面看有沒有apr目錄
[root@localhost apr-1.6.5]# ls /usr/local/
apr  bin  etc  games  include  lib  lib64  libexec  sbin  share  src
[root@localhost apr-1.6.5]# ls /usr/local/apr/
bin  build-1  include  lib
 //出現以上檔案說明安裝成功  

四、解壓、預編譯、安裝arp-util擴充套件包

[root@localhost ~]# tar -zxvf apr-util-1.6.1.tar.gz 
[root@localhost ~]# cd apr-util-1.6.1/
[root@localhost apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util -with-apr=/usr/local/apr 
  //在這說明一下,這裡預編譯必須指明apr檔案路徑,否則會報錯
...........
configure: creating ./config.status
config.status: creating Makefile
config.status: creating export_vars.sh
config.status: creating build/pkg/pkginfo
config.status: creating apr-util.pc
config.status: creating apu-1-config
config.status: creating include/private/apu_select_dbm.h
config.status: creating include/apr_ldap.h
config.status: creating include/apu.h
config.status: creating include/apu_want.h
config.status: creating test/Makefile
config.status: creating include/private/apu_config.h
config.status: executing default commands
 //預編譯過程沒有報錯,說明安裝成功
[root@localhost apr-util-1.6.1]# make && make install
............
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the '-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the 'LD_LIBRARY_PATH' environment variable
     during execution
   - add LIBDIR to the 'LD_RUN_PATH' environment variable
     during linking
   - use the '-Wl,-rpath -Wl,LIBDIR' linker flag
   - have your system administrator add LIBDIR to '/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
/usr/bin/install -c -m 644 aprutil.exp /usr/local/apr-util/lib
/usr/bin/install -c -m 755 apu-config.out /usr/local/apr-util/bin/apu-1-config
 //安裝過程沒有報錯,說明安裝成功
[root@localhost apr-util-1.6.1]# ls /usr/local/
apr  apr-util  bin  etc  games  include  lib  lib64  libexec  sbin  share  src
[root@localhost apr-util-1.6.1]# ls /usr/local/apr-util/
bin  include  lib
 //出現上面所示的目錄,說明安裝成功  

五、解壓、預編譯、安裝PCRE擴充套件包

[root@localhost ~]# tar -zxvf pcre-8.40.tar.gz
[root@localhost ~]# cd pcre-8.40/
[root@localhost pcre-8.40]# ./configure --prefix=/usr/local/pcre
...........
Internal link size .............. : 2
    Nested parentheses limit ........ : 250
    Match limit ..................... : 10000000
    Match limit recursion ........... : MATCH_LIMIT
    Build shared libs ............... : yes
    Build static libs ............... : yes
    Use JIT in pcregrep ............. : no
    Buffer size for pcregrep ........ : 20480
    Link pcregrep with libz ......... : no
    Link pcregrep with libbz2 ....... : no
    Link pcretest with libedit ...... : no
    Link pcretest with libreadline .. : no
    Valgrind support ................ : no
    Code coverage ................... : no
  //預編譯過程沒有報錯,說明安裝成功
[root@localhost pcre-8.40]# make && make install
...........
ln -sf pcre_pattern_to_host_byte_order.3 /usr/local/pcre/share/man/man3/pcre32_pattern_to_host_byte_order.3
ln -sf pcre_refcount.3			 /usr/local/pcre/share/man/man3/pcre32_refcount.3
ln -sf pcre_study.3			 /usr/local/pcre/share/man/man3/pcre32_study.3
ln -sf pcre_utf32_to_host_byte_order.3	 /usr/local/pcre/share/man/man3/pcre32_utf32_to_host_byte_order.3
ln -sf pcre_version.3			 /usr/local/pcre/share/man/man3/pcre32_version.3
make[3]: 離開目錄“/root/pcre-8.40”
make[2]: 離開目錄“/root/pcre-8.40”
make[1]: 離開目錄“/root/pcre-8.40”
 //安裝成功沒有報錯,說明安裝成功
[root@localhost pcre-8.40]# ls /usr/local/
apr  apr-util  bin  etc  games  include  lib  lib64  libexec  pcre  sbin  share  src
[root@localhost pcre-8.40]# ls /usr/local/pcre/
bin  include  lib  share
 //出現上面所示的目錄,說明安裝成功  

六、解壓、預編譯、安裝Apache服務

[root@localhost ~]# tar -zxvf httpd-2.4.46.tar.gz
[root@localhost ~]# cd httpd-2.4.46/
[root@localhost httpd-2.4.46]# ./configure --prefix=/usr/local/httpd \
> --with-apr=/usr/local/apr \
> --with-apr-util=/usr/local/apr-util \
> --with-pcre=/usr/local/pcre \
> --enable-so \            //動態模組載入方式
> --enable-rewrite         //支援rewrite(地址重定向)
.............
config.status: creating build/rules.mk
config.status: creating build/pkg/pkginfo
config.status: creating build/config_vars.sh
config.status: creating include/ap_config_auto.h
config.status: executing default commands
configure: summary of build options:

    Server Version: 2.4.46
    Install prefix: /usr/local/httpd
    C compiler:     gcc
    CFLAGS:          -g -O2 -pthread  
    CPPFLAGS:        -DLINUX -D_REENTRANT -D_GNU_SOURCE  
    LDFLAGS:           
    LIBS:             
    C preprocessor: gcc -E
 //預編譯過程沒有報錯,說明預編譯成功
[root@localhost httpd-2.4.46]# make && make install
 ...........
Installing CGIs
mkdir /usr/local/httpd/cgi-bin
Installing header files
mkdir /usr/local/httpd/include
Installing build system files
mkdir /usr/local/httpd/build
Installing man pages and online manual
mkdir /usr/local/httpd/man
mkdir /usr/local/httpd/man/man1
mkdir /usr/local/httpd/man/man8
mkdir /usr/local/httpd/manual
make[1]: 離開目錄“/root/httpd-2.4.46”
 //安裝過程沒有報錯,說明安裝成功
[root@localhost httpd-2.4.46]# ls /usr/local/
apr  apr-util  bin  etc  games  httpd  include  lib  lib64  libexec  pcre  sbin  share  src
[root@localhost httpd-2.4.46]# ls /usr/local/httpd/
bin  build  cgi-bin  conf  error  htdocs  icons  include  logs  man  manual  modules
 //出現什麼所示目錄,說明安裝成功

七、給httpd啟動指令碼建立軟連結,加入/usr/local/bin/裡面(使用者環境變數)

[root@localhost ~]# ln -s /usr/local/httpd/bin/apachectl /usr/local/bin/apachectl

八、修改httpd.conf的配置檔案

[root@localhost ~]# cd /usr/local/httpd/conf/
[root@localhost conf]# ls
extra  httpd.conf  magic  mime.types  original
[root@localhost conf]# vim httpd.conf 
...........
User apache         //所列出的是需要修改的內容
Group apache
...........
ServerName 192.168.174.128:80    //將servername修改自己的IP加埠號

九、建立apache使用者  

[root@localhost ~]# groupadd -r apache
[root@localhost ~]# useradd -r -g apache apache
[root@localhost ~]# id apache
uid=991(apache) gid=987(apache) 組=987(apache) 

十、啟動httpd服務

[root@localhost ~]# apachectl start
[root@localhost ~]# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1083/sshd           
tcp6       0      0 :::80                   :::*                    LISTEN      59291/httpd         
tcp6       0      0 :::22                   :::*                    LISTEN      1083/sshd           
udp        0      0 0.0.0.0:68              0.0.0.0:*                           1588/dhclient     

十一、測試Apache首頁  

安裝成功!!!