How to Install Apache on Ubuntu

Follow these steps to install the Apache HTTP server on a Linux system:

Step 1: Update the Package Index

Run the following command to update your package index:


        sudo apt update
    

Step 2: Install Apache

Use the following command to install Apache:


        sudo apt install apache2
    

Step 3: Start and Enable Apache

Start the Apache service and enable it to start on boot:


        sudo systemctl start apache2
        sudo systemctl enable apache2
    

Step 4: Verify Installation

Open a web browser and navigate to http://your_server_ip. You should see the default Apache welcome page.

Step 5: Adjust Firewall Settings (Optional)

If you are using a firewall, allow HTTP and HTTPS traffic:


        sudo ufw allow 'Apache'
        sudo ufw reload
    

That's it! Apache is now installed and running on your Linux system.

Step 6: Configuration file and use webpage via domain name

If we need to setup our website using via domain name


        sudo vim /etc/apache2/sites-available/your_domain.conf
    

Then add the following lines to the file:


        <VirtualHost *:80>
            ServerAdmin webmaster.com
            ServerName your_domain
            DocumentRoot /var/www/html
            ErrorLog ${APACHE_LOG_DIR}/error.log
            CustomLog ${APACHE_LOG_DIR}/access.log combined
        </VirtualHost>

        <VirtualHost *:443>
            ServerAdmin webmaster.com
            ServerName your_domain
            DocumentRoot /var/www/html
            ErrorLog ${APACHE_LOG_DIR}/error.log
            CustomLog ${APACHE_LOG_DIR}/access.log combined
            SSLEngine on
            SSLCertificateFile /etc/ssl/certs/your_domain.crt
            SSLCertificateKeyFile /etc/ssl/private/your_domain.key
            SSLCertificateChainFile /etc/ssl/certs/your_domain.ca-bundle
        </VirtualHost>

        <directory /var/www/html>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </directory>
    

After all the entry run this commnad this will show you all syntex is right formate:


        httpd -t
    

Step 7: New Entry into Host

Then entry into host file:


        sudo vim /etc/hosts
    

Then add the following lines to the file:


        localIP your_domain
        -> example:  192.168.0.101 webmaster.com
        Note1: localIP is your server IP address & your_domain is your domain name
        Note2: If you are using a domain name, make sure to point it to your server's IP address in your DNS settings.
        Note3: If you set domanin entry so change hostname to your_domain
    

Step 8: Restart Apache

Finally, restart Apache to apply the changes:


        sudo systemctl restart apache2
    

Now you can access your website using the domain name you configured.

Step 9: Check Apache Status

To check the status of Apache, use the following command:


        sudo systemctl status apache2
    

This will show you whether Apache is running and any errors if it is not.

Step 10: Enable SSL (Optional)

If you want to enable SSL for secure connections, you can use the following command:


        sudo a2enmod ssl
        sudo systemctl restart apache2
    

Then create a self-signed SSL certificate:


        sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/apache-selfsigned.key -out /etc/ssl/certs/apache-selfsigned.crt
    

And update your Apache configuration to use the SSL certificate.

Step 11: Test SSL Configuration

To test your SSL configuration, you can use the following command:


        sudo apachectl configtest
    

If there are no errors, restart Apache:


        sudo systemctl restart apache2
    

Now you can access your website using HTTPS.

Step 12: Install PHP (Optional)

If you want to run PHP applications, you can install PHP and the required modules:


        sudo apt install php libapache2-mod-php
        sudo systemctl restart apache2
    

Now you can create a PHP file in the web root directory to test PHP:


        echo <?php phpinfo(); ?> | sudo tee /var/www/html/info.php
    

Then access it in your web browser at http://your_server_ip/info.php.

Step 13: Install MySQL (Optional)

If you want to run a database server, you can install MySQL:


        sudo apt install mysql-server
        sudo mysql_secure_installation
    

Follow the prompts to secure your MySQL installation.

Step 14: Install PHPMyAdmin (Optional)

If you want to manage your MySQL databases through a web interface, you can install PHPMyAdmin:


        sudo apt install phpmyadmin
        sudo phpenmod mbstring
        sudo systemctl restart apache2
    

Follow the prompts to configure PHPMyAdmin.

Step 15: Conclusion

You have successfully installed Apache on your Linux system. You can now host websites and web applications.

Step 16: Additional Resources

For more information, you can refer to the official Apache documentation:

Apache HTTP Server Documentation

For troubleshooting, you can check the Apache error logs:


        sudo tail -f /var/log/apache2/error.log
    

How to Install Apache Tomcat on Redhat

Follow these steps to install the Apache HTTP server on a Redhat/CentOS Linux system:

Step 1: Update the Package Index

Run the following command to update your package index:


        sudo yum update
    

Step 2: Install Apache

Use the following command to install Apache:


        sudo yum install httpd
    

Step 3: Start and Enable Apache

Start the Apache service and enable it to start on boot:


        sudo systemctl start httpd
        sudo systemctl enable httpd
    

Step 4: Verify Installation

Open a web browser and navigate to http://your_server_ip. You should see the default Apache welcome page.

Step 5: Adjust Firewall Settings (Optional)

If you are using a firewall, allow HTTP and HTTPS traffic:


        sudo firewall-cmd --permanent --add-service=http
        sudo firewall-cmd --permanent --add-service=https
        sudo firewall-cmd --reload
    

