Apache Server Local Machine
When setting up a host for HTML pages or serving a local build of SPAs, I usually rely on IIS or NGINX. However, this time I’ll be using Apache. Are there any significant differences? Not really, for my basic needs. So, why the switch? Experimenting with different servers helps me understand how production servers operate, even in a simplified context.
Downloading Apache
You can either download and build Apache from the source code (which will be covered in another article) or opt for a pre-built binary. I downloaded it from Apache Lounge, but you can find other sources here.
“Setup”
Extract the ZIP file
- Extract the ZIP file.
- Move the
Apache24
directory toC:\
. - Search for “Edit the system environment variables” and open it.
- Click on “Environment Variables”.
- Edit the
Path
variable and addPathToApache24\bin
(in my case,C:\Apache24\bin
).
This setup allows you to call any executable in the bin
folder from anywhere in Windows
Run
- This setup allowed us to call any executable in th bin folder from anywhere in windows.
- Open RUN, Powershell, CMD
- Type
httpd
and the server would start
Serving content
- Open
C:\Apache24\conf\httpd.conf
in any text editor you prefer (e.g., VSCode, Notepad++, etc.). - The
httpd.conf
file contains the server configuration, including enabled modules, listening ports, virtual hosts, and their configurations. For a local development setup, you can keep things simple and consolidate all configurations inhttpd.conf
.
Basic Configuration
- Add a
Listen
port, e.g.,Listen 5000
. Locate the Listen section in the file and add your port there.
- Add a Virtual Host, I would usually add vhosts at the bottom of the file
<VirtualHost *:5000>
ServerName mysite
DocumentRoot "D:\MySite"
<Directory "D:\MySite">
Require all granted
</Directory>
</VirtualHost>
- Open CMD or PowerShell, run
httpd
, and navigate tolocalhost:5000
Serving SPA
- open httpd.conf
- search for LoadModule rewrite_module modules/mod_rewrite.so
- uncomment it
- Add or edit existing VirtualHost and add module rewrite configuration in the Directory Directive
- Add or edit the existing VirtualHost configuration to include rewrite rules in the Directory Directive:
<VirtualHost *:4320>
ServerName spa
DocumentRoot "D:\spa"
<Directory "D:\spa">
Require all granted
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
</Directory>
</VirtualHost>
This basicall tells apache rewrite module that any request should be redirected to index.html.
- restart server
- open
localhost:4320