Apache EssentialsInstall, Configure, MaintainSecond EditionDarren James HarknessApache Essentials: Install, Configure, Maintain Darren James Harkness Port Coquitlam, BC, Canada ISBN-13 (pbk): 978-1-4842-8323-3 ISBN-13 (electronic): 978-1-4842-8324-0 https://doi.org/10.1007/978-1-4842-8324-0 Copyright 2022 by Darren James Harkness This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made.
The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director, Apress Media LLC: Welmoed Spahr Acquisitions Editor: Divya Modi Development Editor: James Markham Coordinating Editor: Divya Modi Copyeditor: Kim Burton Cover designed by eStudioCalamar Cover image designed by Freepik (www.freepik.com) Distributed to the book trade worldwide by Springer Science+Business Media New York, 1 New York Plaza, New York, NY 10004. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail booktranslations@springernature.com; for reprint, paperback, or audio rights, please e-mail bookpermissions@springernature.com.
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales web page at http://www.apress.com/bulk-sales. Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub (https://github.com/Apress). For more detailed information, please visit http://www. apress.com/source-code.
Printed on acid-free paper To my own essentials, Chance and KirstenTable of Contents About the Author xi About the Technical Reviewer xiii Acknowledgments xv Introduction xvii Chapter 1: Getting Started with Apache 1 How Web Servers Work 1 The HyperText Transfer Protocol 2 A (Very) Brief History of Apache 3 Before You Begin 5 Keep Notes 5 Get Familiar with Your Terminal 5 Be Prepared to Read Documentation 6 What to Know Before Installing Apache 6 Using the Right Development Environment 8 Apache vs Other Web Servers 9 Choosing Apache Over Nginx 9 Apache over npm start for Node Development 10 Obtaining Apache 11 Installing Apache on macOS 11 Installing Apache Through Homebrew 12 Running Apache as a Service Through Homebrew 14 Installing Apache on Linux 15 Choosing the Source Installation 17 The Benefit of Binary Installation 19 Installing on Windows 20 v Table of ConTenTs Installing Apache Through XAMPP 21 Installing Apache as a Service in Windows 22 Basic Configuration 23 Where to Put Your Website Files 23 Configuration Directory Structure 24 httpdconf/apache2conf 25 mimetypes 26 Configuring a Default Site 26 Summing It Up 28 Chapter 2: Configuration Essentials 29 How Apaches Configuration Is Structured 29 Where Do I Find Modules? 32 Installing Modules 33 Adding File Types 33 Configuring Modules 34 All About Directives 34 Directive Configuration 34 DocumentRoot /var/www 34 Configuring Your Primary Apache Site 36 Creating the New Directory 37 Updating Your Apache Configuration 38 Using htaccess Files 42 Other Configuration Options You Should Know 43 Rewriting URLs 43 Creating Custom Error Messages 50 Authenticating Users Through Apache 54 Adding New File Types 57 Including External Configuration Files 57 Applying Changes 58 Summing It Up 58 vi Table of ConTenTs Chapter 3: Configuring Virtual Hosts 59 A Quick and Dirty Guide to Domains 60 What Are Virtual Hosts? 61 Creating Development Domains 62 Using the hosts File to Manage Local Domains 62 Using ngrok to Manage Local Domains 63 Setting up a Subdomain Pointing to Your Local Environment 64 Why Develop Locally? 65 Configuring Apaches Virtual Hosts 65 A Sample Apache Configuration 66 Using Separate Configuration Files 73 Putting It All to Work 73 Pointing the Virtual Domains to Your Local Computer 74 Configuring Apache for Multiple Virtual Hosts 74 Summing It Up 78 Chapter 4: Scripting Languages 79 PHP 80 Installing PHP 80 Installing PHP on macOS 80 Installing PHP on Linux 82 Configuring Apache for PHP 82 On macOS 83 On Linux 85 Configuring PHP 86 Updating Default Configuration 86 Increasing the Memory Usage Limit 86 Increasing the Upload File Size 87 Configuring Noisy Error Reporting 87 Increasing Max Execution Time 88 Installing PHP Extensions 89 vii Table of ConTenTs Recommended phpini 90 Testing It Out 96 Configuring Nodejs on Apache 97 Configuring Your Nodejs Application for Apache 98 Installing pm2 98 Configuring the Application 98 Adding Your Application as a Service 99 Configuring Apaches Reverse Proxy 100 Enabling Apaches Proxy Modules 100 On macOS and Windows 100 On Linux 101 Creating a Virtual Host Configuration 101 Testing It Out 102 Configuring React Apps on Apache 103 Summing It Up 104 Chapter 5: Securing Your Setup 105 SSL at a Glance 105 Configuring SSL for Subdomains 106 Installing Certbot 106 Running Certbot 107 Configuring HTTPS for Local Domains 109 Setting up a Local SSL Certificate Authority 109 Installing OpenSSL 109 Storing Your Certificates 110 Configuring Apache for HTTPS 116 Creating a Separate Configuration File 116 Loading the SSL Module 117 Configuring the Apaches SSL Module 117 Summing It Up 123 viii Table of ConTenTs Chapter 6: Log Files 125 Errorlog 125 Configuring the Error Log 125 accesslog 128 Formatting the Access Log 128 Where Can I Find Apaches Log Files? 135 Configuring Apache Logs 135 Log Files for Virtual Hosts 136 Rotating Logs 136 What Do I Do with Log Files? 143 Using Log Analysis Programs 144 Creating Custom Scripts 145 Apache Configuration 147 Tracking Down 404 Errors and Why They Occur 148 Troubleshooting PHP 148 Writing to the Error Log 149 Summing It Up 152 Chapter 7: Sample Apache Configurations 153 Basic Apache Configuration (with PHP) 153 httpdconf 153 Basic HTTPS Configuration 160 httpdconf 160 extra/httpd-sslconf 160 Adding Rewrite Rules to Provide Human-Readable URLs 162 htaccess 162 Setting up Multiple Hosts (One PHP and One Nodejs) 163 httpdconf 163 portfoliolocalconf 164 nodejslocalconf 165 ix Table of ConTenTs Protected Directory 165 httpdconf 167 devmydomaincomconf 167 Epilogue 169 Keep Learning 169 Appendix: HTTP Status Codes 171 HTTP Status Codes 171 1xx Informational Response 171 2xx Success 172 3xx Redirection 174 4xx Client Errors 175 5xx Server Errors 180 Index 183 x
Next page