Thursday, December 28, 2017

Install Apache httpd on CentOS Linux 7

1. Run the following command:

sudo yum install httpd

2. Keep a backup copy after httpd.conf

cp /etc/httpd/conf/httpd.conf ~/httpd.conf.backup.timestamp

3. Check installed version with:

httpd -v
 

Server version: Apache/2.4.6 (CentOS)
Server built:   Oct 19 2017 20:39:16


4.  Right now httpd is installed and not running:

service httpd status
 

Redirecting to /bin/systemctl status  httpd.service
httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: man:httpd(8)
           man:apachectl(8)


5. Create document root folder:

cd /var/www/html && mkdir test.com

6. Add document root to httpd.conf

vi /etc/httpd/conf/httpd.conf 

...
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "/var/www/html/test.com"

...

7. Configure virtual host:

cd /etc/httpd/conf.d

touch vhost.conf

vi vhost.conf
 ....

<VirtualHost *:80>
    ServerName localhost
    ServerAlias www.test.com
    DocumentRoot /var/www/html/test.com/
    ErrorLog logs/error.log
    CustomLog logs/access.log combined
</VirtualHost> 

...

8. Test httpd configuration by running:

service httpd configtest
 

Syntax OK

9. Start apache httpd:

service httpd start

10. Check service is running:

service httpd status

Redirecting to /bin/systemctl status  httpd.service
httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2017-11-29 07:44:34 PST; 7s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 21159 (httpd)
   Status: "Processing requests..."
   CGroup: /system.slice/httpd.service
           ├─21159 /usr/sbin/httpd -DFOREGROUND
           ├─21160 /usr/sbin/httpd -DFOREGROUND
           ├─21161 /usr/sbin/httpd -DFOREGROUND
           ├─21162 /usr/sbin/httpd -DFOREGROUND
           ├─21163 /usr/sbin/httpd -DFOREGROUND
           └─21164 /usr/sbin/httpd -DFOREGROUND


11. Make sure Inbound port 80 is enabled on CentOS 7 Firewall:

firewall-cmd --zone=public --add-port=80/tcp 

success


12. Test from within browser apache is accessible:



Tuesday, December 26, 2017

Create embedded Jetty Java application

Here are the exact steps:

1. Download and install Eclipse:
https://www.eclipse.org/downloads/ 

2. Crete New Maven Project:



3. Create a simple project (skip archetype selection)

4. Configure project with following values:


 

5. In the end, your project structure shall look like this:


 

6. Edit pom.xml by adding latest Jetty dependencies. You can find it from here:

https://www.eclipse.org/jetty/download.htmlhttps://www.eclipse.org/jetty/download.html


pom.xml shall look like this:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.dtechnotes</groupId>
  <artifactId>embedded-jetty-java</artifactId>
  <version>1.0.0</version>
  <dependencies>
 <!--Jetty  dependencies-->
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-server</artifactId>
            <version>9.4.8.v20171121</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-servlet</artifactId>
            <version>9.4.8.v20171121</version>
        </dependency>
        <!--Jetty  dependencies end-->
    </dependencies>
</project>


7. Add 2 new packages as per below:



8.  Add 2 new Java classes for each of the above packages:
 9. JettyJavaMain.java

package com.dtechnotes.jettyjava;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;

import com.dtechnotes.jettyjava.servlet.TestServlet;

public class JettyJavaMain {
    public static void main(String[] args) throws Exception {

        Server server = new Server(8082);
        ServletContextHandler handler = new ServletContextHandler(server, "/dtech");
        handler.addServlet(TestServlet.class, "/");
        server.start();

    }
}


10. TestServlet.java

package com.dtechnotes.jettyjava.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.http.HttpStatus;

public class TestServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        resp.setStatus(HttpStatus.OK_200);
        resp.getWriter().println("<b>This is my first Jetty Test</b>");
    }
}


