Apache2 in Local Machine
Published: 2023-08-08 03:28:00



> **_08-08-2023_**

**<u>What is Apache2 server ?</u>**

1. Apache is a open-source software .
2. Developed by " Apache Software Foundation ".
3. Its a WEB HOSTING SERVER.

**<u>Install Apache2 over the system :</u>**

```
sudo apt install apache2
systemctl status apache2
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/72e6avv9a3x6otox776f.png)
```
sudo systemctl start apache2
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wlwxht2cmvv8wvi7xctc.png)
This error is due to another server with same functionality is running, 
```
sudo systemctl stop nginx
sudo systemctl status nginx
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bfhvdmxvj8b7n0lot2t4.png)
```
sudo systemctl disable nginx
```
Now start the apach2 server,
```
sudo systemctl start apache2
sudo systemctl status apache2
sudo systemctl enable apache2
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1pst9o45km964g3y4w32.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p81bxkzarzhupdwd6tv1.png)

```
cd /var/www/html/  --> Edit the index.html file
vi /etc/hosts      --> Add the entry of the host
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kei8vyllqr2chss1zzj9.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/og7vf8wrod83gyuaxvx5.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q6s6d0ygzod9j5md7hkm.png)

All these are local machine changes, so this won't be available in INTERNET.

_One Machine - 1 Site
One Machine - Multiple Site ( Virtual )_

**<u>Types of hosting a web server:</u>**

1. Name based eg., site1.name.com & site2.name.com
2. Port based Virtual Hosting 
3. IP based


<u>Now creating Name based Virtual Hosting ,</u>

```
cd /var/www/html
sudo mkdir site1.test.com
sudo mkdir site2.test.com
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mmgwtyhlfkzt5pkudl8f.png)

now go inside these folders ,

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dny61si1ntdmesx40pnm.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tkbgejk9szw7ychnitfy.png)

**<u>Permission Change:</u>**

Current owner of the files & folders,

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t7zxtjjba0u5bx4t55i4.png)

This permission change is required , as these are in **ROOT** permission it may create some issue in future , so always better to change to any user. Here we are using "**www-data**".

**www-data** ---> Who is this default user ? Apache2 user.

```
sudo chown -R www-data:www-data site1.test.com site2.test.com
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/es1rkplrxi0gxredfj27.png)

**<u>Configuration Files :</u>**

```
cd /etc/apache2/
cd /etc/apache2/sites-available
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6tdfzfd5qlkh3vr5sqnl.png)

Create a configuration file inside this folder for the two sites,

```
/etc/apache2/sites-available
sudo vi site1.test.com.conf
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/95y33c11sooz7cs0argk.png)

**<u>Sample Entry Below,</u>**
```
<VirtualHost *:80>
ServerAdmin admin@apple.hashlabs.in
ServerName apple.hashlabs.in
DocumentRoot /var/www/html/apple.hashlabs.in
DirectoryIndex index.html
ErrorLog ${APACHE_LOG_DIR}/apple.hashlabs.in_error.log
CustomLog ${APACHE_LOG_DIR}/apple.hashlabs.in_access.log combined
</VirtualHost>
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vqb6aq5676gamsqwktp0.png)

```

sudo a2ensite site1.test.com
sudo systemctl reload apache2
sudo systemctl restart apache2

sudo a2ensite site2.test.com
sudo systemctl reload apache2
sudo systemctl restart apache2
```
```
sudo vi  /etc/hosts
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w526xvtt7o6t7p83igok.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f283c0k4rbicpi885jiv.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0il2mh51iz1xqkndbw4a.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/875d4j8f98yi6hmml0cu.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tnp3r9bkgyklupjezjkt.png)


**<u>Important & Reference:</u>**

1. https://github.com/alebcay/awesome-shell
2. https://www.nginx.com/ --> WEB HOSTING SERVER
3. SHIFT + CONTROL + P --.> Private Tab
4. **a2ensite** ? a2ensite is a script that configures apache2 to enable the provided site (which contains a block). It accomplishes this by generating symlinks in /etc/apache2/sites-enabled . (or) **a2ensite** is a script that enables the specified site (**which contains a '<'VirtualHost'>' block**) within the apache2 configuration. It does this by creating symlinks within /etc/apache2/sites-enabled. Likewise, a2dissite disables a site by removing those symlinks.
5. digitalocean - Try this site too to buy cloud.
6. https://httpd.apache.org/ - Apache2 website
7. https://www.javatpoint.com/what-is-apache