How to Install Apache on CentOS 7

Apache is a free, open source and popular HTTP Server that runs on Unix-like operating systems including Linux and also Windows OS. Since its release 20 years ago, it has been the most popular web server powering several sites on the Internet. It is easy to install and configure to host single or multiple websites on a same Linux or Windows server.

In this article, we will explain how to install, configure and manage Apache HTTP web server on a CentOS 7 or RHEL 7 server using command line.

Prerequisites:

  1. A CentOS 7 Server Minimal Install
  2. A RHEL 7 Server Minimal Install
  3. A CentOS/RHEL 7 system with static IP address

Install Apache Web Server

1. First update the system software packages to the latest version.

# yum -y update

2. Next, install Apache HTTP server from the default software repositories using the YUM package manager as follows.

# yum install httpd
Install Apache on CentOS 7
Install Apache on CentOS 7

Manage Apache HTTP Server on CentOS 7

3. Once Apache web server installed, you can start it first time and enable it to start automatically at system boot.

# systemctl start httpd
# systemctl enable httpd
# systemctl status httpd
Start and Enable Apache
Start and Enable Apache

Configure firewalld to Allow Apache Traffic

4. By default, CentOS 7 built-in firewall is set to block Apache traffic. To allow web traffic on Apache, update the system firewall rules to permit inbound packets on HTTP and HTTPS using the commands below.

# firewall-cmd --zone=public --permanent --add-service=http
# firewall-cmd --zone=public --permanent --add-service=https
# firewall-cmd --reload
Configure firewalld to Allow Apache
Configure firewalld to Allow Apache

Test Apache HTTP Server on CentOS 7

5. Now you can verify Apache server by going to the following URL, a default Apache page will be shown.

http://SERVER_DOMAIN_NAME_OR_IP 
Default Apache Welcome Page
Default Apache Welcome Page

Configure Name-based Virtual Hosts on CentOS 7

This section only useful, if you want to host more than one domain (virtual host) on the same Apache web server. There are many ways to setup a virtual host, but we will explain one of the simplest methods here.

6. First create a vhost.conf file under /etc/httpd/conf.d/ directory to store multiple virtual host configurations.

# vi /etc/httpd/conf.d/vhost.conf

Add the following example virtual host directive template for website mytecmint.com, make sure to change the necessary values for your own domain

NameVirtualHost *:80

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName mytecmint.com
    ServerAlias www.mytecmint.com
    DocumentRoot /var/www/html/mytecmint.com/
    ErrorLog /var/log/httpd/mytecmint.com/error.log
    CustomLog /var/log/httpd/mytecmint.com/access.log combined
</VirtualHost>
Apache Virtual Host Configurations
Apache Virtual Host Configurations

Important: You can add as many as domains to vhost.conf file, just copy the VirtualHost block above and change the values for each domain you add.

7. Now create the directories for mytecmint.com website as referenced in the VirtualHost block above.

# mkdir -p /var/www/html/mytecmint.com    [Document Root - Add Files]
# mkdir -p /var/log/httpd/mytecmint.com   [Log Directory]

8. Create a dummy index.html page under /var/www/html/mytecmint.com.

# echo "Welcome to My TecMint Website" > /var/www/html/mytecmint.com/index.html

9. Finally, restart Apache service for the above changes to take effect.

# systemctl restart httpd.service

10. Now you can visit mytecmint.com to test the index page created above.

Check Virtualhost Website
Check Virtualhost Website

Apache Important Files and Directoires

  • The default server root directory (top level directory containing configuration files): /etc/httpd
  • The main Apache configuration file: /etc/httpd/conf/httpd.conf
  • Additional configurations can be added in: /etc/httpd/conf.d/
  • Apache virtual host configuration file: /etc/httpd/conf.d/vhost.conf
  • Configurations for modules: /etc/httpd/conf.modules.d/
  • Apache default server document root directory (stores web files): /var/www/html

You may also like to read these following Apache web server related articles.

  1. 13 Apache Web Server Security and Hardening Tips
  2. 5 Tips to Boost the Performance of Your Apache Web Server
  3. How to Install Let’s Encrypt SSL Certificate to Secure Apache
  4. Protect Apache Against Brute Force or DDoS Attacks Using Mod_Security and Mod_evasive Modules
  5. How to Password Protect Web Directories in Apache Using .htaccess File
  6. How to Check Which Apache Modules are Enabled/Loaded in Linux
  7. How to Change Apache Server Name to Anything in Server Headers

That’s all! To ask questions or share any additional thoughts, please use the feedback form below. And always remember to stay connected to Tecmint.com.

Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

