Skip to content

Latest commit

 

History

History
268 lines (180 loc) · 4.2 KB

File metadata and controls

268 lines (180 loc) · 4.2 KB

🚀 Apache Web Server: IP Binding & Hosting Multiple Sites

This guide explains how to:

  • Bind Apache to a specific IP address
  • Run sites on multiple ports
  • Host multiple websites using Virtual Hosts
  • Use firewall and permission commands

🔍 1. Check Where Apache Is Listening

Use netstat to see which IPs and ports Apache is using:

netstat -nltup | grep httpd

This confirms if Apache is running and which ports it's listening on.


⚙️ 2. Bind Apache to a Specific IP Address

Edit the main Apache config file:

vim /etc/httpd/conf/httpd.conf

Make the following changes:

# Listen 80              ← Comment out the default listen
Listen 192.168.29.24:80  ← Add this to bind only to a specific IP

This forces Apache to respond only on IP 192.168.29.24.

Save and exit:

:wq!

🔄 3. Restart Apache

Apply the changes by restarting Apache:

systemctl restart httpd

Then verify:

netstat -nltup | grep httpd

🧪 Browser test:

http://192.168.29.24

🌐 4. Listen on Multiple Ports (80, 81, 82)

To run Apache on additional ports:

vim /etc/httpd/conf/httpd.conf

Add:

Listen 80
Listen 81
Listen 82

This allows Apache to accept traffic on ports 80, 81, and 82.

Save and exit.


🔥 5. Open Custom Ports in Firewall

To allow access to these ports:

firewall-cmd --permanent --add-port=81/tcp
firewall-cmd --permanent --add-port=82/tcp
firewall-cmd --reload

🧪 Test in browser:

http://192.168.29.24:81
http://192.168.29.24:82

🧪 6. Test Apache Configuration

Always check syntax after changes:

httpd -t

If output is Syntax OK, continue.


📁 7. Create Website Directories

Go to Apache's web root and make separate folders for each website:

cd /var/www/html
mkdir site1 site2 site3 site4

Set correct ownership:

chown -Rv apache:apache site1 site2 site3 site4
ls -lh

⬇️ 8. Download & Deploy Website Templates

From your home directory:

cd ~

# Download templates
wget <template-link-1>
wget <template-link-2>
wget <template-link-3>
wget <template-link-4>

# Unzip them
unzip template1.zip
unzip template2.zip
unzip template3.zip
unzip template4.zip

# Copy to site folders
cp -vr template1/* /var/www/html/site1/
cp -vr template2/* /var/www/html/site2/
cp -vr template3/* /var/www/html/site3/
cp -vr template4/* /var/www/html/site4/

Replace <template-link-x> with actual links.


🔒 9. Set Correct Permissions Again

To ensure Apache can access the files:

cd /var/www/html
chown -Rv apache:apache site*

🌍 10. Configure Apache Virtual Hosts

Edit Apache config:

vim /etc/httpd/conf/httpd.conf

At the bottom of the file, remove any extra Listen ports if added earlier.

Then add these virtual host blocks:

<VirtualHost 192.168.29.24:80>
    DocumentRoot /var/www/html/site1
    DirectoryIndex index.html
</VirtualHost>

<VirtualHost 192.168.29.25:80>
    DocumentRoot /var/www/html/site2
    DirectoryIndex index.html
</VirtualHost>

<VirtualHost 192.168.29.26:80>
    DocumentRoot /var/www/html/site3
    DirectoryIndex index.html
</VirtualHost>

<VirtualHost 192.168.29.27:80>
    DocumentRoot /var/www/html/site4
    DirectoryIndex index.html
</VirtualHost>

💾 Save and exit:

:wq!

🔁 11. Restart & Verify

httpd -t       # Check for syntax errors
systemctl restart httpd

🧪 12. Test All Websites

Ensure these IPs are configured on your system:

ip addr add 192.168.29.25/24 dev eth0
ip addr add 192.168.29.26/24 dev eth0
ip addr add 192.168.29.27/24 dev eth0

Open in browser:

http://192.168.29.24
http://192.168.29.25
http://192.168.29.26
http://192.168.29.27

Each IP should open a different site.


📌 Summary Table

Step Description
Listen IP:Port Binds Apache to specific IP and port
VirtualHost Allows multiple websites on one server
firewall-cmd Opens custom ports in the system firewall
chown Assigns correct ownership to Apache
httpd -t Tests Apache configuration for errors