Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / operating-systems / Linux

A Note on CentOS & Miscellaneous

5.00/5 (2 votes)
6 May 2018CPOL7 min read 6.1K  
How to install CentOS in a VirtualBox
This article provides a guide on installing CentOS in a VirtualBox, installing programs using YUM, setting up Java and Tomcat and installing the Apache Web Server as a reverse proxy.

Introduction

This is a note on CentOS & miscellaneous subjects.

Background

This is a note on the popular Linux distribution CentOS & miscellaneous subjects. In this note, I will cover the following subjects.

  • How to install CentOS operating system in a VirtualBox
  • How to install a program in a CentOS computer. I will talk about the YUM utility.
  • How to setup Java and Tomcat in a CentOS computer. I will talk about how to use the CHKCONFIG utility to automatically start Tomcat when the computer starts.
  • How to install the Apache Web Server in a CentOS computer. I will talk about how to use Apache as a reverse proxy to the Tomcat server. I will also talk about how to enable Apache to automatically start when the computer starts by the SYSTEMD utility.
  • And possibly miscellaneous subjects, such as SSH.

Install CentOS in a VirtualBox

Image 1

The VirtualBox is an easy to use virtual host system. If you are not familiar with it, I will recommend you to take a look at my earlier note Run Linux in Microsoft Windows in a VirtualBox - 2017 where I showed how to create a Linux Mint VM (Virtual Machine). If you want to follow this note to create your own CentOS VM, I have the following recommendations:

  • I will recommend to choose a newer version of VirtualBox. I have noticed that an earlier version of VirtualBox had difficulties on newer versions of OS distributions;
  • I will recommend to choose a newer version of CentOS ISO, such as CentOS 7;
  • I will recommend to choose a fixed size DVI (VirtualBox Disk Image) of 20G or larger for better performance.

The CentOS download page provides multiple ISO options. In this note, I chose the DVD ISO option.

Install CentOS to the VM

The installation of CentOS to the VM is straightforward. If you are not familiar with VirtualBox, you can take a look at my earlier note Run Linux in Microsoft Windows in a VirtualBox - 2017. The CentOS ISO provides multiple installation options. I would recommend the Server with GUI option which is a nice balance between the size and the features.

Image 2

By default, the network connection is disabled after the installation, I would recommend you to enable it.

Image 3

During the installation process, you have the chance to set the password for the root user and create your own user. I would recommend you to do it. I also recommend you to make your user an administrator.

Image 4

VirtualBox Guest Additions

For a better user experience, you need to install the Guest Additions. You can click on Devices -> Insert Guest Additions CD image ... to install the guest additions. Depending on the version of the VirtualBox and your CentOS ISO, the installation may fail with messages similar to the following:

Image 5

According to this link, you can run the following commands to install the Development Tools.

yum update
yum groupinstall "Development Tools"
yum install kernel-devel

You may also need to install the following packages:

yum install epel-release
yum install dkms

After the package installations, you should be able to successfully install the VirtualBox guest additions.

Excessive CPU Utilization by VirtualBox VM

Depending on the version of the VirtualBox and the OS of the host computer, your VM may occasionally freeze. I observed excessive CPU Utilization by the VirtualBox VM in a Mac host computer. In such case, you may change the VM default settings -> System -> Acceleration to un-check the Enable Nested Paging.

Image 6

If it still does not solve the problem, you may set Paravirtualization Interface to NONE.

Package Installations & YUM

Image 7

The Google Chrome is not a default installation on a CentOS computer. You can double-check if Google Chrome is installed by the following command:

which google-chrome

We can take this opportunity to install Google Chrome and get familiar with the package management tool YUM.

Install Google Chrome by YUM

The Google Chrome is not in the default YUM repositories. To install Google Chrome in Centos 7, we need to add the repository in the file /etc/yum.repos.d/google-chrome.repo.

[google-chrome]
name=google-chrome
baseurl=http://dl.google.com/linux/chrome/rpm/stable/$basearch
enabled=1
gpgcheck=1
gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub

After adding the google-chrome.repo file, we can then check if Google Chrome is available for installation.

yum list available | grep google-chrome

If it is in the available list, we can then install it by the following command:

yum install google-chrome-stable

To uninstall a package, we can issue the following command:

yum remove google-chrome-stable

A Summary of YUM Commands

As a summary, the following are the most commonly used YUM commands.

yum clean all
yum update
yum repolist
yum repolist enabled
yum check-update
yum list available
yum list installed
yum install package-name
yum remove package-name
yum-config-manager --add-repo repository_url
yum-config-manager --enable repository…
yum-config-manager --disable repository…

In case you want to disable updating certain packages, you can check out this document.

Tomcat Server & JAVA & CHKCONFIG

Image 8

Tomcat runs on Java. By default CentOS has OpenJDK installed.

Image 9

If you want to use another Java implementation, you can uninstall the OpenJDK. But it is not required. It is very common that a computer has multiple Java run-time environment installed. If you do want to uninstall OpenJDK, you can issue the following command:

yum remove java-1.8.0-openjdk-headless

In this note, I will use the apache-tomcat-8.5.31 and the Oracle jdk-8u172-linux-x64. You can un-zip Java and Tomcat in any directory. In this note, I simply put them into the /apps directory.