21 thoughts on “How to Install Apache on CentOS 7”

  1. Hi All,

    I am using CentOS 7 inside VMWare.

    Apache works when I just put in the IP i.e 192.168.0.141

    I want that from my windows machine on which VMWare I have installed, the result of Apache should reflect when I put the IP on the browser.

    What additional thing do I need to do for that?

    Thank you,
    Kunder Akshay,

    Reply
  2. How to set an automatic logout on Apache HTTP Server /balancer-manager page?

    I have configured a balancer-manager in apache with the login page so that the user has to log in to see the balancer-manager page. But once I log in to balancer-manager page using AuthName it stays logged in for an infinite time.

    Is it possible to set a time out or expiry timer for the currently logged in session after in case of no activity so that the user has to login again to see /balancer-manager page?

    balancer-manager configuration in httpd.conf are as below:

        
            SetHandler balancer-manager
            AuthType basic
            AuthName "wakanda"
            AuthUserFile "/etc/httpd/passwd/passwords"
            Require valid-user
        
    
        ProxyPass /balancer-manager ! 
    

    If anyone can help me with an answer that will be great!

    Reply
  3. Sir, I have done exactly what you said in the tutorial, and i got the output also, but when i search using ip/localhost the website appear but when i enter the name of the site ( i created tecmint.com) the page is not displaying and shows error.

    Reply
    • @Srju,

      You need to add tecmint.com with your server IP address on your local machine hosts file from where you accessing the site.

      Reply
      • Thank you,

        Let me check and tell you. if possible pleas share your mail id or anything to contact you personally.

        Reply
  4. My Apache doesn’t run if i do not configure /etc/hosts file. So is it necessary to configure hosts file in etc directory?

    Reply
  5. Hello Sir i am new to Linux and I followed all your instructions but when i try to restart it this is what i get

    [root@pdg ~]# systemctl restart httpd.service
    Job for httpd.service failed because the control process exited with error code. 
    See "systemctl status httpd.service" and "journalctl -xe" for details.
    

    Please your help need this to work out for me thanks.

    Reply
    • @John

      Please, carefully read the output of the command:

      # systemctl restart httpd.service
      OR
      # journalctl -xe
      

      It shows that your configuration has an error.

      Reply
  6. Hello Sir,

    I have done exactly what you did but the httpd refused to restart please see below.

    [root@pdg ~]# systemctl status httpd.service
    ● httpd.service - The Apache HTTP Server
       Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
       Active: failed (Result: exit-code) since Tue 2019-03-19 17:33:00 UTC; 16s ago
         Docs: man:httpd(8)
               man:apachectl(8)
      Process: 90010 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=1/FAILURE)
      Process: 90008 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=exited, status=1/FAILURE)
     Main PID: 90008 (code=exited, status=1/FAILURE)
    
    Mar 19 17:33:00 pdg httpd[90008]: AH00112: Warning: DocumentRoot [/var/www/...st
    Mar 19 17:33:00 pdg httpd[90008]: AH00558: httpd: Could not reliably determ...ge
    Mar 19 17:33:00 pdg httpd[90008]: (2)No such file or directory: AH02291: Ca...:3
    Mar 19 17:33:00 pdg httpd[90008]: AH00014: Configuration check failed
    Mar 19 17:33:00 pdg systemd[1]: httpd.service: main process exited, code=ex...RE
    Mar 19 17:33:00 pdg kill[90010]: kill: cannot find process ""
    Mar 19 17:33:00 pdg systemd[1]: httpd.service: control process exited, code...=1
    Mar 19 17:33:00 pdg systemd[1]: Failed to start The Apache HTTP Server.
    Mar 19 17:33:00 pdg systemd[1]: Unit httpd.service entered failed state.
    Mar 19 17:33:00 pdg systemd[1]: httpd.service failed.
    Hint: Some lines were ellipsized, use -l to show in full.
    

    Please what can i do?

    Reply
  7. Hello Sir, I already did what you done, and the httpd.service is active (running) but when i type the IP address on my browser, i received a 504 Gateway timeout. What should i do??

    Reply
    • @Luke,

      I think it’s due to proxy server settings, try to disable proxy in your browser and try.. If you still see same error, try to check the Apache logs at /var/log/httpd and fix the issue if any..

      Reply
  8. Hello, Dear Sir

    My Apache web server stopped some how. I run service httpd restart on terminal, but it not worked.

    It show:

    [root@localhost ~]# service httpd restart
    

    httpd: Syntax error on line 216 of /usr/local/apps/apache/etc/httpd.conf: Syntax error on line 2 of /usr/local/apps/apache/etc/conf.d/wsgi.conf: Cannot load /us r/local/apps/apache/modules/mod_wsgi.so into server: /usr/local/apps/apache/modu les/mod_wsgi.so: cannot open shared object file: No such file or directory

    Kindly tell me How To fix it.

    Reply
    • @Rajib

      Run the command below:

      # mv /usr/local/apps/apache/etc/conf.d/wsgi.conf /usr/local/apps/apache/etc/conf.d/wsgi.conf.orig
      

      Then restart the Apache service, like this:

      # service httpd restart
      

      If this fails, try to check the mentioned files for any syntax errors on the specified lines.

      Reply

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.