TechnologyLinux

How to run multiple websites in one Redhat/centos server in 2023.

Step-1

Install all prerequisites.

sudo yum update

updates all installed packages and their dependencies on a Linux system using the Yum package manager.

i.Install PHP latest version

Setup EPEL & Remi Repository:-

sudo dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm 
sudo dnf install -y https://rpms.remirepo.net/enterprise/remi-release-9.rpm

Packages for PHP 8.2, 8.1, 8.0, and 7.4 are available in the remi repository. The following methods can be used to activate the desired repository and install PHP packages:-

Once the repository installation is complete, use the following command to display all the PHP module streams that are accessible:

sudo dnf module list php

The image above demonstrates that PHP 8.1 has a live repository. Run the command below to determine the PHP version needed for your application.

For instance, I must install PHP 8.2 before selecting php:remi-8.2. You can choose a different version based on your needs:

sudo dnf module enable php:remi-8.2 

Finally, run the following command to install PHP on your CentOS 9/redhat system.

sudo dnf install php php-cli php-common 

You can verify the installed PHP version using:

php -v 
php -m
Output:- PHP 8.2.2 (cli) (built: Jan 31 2023 13:31:55) (NTS gcc x86_64) 

Copyright (c) The PHP Group Zend Engine v4.2.2, Copyright (c) Zend Technologieswith Zend OPcache v8.2.2, Copyright (c), by Zend Technologies

Installing PHP Modules:

PHP modules are libraries that extend the functionality of PHP. They are used to add specific features to PHP, such as database connectivity or image manipulation. To install additional PHP modules, use the following command:

sudo dnf install php-{extension-name}

Replace {extension-name} with the name of the module you want to install. For example, to install the mysql module, run the following command:

sudo dnf install php-mysql

PHP installtion is done.

ii.Install Apache/httpd server

yum install httpd
systemctl start httpd
systemctl enable httpd
systemctl status httpd

Apache/httpd server installtion is done.

iii.Install mariadb server

yum install mariadb-server
systemctl start mariadb
systemctl enable mariadb
systemctl status mariadb
systemctl restart httpd


Step-2

Open HTTP and optionally HTTPS port 80 and 443 on your firewall. If the firewall service is not installed try this command:

yum install firewalld
sudo systemctl enable firewalld
sudo reboot
sudo firewall-cmd --state

After rebooting.

firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload
sudo firewall-cmd --list-all
systemctl restart httpd

Step-3

(Optional) Secure your MariaDB installation and set root password:

mysql_secure_installation

Step-4

Setting Up Virtual Hosts (To host website)

Apache on CentOS and redhat has one server block enabled by default that is configured to serve documents from the /var/www/html directory. While this works well for a single site, it can become unwieldy if you are hosting multiple sites. Instead of modifying /var/www/html, you will create a directory structure within /var/www for the your_domain site, leaving /var/www/html in place as the default directory to be served if a client request doesn’t match any other sites.

Create the html directory for your_domain as follows, using the -p flag to create any necessary parent directories:

sudo mkdir -p /var/www/your_domain/html

Create an additional directory to store log files for the site:

sudo mkdir -p /var/www/your_domain/log

Next, assign ownership of the html directory with the $USER environmental variable:

sudo chown -R $USER:$USER /var/www/your_domain/html

Next, create a sample index.html page using vim/nano/vi or your favorite editor:

sudo vi /var/www/your_domain/html/index.html

Press i to switch to INSERT mode and add the following sample HTML to the file:

/var/www/your_domain/html/index.html(Path)

<html>
  <head>
    <title>Welcome to your website!</title>
  </head>
  <body>
    <h1>Success! The your_domain virtual host is working!</h1>
  </body>
</html>

Save and close the file by pressing ESC, typing :wq, and pressing ENTER.

With your site directory and sample index file in place, you are almost ready to create the virtual host files. Virtual host files specify the configuration of your separate sites and tell the Apache web server how to respond to various domain requests.

Before you create your virtual hosts, you will need to create a sites-available directory to store them in. You will also create the sites-enabled directory that tells Apache that a virtual host is ready to serve to visitors. The sites-enabled directory will hold symbolic links to virtual hosts that we want to publish. Create both directories with the following command:

sudo mkdir /etc/httpd/sites-available /etc/httpd/sites-enabled

Next, you will tell Apache to look for virtual hosts in the sites-enabled directory. To accomplish this, edit Apache’s main configuration file and add a line declaring an optional directory for additional configuration files:

sudo vi /etc/httpd/conf/httpd.conf

Add this line to the end of the file:

IncludeOptional sites-enabled/*.conf

Save and close the file when you are done adding that line. Now that you have your virtual host directories in place, you will create your virtual host file.

Start by creating a new file in the sites-available directory:

sudo vi /etc/httpd/sites-available/your_domain.conf

Add in the following configuration block, and change the your_domain domain to your domain name:

/etc/httpd/sites-available/your_domain.conf(path)

<VirtualHost *:80>
    ServerName www.your_domain
    ServerAlias your_domain
    DocumentRoot /var/www/your_domain/html
    ErrorLog /var/www/your_domain/log/error.log
    CustomLog /var/www/your_domain/log/requests.log combined
</VirtualHost>

This will tell Apache where to find the root directly that holds the publicly accessible web documents. It also tells Apache where to store error and request logs for this particular site.

Save and close the file when you are finished.

Now that you have created the virtual host files, you will enable them so that Apache knows to serve them to visitors. To do this, create a symbolic link for each virtual host in the sites-enabled directory:

sudo ln -s /etc/httpd/sites-available/your_domain.conf /etc/httpd/sites-enabled/your_domain.conf

Your virtual host is now configured and ready to serve content. Before restarting the Apache service, let’s make sure that SELinux has the correct policies in place for your virtual hosts.

Quick Navigation
Show More

Leave a Reply

Your email address will not be published. Required fields are marked *

14 − thirteen =

Back to top button

Adblock Detected

Please consider supporting us by disabling your ad blocker