Skip to content

Instantly share code, notes, and snippets.

@bjayanta
Last active February 11, 2023 14:04
Show Gist options
  • Save bjayanta/3c4340872878c405987207906d5d7c5b to your computer and use it in GitHub Desktop.
Save bjayanta/3c4340872878c405987207906d5d7c5b to your computer and use it in GitHub Desktop.
Apache Web Server on Ubuntu
Step 1: Installing Apache
----------------------------------------------
For updating the local package:
> sudo apt update
> sudo apt-get upgrade
Install the apache2 package:
> sudo apt install apache2
Step 2: Adjusting the Firewall
----------------------------------------------
List the UFW application:
> sudo ufw app list
Receive a list of the applications: Output
Available applications:
Apache
Apache Full
Apache Secure
OpenSSH
As indicated by the output, there are three profiles available for Apache:
Apache: This profile opens only port 80 (normal, unencrypted web traffic)
Apache Full: This profile opens both port 80 (normal, unencrypted web traffic) and port 443 (TLS/SSL encrypted traffic)
Apache Secure: This profile opens only port 443 (TLS/SSL encrypted traffic)
We will only need to allow traffic on port 80:
> sudo ufw allow 'Apache'
Can verify the change:
> sudo ufw status
The output will provide a list of allowed HTTP traffic: Output
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Apache ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Apache (v6) ALLOW Anywhere (v6)
Step 3: Checking your Web Server
----------------------------------------------
Check the service is running:
> sudo systemctl status apache2
Enter it into your browser’s address bar:
http://your_domain
or,
http://your_server_ip
You should see the Apache2 Ubuntu Default Page.
Step 5: Setting Up Virtual Hosts
----------------------------------------------
Create the directory for "your_domain.com" as follows:
> sudo mkdir /var/www/your_domain.com
Next, assign ownership of the directory with the $USER environment variable:
> sudo chown -R $USER:$USER /var/www/your_domain.com
Allow the owner to read, write, and execute the files while granting only read and execute permissions to groups and others:
> sudo chmod -R 775 /var/www/your_domain.com
Next, create a sample index.html page using nano:
> sudo nano /var/www/your_domain/index.html
Add the following sample HTML code:
<html>
<head>
<title>Welcome to Your_domain!</title>
</head>
<body>
<h1>Hello World</h1>
<h3>The your_domain virtual host is working!</h3>
</body>
</html>
In order for Apache to serve this content, it’s necessary to create a virtual host file with the correct directives:
sudo nano /etc/apache2/sites-available/your_domain.com.conf
Paste in the following configuration block, but updated for our new directory and domain name:
<VirtualHost *:80>
ServerAdmin admin@your_domain.com
ServerName your_domain.com
ServerAlias www.your_domain.com
DocumentRoot /var/www/your_domain.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Let’s enable the file with the a2ensite tool:
> sudo a2ensite your_domain.conf
Disable the default site defined in 000-default.conf:
> sudo a2dissite 000-default.conf
Next, let’s test for configuration errors:
> sudo apache2ctl configtest
You should receive the following output:
Syntax OK
Restart Apache to implement your changes:
> sudo systemctl restart apache2
Enter it into your browser’s address bar:
http://your_domain
or,
http://your_server_ip
You should see the HTML Page.
Extra: Managing the Apache Process:
----------------------------------------------
To stop your web server, type:
> sudo systemctl stop apache2
To start the web server when it is stopped, type:
> sudo systemctl start apache2
To stop and then start the service again, type:
> sudo systemctl restart apache2
If you are simply making configuration changes, Apache can often reload without dropping connections.
Use this command:
> sudo systemctl reload apache2
To re-enable the service to start up at boot, type:
> sudo systemctl enable apache2
Necessary softwares:
----------------------------------------------
Install zip extension with below command:
> sudo apt install zip unzip
Thanks.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment