> For the complete documentation index, see [llms.txt](https://riteshs4hu.gitbook.io/infosec-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://riteshs4hu.gitbook.io/infosec-notes/linux-server-administrator/servers-configurations-centos7/apache-web-server.md).

# Apache Web Server

## Web Server

A web server is a computer program that serves content, such as web pages, to users over the internet. When you access a website, your web browser sends a request to the web server hosting the site, and the server responds by sending back the web page and any related content, such as images or videos.

***

## Apache Web Server

Apache is known for its flexibility and reliability, and it is used to host a wide range of websites and applications. It can run on various operating systems, including Linux, Unix, and Windows, and it supports a wide range of programming languages and technologies, including PHP, Perl, and Python.

***

## Configure Apache Web Server

* Install Apache Web Server in Red Hat Linux

```bash
yum install httpd*
```

Files

```
1. /etc/httpd/conf/httpd.conf
2. /var/www/html/
```

Change Owner and Group

```bash
chown -Rv apache:apache /var/www/html/*
```

Allow Ports in iptables

```bash
vim /etc/sysconfig/iptables
```

Add:

```
-A INPUT -p tcp --dport 80 -j ACCEPT
-A INPUT -p tcp --dport 443 -j ACCEPT
```

Save and exit with `:wq!`

Start iptables Service

```bash
systemctl start iptables   # Start iptables service
systemctl restart iptables # Restart iptables service
systemctl enable iptables  # Enable iptables service at boot
```

***

## CSS Not Properly Loading Issues

* Edit Configuration File

```bash
vim /etc/httpd/conf/httpd.conf
```

Search for `AddType text/html .shtml` and add:

```bash
AddType text/css .css
```

***

## Website Bind with IP

* Edit Configuration File

```bash
vim /etc/httpd/conf/httpd.conf
```

Add the following lines:

```
<Virtualhost Enter_Your_IP>
    DocumentRoot /path/to/directory
    DirectoryIndex index.html
</Virtualhost>
```

* Start Service

```bash
systemctl restart httpd.service
```

***

## Website Bind with Port

* Edit Configuration File

```bash
vim /etc/httpd/conf/httpd.conf
```

Add the following lines:

```
Listen Enter_Port_Number
<Virtualhost Enter_Your_IP>
    DocumentRoot /path/to/directory
    DirectoryIndex index.html
</Virtualhost>
```

#### Start Service

```bash
systemctl restart httpd.service
```

***

## Website Bind with Domain Name

* Create a Local Domain

Refer to [DNS Configuration Guide](https://github.com/riteshs4hu/Linux-Servers/blob/main/Red%20HAT/DNS/DNS%20Server%20Configure.md).

Edit Configuration File

```bash
vim /etc/httpd/conf/httpd.conf
```

Add the following lines:

```
<Virtualhost Enter_Your_Domain_IP>
    ServerName zone.name
    DocumentRoot /path/to/directory
    DirectoryIndex index.html
    ServerAlias www.zone.name
</Virtualhost>
```

* Start Service

```bash
systemctl restart httpd.service
```

***

## Website Bind with SSL

* Install SSL

```bash
yum install mod_ssl
```

* Edit Configuration File

```bash
vim /etc/httpd/conf/httpd.conf
```

Add the following lines:

```
<Virtualhost Enter_Your_Domain_IP>
    SSLEngine on
    SSLCertificateFile /etc/pki/tls/certs/localhost.crt
    SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
    DocumentRoot /path/to/directory
    DirectoryIndex index.html
</Virtualhost>
```

* Start Service

```bash
systemctl restart httpd.service
```

***

## Website Call with User's Home Directory

* Enable UserDir

```bash
vim /etc/httpd/conf.d/userdir.conf
```

* Bind Website

```bash
vim /etc/httpd/conf.d/user_name.conf
```

Add the following lines:

```
<Virtualhost Enter_Bind_IP>
    DocumentRoot /home/user_name/public_html
    DirectoryIndex index.html
</Virtualhost>
```

* Grant Execute Permission

```bash
chmod 711 /home/user_name
```

#### Create `public_html` Directory

Switch to the user and run:

```bash
mkdir ~/public_html
```

Place your website files in the `public_html` folder.

***

## WordPress Deployment in Apache

* Add YUM Repositories

```bash
yum install epel-release.noarch
```

```bash
yum install remi-release-7.9-4.el7.remi.noarch
```

```bash
yum install elrepo-release-7.0-6.el7.elrepo.noarch
```

* Install PHP

```bash
yum-config-manager --enable remi-php80
```

```bash
yum install php php-common php-mcrypt php-cli php-opcache php-gd php-curl php-mysqlnd php-xml php-mbstring mysql-devel php-pear php-pecl-http php-pecl-curl php-session
```

#### Install MySQL

Refer to [MySQL Configuration Guide](https://github.com/riteshs4hu/Linux-Servers/tree/main/Red%20HAT/MySQL).

#### Configure phpMyAdmin

Download phpMyAdmin:

```bash
wget https://files.phpmyadmin.net/phpMyAdmin/5.2.0/phpMyAdmin-5.2.0-all-languages.zip
```

Unzip the package:

```bash
tar -xvf phpMyAdmin-5.2.0-all-languages.tar.gz
```

Move to the web directory:

```bash
cp -rv phpMyAdmin-5.2.0-all-languages /var/www/html/phpMyAdmin
```

Bind phpMyAdmin:

```bash
vim /etc/httpd/conf/httpd.conf
```

Add:

```
<Virtualhost Enter_phpMyAdmin_Bind_IP>
    DocumentRoot /var/www/html/phpMyAdmin/
    DirectoryIndex index.php
</Virtualhost>
```

Change folder ownership:

```bash
chown -Rv apache:apache /var/www/html/phpMyAdmin
```

Restart service:

```bash
systemctl restart httpd
```

#### Configure WordPress

Download WordPress:

```bash
wget https://wordpress.org/latest.tar.gz
```

Unzip the package:

```bash
tar -xvf latest.tar.gz
```

Move to the web directory:

```bash
cp -rv wordpress /var/www/html/
```

Bind WordPress:

```bash
vim /etc/httpd/conf/httpd.conf
```

Add:

```
<Virtualhost Enter_WordPress_Bind_IP>
    DocumentRoot /var/www/html/wordpress/
    DirectoryIndex index.php
</Virtualhost>
```

Change folder ownership:

```bash
chown -Rv apache:apache /var/www/html/wordpress/
```

Disable SELinux permissions:

```bash
vim /etc/sysconfig/selinux
```

Change `SELINUX=enforcing` to:

```bash
SELINUX=disable
```

Restart service:

```bash
systemctl restart httpd
```
