This is a note on CentOS & miscellaneous subjects.
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
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 VirtualBox. I have noticed that an earlier version VirtualBox had difficulties on newer versions of OS distributions;
- I will recommend to choose a newer version 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.
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.
By default the network connection is disabled after the installation, I would recommend you to enable it.
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.
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.
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“.
Package Installations & YUM
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.
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
In case you want to disable updating certain packages, you can check out this document.
Tomcat Server & JAVA & CHKCONFIG
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.
#!/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
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.
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
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-nam
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
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.
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.
Points of Interest
- This is a note on CentOS & miscellaneous subjects;
- I hope you like my postings and I hope this note can help you one way or the other.
First Revision – 5/4/2018.