Linux安装apache

Linux安装apache

  1. 作者QQ:67065435 QQ群:821635552

  2. 安装前的准备

    yum install \
    vim \
    gcc \
    gcc-c++ \
    wget \
    make \
    libtool \
    automake \
    autoconf \
    expat-devel \
    -y
    
  3. 安装apr、apr-util

    cd /root
    wget http://mirrors.hust.edu.cn/apache/apr/apr-1.6.5.tar.gz
    tar -zxvf apr-1.6.5.tar.gz
    cd apr-1.6.5
    ./configure
    make
    make install
    
    cd /root
    wget http://mirrors.hust.edu.cn/apache/apr/apr-util-1.6.1.tar.gz
    tar -zxvf apr-util-1.6.1.tar.gz
    cd apr-util-1.6.1
    ./configure --with-apr=../apr-1.6.5
    make
    make install
    
  4. 安装PCRE库

    cd /root
    wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.44.tar.gz
    或者(第三方):wget https://note.so1234.top/pcre-8.44.tar.gz
    tar -zxvf pcre-8.44.tar.gz
    cd pcre-8.44
    ./configure
    make
    make install
    
  5. 安装zlib库

    cd /root
    wget http://zlib.net/zlib-1.2.11.tar.gz
    tar -zxvf zlib-1.2.11.tar.gz
    cd zlib-1.2.11
    ./configure
    make
    make install
    
  6. 获取apache源码并安装

    cd /root
    wget http://archive.apache.org/dist/httpd/httpd-2.4.29.tar.gz
    tar -zxvf httpd-2.4.29.tar.gz
    cd httpd-2.4.29
    # 注意:
       # with-apr和with-apr-util要指定安装路径
       # with-z with-ssl with-pcre如安装时指定了路径,这里也要指定路径
    ./configure \
    --prefix=/usr/local/apache \
    --sysconfdir=/usr/local/apache/conf \
    --with-mpm=event \
    --with-apr=../apr-1.6.5/ \
    --with-apr-util=../apr-util-1.6.1 \
    --with-z \
    --with-ssl \
    --with-pcre \
    --enable-so \
    --enable-cgi \
    --enable-ssl \
    --enable-rewrite \
    --enable-modules=most \
    --enable-mpms-shared=all
    make
    make install
    
  7. 修改配置

    # 注意,这里不要使用和其它web服务冲突的端口号
    vim /usr/local/apache/conf/httpd.conf
    
    User www
    Group www
    ServerName localhost:80
    
    ESC
    :wq
    
  8. 修改目录所属用户(也可不修改)

    useradd www
    chown -R www:www /usr/local/apache
    
  9. 启动与关闭测试

    # 检测
    /usr/local/apache/bin/apachectl configtest
    # 启动
    /usr/local/apache/bin/apachectl start
    # 重启
    /usr/local/apache/bin/apachectl restart
    # 关闭
    /usr/local/apache/bin/apachectl stop
    
  10. 创建编辑启动脚本

    cp /usr/local/apache/bin/apachectl /etc/rc.d/init.d/httpd
    
    vim /etc/rc.d/init.d/httpd
    
    #下面两行加在#!/bin/sh后面
    #chkconfig: 2345 10 90
    #description: Apache Web Server
    
    ESC
    :wq
    
  11. Apache加入系统服务

    chkconfig --add httpd
    chkconfig --list httpd
    
  12. 设置开机启动并启动

    systemctl start httpd
    systemctl enable httpd
    
  13. 查看应用状态

    ps aux|grep httpd
    systemctl status httpd
    
  14. 开启防火墙指定端口

    # 不懂防火墙的跳过此步骤
    yum install firewalld
    systemctl enable firewalld
    systemctl start firewalld
    firewall-cmd --zone=public --add-port=80/tcp --permanent
    firewall-cmd --zone=public --add-port=443/tcp --permanent
    firewall-cmd --reload
    
  15. 访问Web服务查看是否正常运行

    # 访问http://xxx.xxx.xxx.xxx
    # 显示: It works! 表示一切正常
    
  16. apache-2.4.x主配置文件httpd.conf(干货)

    # apache安装目录
    ServerRoot "/usr/local/apache"
    # 默认监听端口
    #Listen 0.0.0.0:80
    Listen 80
    # 加载模块
    LoadModule mpm_event_module modules/mod_mpm_event.so
    LoadModule authn_file_module modules/mod_authn_file.so
    LoadModule authn_core_module modules/mod_authn_core.so
    LoadModule authz_host_module modules/mod_authz_host.so
    LoadModule authz_groupfile_module modules/mod_authz_groupfile.so
    LoadModule authz_user_module modules/mod_authz_user.so
    LoadModule authz_core_module modules/mod_authz_core.so
    LoadModule access_compat_module modules/mod_access_compat.so
    LoadModule auth_basic_module modules/mod_auth_basic.so
    LoadModule reqtimeout_module modules/mod_reqtimeout.so
    LoadModule filter_module modules/mod_filter.so
    LoadModule mime_module modules/mod_mime.so
    LoadModule log_config_module modules/mod_log_config.so
    LoadModule env_module modules/mod_env.so
    LoadModule headers_module modules/mod_headers.so
    LoadModule setenvif_module modules/mod_setenvif.so
    LoadModule version_module modules/mod_version.so
    LoadModule unixd_module modules/mod_unixd.so
    LoadModule status_module modules/mod_status.so
    LoadModule autoindex_module modules/mod_autoindex.so
    LoadModule dir_module modules/mod_dir.so
    LoadModule alias_module modules/mod_alias.so
    LoadModule php7_module modules/libphp7.so
    #LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
    #LoadModule mpm_worker_module modules/mod_mpm_worker.so
    #LoadModule authn_dbm_module modules/mod_authn_dbm.so
    #LoadModule authn_anon_module modules/mod_authn_anon.so
    #LoadModule authn_dbd_module modules/mod_authn_dbd.so
    #LoadModule authn_socache_module modules/mod_authn_socache.so
    #LoadModule authz_dbm_module modules/mod_authz_dbm.so
    #LoadModule authz_owner_module modules/mod_authz_owner.so
    #LoadModule authz_dbd_module modules/mod_authz_dbd.so
    #LoadModule auth_form_module modules/mod_auth_form.so
    #LoadModule auth_digest_module modules/mod_auth_digest.so
    #LoadModule allowmethods_module modules/mod_allowmethods.so
    #LoadModule file_cache_module modules/mod_file_cache.so
    #LoadModule cache_module modules/mod_cache.so
    #LoadModule cache_disk_module modules/mod_cache_disk.so
    #LoadModule cache_socache_module modules/mod_cache_socache.so
    #LoadModule socache_shmcb_module modules/mod_socache_shmcb.so
    #LoadModule socache_dbm_module modules/mod_socache_dbm.so
    #LoadModule socache_memcache_module modules/mod_socache_memcache.so
    #LoadModule watchdog_module modules/mod_watchdog.so
    #LoadModule macro_module modules/mod_macro.so
    #LoadModule dbd_module modules/mod_dbd.so
    #LoadModule dumpio_module modules/mod_dumpio.so
    #LoadModule buffer_module modules/mod_buffer.so
    #LoadModule ratelimit_module modules/mod_ratelimit.so
    #LoadModule ext_filter_module modules/mod_ext_filter.so
    #LoadModule request_module modules/mod_request.so
    #LoadModule include_module modules/mod_include.so
    #LoadModule substitute_module modules/mod_substitute.so
    #LoadModule sed_module modules/mod_sed.so
    #LoadModule deflate_module modules/mod_deflate.so
    #LoadModule log_debug_module modules/mod_log_debug.so
    #LoadModule logio_module modules/mod_logio.so
    #LoadModule expires_module modules/mod_expires.so
    #LoadModule unique_id_module modules/mod_unique_id.so
    #LoadModule remoteip_module modules/mod_remoteip.so
    #LoadModule proxy_module modules/mod_proxy.so
    #LoadModule proxy_connect_module modules/mod_proxy_connect.so
    #LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
    #LoadModule proxy_http_module modules/mod_proxy_http.so
    #LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
    #LoadModule proxy_scgi_module modules/mod_proxy_scgi.so
    #LoadModule proxy_fdpass_module modules/mod_proxy_fdpass.so
    #LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so
    #LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
    #LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
    #LoadModule proxy_express_module modules/mod_proxy_express.so
    #LoadModule proxy_hcheck_module modules/mod_proxy_hcheck.so
    #LoadModule session_module modules/mod_session.so
    #LoadModule session_cookie_module modules/mod_session_cookie.so
    #LoadModule session_dbd_module modules/mod_session_dbd.so
    #LoadModule slotmem_shm_module modules/mod_slotmem_shm.so
    #LoadModule ssl_module modules/mod_ssl.so
    #LoadModule lbmethod_byrequests_module modules/mod_lbmethod_byrequests.so
    #LoadModule lbmethod_bytraffic_module modules/mod_lbmethod_bytraffic.so
    #LoadModule lbmethod_bybusyness_module modules/mod_lbmethod_bybusyness.so
    #LoadModule lbmethod_heartbeat_module modules/mod_lbmethod_heartbeat.so
    #LoadModule dav_module modules/mod_dav.so
    #LoadModule info_module modules/mod_info.so
    #LoadModule dav_fs_module modules/mod_dav_fs.so
    #LoadModule vhost_alias_module modules/mod_vhost_alias.so
    #LoadModule negotiation_module modules/mod_negotiation.so
    #LoadModule actions_module modules/mod_actions.so
    #LoadModule speling_module modules/mod_speling.so
    #LoadModule userdir_module modules/mod_userdir.so
    #LoadModule rewrite_module modules/mod_rewrite.so
    # 有些配置在指定模块被成功加载后才能添加
    
    	#LoadModule cgid_module modules/mod_cgid.so
    
    
    	#LoadModule cgi_module modules/mod_cgi.so
    
    
    # 子进程用户
    User www
    Group www
    
    # 主服务配置
    ServerAdmin [email protected]
    ServerName localhost:80
    
       SetHandler application/x-httpd-php
    
    # 这里是全局目录默认规则
    
       AllowOverride none
       Require all denied
    
    # web目录默认位置
    DocumentRoot "/www"
    
       Options Indexes FollowSymLinks
       AllowOverride None
       Require all granted
    
    # 不指定文件名时默认访问路径下的文件
    
       DirectoryIndex index.php index.html index.htm
    
    # 防止 .ht* 文件被从Web上访问
    
       Require all denied
    
    # 错误日志位置
    ErrorLog "logs/error_log"
    # 错误日志级别: 警告
    LogLevel warn
    # 日志格式
    
       LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
       LogFormat "%h %l %u %t \"%r\" %>s %b" common
       
          LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
       
       CustomLog "logs/access_log" common
       #CustomLog "logs/access_log" combined
    
    # alias_module
    
       ScriptAlias /cgi-bin/ "/usr/local/apache/cgi-bin/"
    
    # cgid_module
    
       #Scriptsock cgisock
    
    # cgi-bin目录
    
       AllowOverride None
       Options None
       Require all granted
    
    # 请求头
    
       RequestHeader unset Proxy early
    
    # mime类型对照
    
       TypesConfig /etc/mime.types
       AddType application/x-compress .Z
       AddType application/x-gzip .gz .tgz
       #AddEncoding x-compress .Z
       #AddEncoding x-gzip .gz .tgz
       #AddType application/x-gzip .tgz
       #AddType text/html .shtml
       #AddHandler cgi-script .cgi
       #AddHandler type-map var
       #AddOutputFilter INCLUDES .shtml
    
    #MIMEMagicFile /etc/magic
    #ErrorDocument 500 "The server made a boo boo."
    #ErrorDocument 404 /missing.html
    #ErrorDocument 404 "/cgi-bin/missing_handler.pl"
    #ErrorDocument 402 http://www.example.com/subscription_info.html
    #MaxRanges unlimited
    #EnableMMAP off
    #EnableSendfile on
    
    # Server-pool management (MPM specific)
    #Include /etc/extra/httpd-mpm.conf
    
    # Multi-language error messages
    #Include /etc/extra/httpd-multilang-errordoc.conf
    
    # Fancy directory listings
    #Include /etc/extra/httpd-autoindex.conf
    
    # Language settings
    #Include /etc/extra/httpd-languages.conf
    
    # User home directories
    #Include /etc/extra/httpd-userdir.conf
    
    # Real-time info on requests and configuration
    #Include /etc/extra/httpd-info.conf
    
    # Virtual hosts
    #Include /etc/extra/httpd-vhosts.conf
    
    # Local access to the Apache HTTP Server Manual
    #Include /etc/extra/httpd-manual.conf
    
    # Distributed authoring and versioning (WebDAV)
    #Include /etc/extra/httpd-dav.conf
    
    # Various default settings
    #Include /etc/extra/httpd-default.conf
    
    # proxy_html_module
    
    Include /etc/extra/proxy-html.conf
    
    
    # https相关配置
    # Secure (SSL/TLS) connections
    #Include /etc/extra/httpd-ssl.conf
    
    SSLRandomSeed startup builtin
    SSLRandomSeed connect builtin
    
    
  17. 虚拟主机(Virtual hosts)配置httpd-vhosts.conf

    # 打开'#Include /xxx/httpd-vhosts.conf'的注释才能生效
    # 监听80端口
    
       # web真实目录
       DocumentRoot "/www/test"
       # 服务域名
       ServerName www.test.com
       # 服务别名(可以没有)
       ServerAlias alias.test.com
       # 错误日志位置(mod_log_config)
       ErrorLog "logs/error_log"
       # 访问日志位置以及日志级别(mod_log_config)
       CustomLog "logs/access_log" common
    
    

你可能感兴趣的:(linux,centos,apache)