That's it! Apache is now installed and running on your Redhat/CentOS Linux system.

Step 6: Configuration file and use webpage via domain name

If we need to setup our website using via domain name


        sudo vim /etc/httpd/conf.d/your_domain.conf
    

Then add the following lines to the file:


        <VirtualHost *:80>
            ServerAdmin webmaster.com
            ServerName your_domain
            DocumentRoot /var/www/html
            ErrorLog ${APACHE_LOG_DIR}/error.log
            CustomLog ${APACHE_LOG_DIR}/access.log combined
        </VirtualHost>

        <VirtualHost *:443>
            ServerAdmin webmaster.com
            ServerName your_domain
            DocumentRoot /var/www/html
            ErrorLog ${APACHE_LOG_DIR}/error.log
            CustomLog ${APACHE_LOG_DIR}/access.log combined
            SSLEngine on
            SSLCertificateFile /etc/ssl/certs/your_domain.crt
            SSLCertificateKeyFile /etc/ssl/private/your_domain.key
            SSLCertificateChainFile /etc/ssl/certs/your_domain.ca-bundle
        </VirtualHost>

        <directory /var/www/html>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </directory>
    

After all the entry run this commnad this will show you all syntex is right formate:


        httpd -t
    

Then entry into host file:


        sudo vim /etc/hosts
    

Then add the following lines to the file:


        localIP your_domain
        -> example:  192.168.0.101 webmaster.com
        Note1: localIP is your server IP address & your_domain is your domain name
        Note2: If you are using a domain name, make sure to point it to your server's IP address in your DNS settings.
        Note3: If you set domanin entry so change hostname to your_domain
    

Finally, restart Apache to apply the changes:


        sudo systemctl restart httpd
    

Now you can access your website using the domain name you configured.

To check the status of Apache, use the following command:


        sudo systemctl status httpd
    

This will show you whether Apache is running and any errors if it is not.

To test your SSL configuration, you can use the following command:


        sudo apachectl configtest
    

If there are no errors, restart Apache:


        sudo systemctl restart httpd
    

Now you can access your website using HTTPS.

If you want to run PHP applications, you can install PHP and the required modules:


        sudo yum install php php-mysql
        sudo systemctl restart httpd
    

Now you can create a PHP file in the web root directory to test PHP:


        echo <?php phpinfo(); ?> | sudo tee /var/www/html/info.php
    

Then access it in your web browser at http://your_server_ip/info.php.

If you want to run a database server, you can install MySQL:


        sudo yum install mysql-server
        sudo mysql_secure_installation
    

Follow the prompts to secure your MySQL installation.

If you want to manage your MySQL databases through a web interface, you can install PHPMyAdmin:


        sudo yum install phpmyadmin
        sudo systemctl restart httpd
    

Follow the prompts to configure PHPMyAdmin.

You have successfully installed Apache on your Redhat/CentOS Linux system. You can now host websites and web applications.

For more information, you can refer to the official Apache documentation:

Apache HTTP Server Documentation

For troubleshooting, you can check the Apache error logs:


        sudo tail -f /var/log/httpd/error_log
    

This will show you the latest error messages from Apache.

That's it! You have successfully installed Apache on your Linux system.

How to Change Apache Port Number

To change the default port number of Apache from 80 to another port (e.g., 8080), follow these steps:

Step 1: Open the Apache Configuration File

Open the Apache configuration file using a text editor. The location of the file may vary depending on your Linux distribution:


        sudo vim /etc/httpd/conf/httpd.conf
    

or


        sudo vim /etc/apache2/ports.conf
    

or


        sudo vim /etc/apache2/sites-available/000-default.conf
    

or


        sudo vim /etc/apache2/apache2.conf
    

Step 2: Change the Port Number

Find the line that specifies the port number. It usually looks like this:


        Listen 80
    

Change it to the desired port number, for example:


        Listen 8080
    

Step 3: Update Virtual Host Configuration

If you have virtual host configurations, update the port number there as well. For example:


        <VirtualHost *:80>
            ServerAdmin webmaster@localhost
            DocumentRoot /var/www/html
        </VirtualHost>
    

Change it to:


        <VirtualHost *:8080>
            ServerAdmin webmaster@localhost
            DocumentRoot /var/www/html
        </VirtualHost>
    

Make sure to change the port number in all virtual host configurations.

Step 4: Save and Exit

Save the changes and exit the text editor.

Step 5: Restart Apache

Restart the Apache service to apply the changes:


        sudo systemctl restart apache2
    

or


        sudo systemctl restart httpd
    

or


        sudo service apache2 restart
    

or


        sudo service httpd restart
    

Step 6: Access Apache on the New Port

Open a web browser and navigate to http://your_server_ip:8080 (replace 8080 with your new port number). You should see the Apache welcome page.

If you have a firewall enabled, make sure to allow traffic on the new port:


        sudo ufw allow 8080
    

or


        sudo firewall-cmd --permanent --add-port=8080/tcp
        sudo firewall-cmd --reload
    

or

If there is SElinux enable so add a new port also:


        sudo semanage port -a -t http_port_t -p tcp 8080
    

Now restart Apache service


        sudo systemctl restart apache2
    

or


        sudo systemctl restart httpd
    

That's it! You have successfully changed the Apache port number.