11.  You can run directly JettyJavaMain.java class and get result by navigating to:
http://localhost:8082/dtech/



12. Additionaly, you can export to single jar file using the Eclipse export feature:
File -> Export -> Runnable Jar File

13.  And run the jar file with the same results as step 11 above:







 
 

Sunday, December 3, 2017

How to fix following error: Failed to execute goal org.apache.rat:apache-rat-plugin on project org.apache.sling.samples.path-based.rtp: Too many files with unapproved license:

1. Run Maven build command:

mvn clean install -s /home/user/.m2/settings.xml

2. Getting the following Build Failure message:

[ERROR] Failed to execute goal org.apache.rat:apache-rat-plugin:0.11:check (default) on project org.apache.sling.samples.path-based.rtp: Too many files with unapproved license: 1677 See RAT report in: /home/scaunasu/path-based-rtp/target/rat.txt -> [Help 1]

3. To solve the above, run Maven command with the following additiona parameter:

mvn clean install -s /home/user/.m2/settings.xml  -Drat.numUnapprovedLicenses=2000

Thursday, November 30, 2017

Java process consuming more memory than Xmx heap allocated


1. Behavior is the following:

Java has the following Xmx parameter set up for max heap size:
-Xmx24576m

2. Despite this a top command shows consumption bigger than that:
 

The above shows Resident Size (memory consumed by Java process) to be around 0.041 Terra = aprox 41 GB . This is far more than the allocated heap at step 1 - 24GB.

Note: Do not take into consideration the Virtual column, this memory is not the same with the one used by Java process.

3. If we do another test using command line, we get the same results:

ps aux  | awk '{print $6/1024 " MB\t\t" $11 $12}'  | sort -rn | head -n 1

It shows:
 

4. Our matter is confirmed, something else is getting memory space being occupied.
Additional pmap can be used to confirm our doubts - just to get memory consumed by anonymous block allocations:

pmap -x 4366 | grep anon | awk '{s+=$3} END {s=s/1048576 "MB";print s}'

5. One possible resolution to this (there are many solutions found over the Internet) is to tune glibc. It is known on Linux to have those kind of memory leaks.

Especially the parameter MALLOC_ARENA_MAX needs to be tuned, and a value of 2 is highly recommended:
export MALLOC_ARENA_MAX=2

More details about this variable here - a very good article to read:
https://siddhesh.in/posts/malloc-per-thread-arenas-in-glibc.html

 



Monday, November 27, 2017

Install Apache Maven

1. This assumes you already have Java 7+ already up and running:

java -version

 

2. Download latest Apache Maven package from https://maven.apache.org/download.cgi

and move it to /usr/local folder.

Alternatively you can download directly on your linux instance using wget:

wget http://www-eu.apache.org/dist/maven/maven-3/3.5.2/binaries/apache-maven-3.5.2-bin.tar.gz

3. Untar the zip:

sudo tar xzf apache-maven-3.5.2-bin.tar.gz

4. It is by default extracted to an apache-maven-3.5.2 folder. For ease, can create a symbolic link for maven:

ln -s apache-maven-3.5.2  maven

5. Get proper path in place:

export PATH=/usr/local/maven/bin:${PATH} 

6. Test by checking maven installed version:

mvn -version



7. To make it work, it also needs a settings.xml file. This file needs to be under /home/user/.m2 folder and can have following sections:

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
                          https://maven.apache.org/xsd/settings-1.0.0.xsd">


<localRepository>/user/local/.m2/repository</localRepository><interactiveMode>true</interactiveMode>
<usePluginRegistry>false</usePluginRegistry>
<offline>false</offline>
<pluginGroups>
</pluginGroups>
<servers/>

<mirrors/>
<proxies/>
<profiles/>
<activeProfiles/>
</settings>

8. If you have an already existing project, you can test from within project folder the following command - it shall build succesfully and download any dependency to the repository:

mvn clean install -s /home/user/.m2/settings.xml