Tomcat as a Service by CHKCONFIG

In a server environment, you may want to automatically start Tomcat as a service when the computer starts. We can use the CHKCONFIG utility to achieve it. But first, we need to create a bash file named tomcat in the /etc/init.d directory.

Bash
#!/bin/bash
    
# description: Tomcat start/stop/restart
    
# processname: tomcat
# chkconfig: 234 20 80
    
export JAVA_HOME=/apps/jdk1.8.0_172/
CATALINA_HOME=/apps/apache-tomcat-8.5.31
    
case $1 in
start)
        sh $CATALINA_HOME/bin/startup.sh
        ;;
stop)
        sh $CATALINA_HOME/bin/shutdown.sh
        ;;
restart)
        sh $CATALINA_HOME/bin/shutdown.sh
        sh $CATALINA_HOME/bin/startup.sh
        ;;
esac
    
exit 0

We also need to make the bash file executable.

chmod +x tomcat

To start Tomcat when the computer starts, we can issue the following commands:

chkconfig --add tomcat
chkconfig tomcat on

By default, Tomcat listens to port 8080. If you now restart your VM and go to http://localhost:8080, you will find that Tomcat is started. You can also manually start/stop/restart Tomcat by the following commands:

service tomcat start
service tomcat stop
service tomcat restart

A Summary of CHKCONFIG Commands

As a summary, the following are the most commonly used CHKCONFIG commands:

chkconfig --list
chkconfig --add service-name
chkconfig --del service-name
systemctl daemon-reload
chkconfig tomcat on
chkconfig tomcat off
service tomcat start
service tomcat stop
service tomcat restart 

Apache Web Server & Reverse Proxy & SYSTEMD

Image 10

The Apache web server is another popular web server besides Tomcat. In CentOS, you can install Apache by the following command:

yum install httpd.x86_64

By default, Apache is configured to listen to the port 80. You can enable it to start when the computer starts by the following command:

systemctl enable httpd

You can manually control its state by the apachectl utility.

apachectl start/stop/restart/status

Reverse Proxy to Tomcat

A typical use of the Apache server is to serve as a reverse proxy to the Tomcat server. In order for Apache to serve as the reverse proxy server, we can add a file /etc/httpd/conf.d/default-site.conf.

<VirtualHost *:80>
    ProxyPreserveHost On
    
    ProxyPass / http://0.0.0.0:8080/
    ProxyPassReverse / http://0.0.0.0:8080/
</VirtualHost>

We also need to run the following command to enable the connection between Apache and Tomcat.

setsebool -P httpd_can_network_connect 1

You can find all the setsebool properties related to Apache by the following command:

getsebool -a | grep httpd

If you restart Apache and make sure Tomcat is running and go to http://localhost, you will find it actually loads the default Tomcat web page.

A Summary of SYSTEMD Commands

The state of the Apache (httpd) service is managed by SYSTEMD. As a summary, the following are the most commonly used SYSTEMCTL commands.

systemctl start service-name
systemctl stop service-name
systemctl restart service-name
systemctl reload service-name
systemctl reload-or-restart service-name
systemctl enable service-name
systemctl disable service-name
systemctl status service-name
systemctl is-active service-name
systemctl is-enabled service-name
systemctl is-failed service-name
systemctl list-units
systemctl list-units --all
systemctl list-unit-files
systemctl cat service-name
systemctl list-dependencies service-name
systemctl show service-name
systemctl mask service-name

SSH to Centos VM

SSH is enabled by default on most Linux system. You can check if SSH is running by the following command in CentOS:

systemctl status sshd

Image 11

Since we are running CentOS in a VirtualBox VM, we need to enable the network communication between the host and the VM. Depending on the network configuration of your host computer, you can choose Bridged Adapter in your VirtualBox VM settings and attach the VM network connection to one of your network card. In your VM, you can use ifconfig to find the IP address of your VM.

Image 12

BY default, all the Linux computer has an SSH client. You can start an SSH session to your VM from your host computer by typing in your password.

Image 13

The "CronTab"

The software utility cron is a time-based job scheduler in Unix-like computer operating systems. You can list all the cron jobs that belong to you by the following command.

crontab -l

Image 14

I added a cron job that adds lines to the file A. If you watch the content of the file A, you can find that one line is added every minute.

Image 15

If you wand to add/edit/delete the cron jobs owned by you, you can use the following command.

crontab -e

The scheduling of a cron job is defined by the following table:

Image 16

The "/var/spool/cron" Directory

All the cron jobs are stored in the /var/spool/cron directory. Each user who owns one or more cron jobs has a file in this directory.

Image 17

In my VM, I am the only user who ever created a cron job, so it has only one file named song. A cron job runs with the permission associated to its owner. If you are a sudoer, you can view/create/edit/delete cron jobs on behalf of other owners.

sudo -u song crontab -l
sudo -u song crontab -e

All the cron jobs rely on the crond. You can check the status of the crond by the following command.

service crond status

Image 18

Points of Interest

  • This is a note on CentOS & miscellaneous subjects.
  • I hope you like my posts and I hope this note can help you one way or the other.

History

  • 6th May, 2018: First revision

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)