Installing Apache

Installing Apache
В
Written by Вікторія
Updated 4 days ago

🔧 1. Installing Apache
Step 1: Update the system

sudo apt update && sudo apt upgrade -y

Step 2: Install Apache

sudo apt install apache2 -y

🚀 2. Starting and Checking Apache
Step 1: Check Apache status

sudo systemctl status apache2

Expected result — the line should show active (running).

Step 2: Check via browser
Open a browser and enter your server’s IP address:

http://your_IP

You should see the Apache2 Debian Default Page.

⚙️ 3. Basic Configuration
Step 1: Enable Apache to start on boot

sudo systemctl enable apache2

Step 2: Open port 80 in the firewall (UFW)

sudo ufw allow 'Apache'
sudo ufw reload

🌐 4. Creating Your First Website (Virtual Host)
Step 1: Create the website directory

sudo mkdir -p /var/www/mysite.com/public_html

Step 2: Set ownership

sudo chown -R $USER:$USER /var/www/mysite.com/public_html

Step 3: Create a starting page

nano /var/www/mysite.com/public_html/index.html

Insert:

<!DOCTYPE html>
<html>
<head>
    <title>Welcome to MySite!</title>
</head>
<body>
    <h1>It works!</h1>
</body>
</html>

📁 5. Configuring the Virtual Host
Step 1: Create a configuration file

sudo nano /etc/apache2/sites-available/mysite.com.conf

Insert:

<VirtualHost *:80>
    ServerAdmin webmaster@mysite.com
    ServerName mysite.com
    ServerAlias www.mysite.com
    DocumentRoot /var/www/mysite.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/mysite.com_error.log
    CustomLog ${APACHE_LOG_DIR}/mysite.com_access.log combined
</VirtualHost>

Step 2: Enable the site

sudo a2ensite mysite.com.conf

Step 3: Disable the default site (optional)

sudo a2dissite 000-default.conf

Step 4: Reload Apache

sudo systemctl reload apache2

🧪 6. Testing
Step 1: Add a record to /etc/hosts (local test)

sudo nano /etc/hosts

Add:

127.0.0.1 mysite.com

Step 2: Open in a browser
Enter:

http://mysite.com

Your It works! page should appear.

📦 7. Optional: Installing PHP

sudo apt install php libapache2-mod-php -y

Test PHP:

nano /var/www/mysite.com/public_html/info.php

Insert:

<?php phpinfo(); ?>

In a browser:

http://mysite.com/info.php

✅ Done!

📂 File Structure:

/var/www/mysite.com/public_html/     your site
/etc/apache2/sites-available/        virtual host configs
/etc/apache2/sites-enabled/          enabled sites (symlinks)

🛡 Security & Final Steps:

  • Remove the info.php file after testing:

    rm /var/www/mysite.com/public_html/info.php
    
  • Add HTTPS with Let’s Encrypt — I can provide instructions if needed.

Did this answer your question?