반응형

How To Create a systemd Service in Linux (CentOS 7)

Creating a systemd service in Linux is much easier than writing init scripts. Here is an example to create an iperf3 service for systemd!


OS used in this guide: CentOS 7 with EPEL for the iperf3 package


1. First, install iperf3.
$ sudo yum install iperf3

2. Next, create a user iperf which will be used to run the iperf3 service.
$ sudo adduser iperf -s /sbin/nologin

3. Next, create the following file:
/etc/systemd/system/iperf3.service

Put in the following contents and save the file:
[Unit]
Description=iperf3 Service
After=network.target

[Service]
Type=simple
User=iperf
ExecStart=/usr/bin/iperf3 -s
Restart=on-abort


[Install]
WantedBy=multi-user.target


Reload systemd to see the changes
$ sudo systemctl daemon-reload

Start the iperf3 service:
$ sudo systemctl start iperf3

Check the status:
[stmiller@ny ~]$ sudo systemctl status iperf3
iperf3.service - iperf3 Service
   Loaded: loaded (/etc/systemd/system/iperf3.service; disabled)
   Active: active (running) since Mon 2014-12-08 13:43:49 EST; 18s ago
 Main PID: 32657 (iperf3)
   CGroup: /system.slice/iperf3.service
           └─32657 /usr/bin/iperf3 -s

Dec 08 13:43:49 ny.stmiller.org systemd[1]: Started iperf3 Service.
[stmiller@ny ~]$ 

Stop the iperf3 service:
$ sudo systemctl stop iperf3

Start the service at boot:
[stmiller@ny ~]$ sudo systemctl enable iperf3
ln -s '/etc/systemd/system/iperf3.service' '/etc/systemd/system/multi-user.target.wants/iperf3.service'

Disable the service at boot:
$ sudo systemctl disable iperf3


반응형
LIST
반응형

How To Install Apache Tomcat 8 on CentOS 7

PostedJune 19, 2015 123.4kviews JAVA DEPLOYMENT CENTOS

Introduction

Apache Tomcat is a web server and servlet container that is used to serve Java applications. Tomcat is an open source implementation of the Java Servlet and JavaServer Pages technologies, released by the Apache Software Foundation. This tutorial covers the basic installation and some configuration of the latest release of Tomcat 8 on your CentOS 7 server.

Prerequisites

Before you begin with this guide, you should have a separate, non-root user account set up on your server. You can learn how to do this by completing steps 1-3 in the initial server setup for CentOS 7. We will be using the demo user created here for the rest of this tutorial.

Install Java

Tomcat requires that Java is installed on the server, so any Java web application code can be executed. Let's satisfy that requirement by installing OpenJDK 7 with yum.

To install OpenJDK 7 JDK using yum, run this command:

  • sudo yum install java-1.7.0-openjdk-devel

Answer y at the prompt to continue installing OpenJDK 7.

Note that a shortcut to the JAVA_HOME directory, which we will need to configure Tomcat later, can be found at /usr/lib/jvm/jre.

Now that Java is installed, let's create a tomcat user, which will be used to run the Tomcat service.

Create Tomcat User

For security purposes, Tomcat should be run as an unprivileged user (i.e. not root). We will create a new user and group that will run the Tomcat service.

First, create a new tomcat group:

  • sudo groupadd tomcat

Then create a new tomcat user. We'll make this user a member of the tomcat group, with a home directory of /opt/tomcat (where we will install Tomcat), and with a shell of /bin/false (so nobody can log into the account):

  • sudo useradd -M -s /bin/nologin -g tomcat -d /opt/tomcat tomcat

Now that our tomcat user is set up, let's download and install Tomcat.

Install Tomcat

The easiest way to install Tomcat 8 at this time is to download the latest binary release then configure it manually.

Download Tomcat Binary

Find the latest version of Tomcat 8 at the Tomcat 8 Downloads page. At the time of writing, the latest version is 8.0.23. Under the Binary Distributions section, then under the Core list, copy the link to the "tar.gz".

Let's download the latest binary distribution to our home directory.

First, change to your home directory:

  • cd ~

Then use wget and paste in the link to download the Tomcat 8 archive, like this (your mirror link will probably differ from the example):

  • wget http://mirror.sdunix.com/apache/tomcat/tomcat-8/v8.0.23/bin/apache-tomcat-8.0.23.tar.gz

We're going to install Tomcat to the /opt/tomcat directory. Create the directory, then extract the the archive to it with these commands:

  • sudo mkdir /opt/tomcat
  • sudo tar xvf apache-tomcat-8*tar.gz -C /opt/tomcat --strip-components=1

Now we're ready to set up the proper user permissions.

Update Permissions

The tomcat user that we set up needs to have the proper access to the Tomcat installation. We'll set that up now.

Change to the Tomcat installation path:

  • cd /opt/tomcat

Then give the tomcat user write access to the conf directory, and read access to the files in that directory:

  • sudo chgrp -R tomcat conf
  • sudo chmod g+rwx conf
  • sudo chmod g+r conf/*

Then make the tomcat user the owner of the webappsworktemp, and logs directories:

  • sudo chown -R tomcat webapps/ work/ temp/ logs/

Now that the proper permissions are set up, let's set up a Systemd unit file.

Install Systemd Unit File

Because we want to be able to run Tomcat as a service, we will set up a Tomcat Systemd unit file .

Create and open the unit file by running this command:

  • sudo vi /etc/systemd/system/tomcat.service

Paste in the following script. You may also want to modify the memory allocation settings that are specified in CATALINA_OPTS:

/etc/systemd/system/tomcat.service
# Systemd unit file for tomcat
[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target

[Service]
Type=forking

Environment=JAVA_HOME=/usr/lib/jvm/jre
Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'

ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/bin/kill -15 $MAINPID

User=tomcat
Group=tomcat

[Install]
WantedBy=multi-user.target

Save and exit. This script tells the server to run the Tomcat service as the tomcat user, with the settings specified.

Now reload Systemd to load the Tomcat unit file:

  • sudo systemctl daemon-reload

Now you can start the Tomcat service with this systemctl command:

  • sudo systemctl start tomcat

If you want to enable the Tomcat service, so it starts on server boot, run this command:

  • sudo systemctl enable tomcat

Tomcat is not completely set up yet, but you can access the default splash page by going to your domain or IP address followed by :8080 in a web browser:

Open in web browser:
http://server_IP_address:8080

You will see the default Tomcat splash page, in addition to other information. Now we will go deeper into the installation of Tomcat.

Configure Tomcat Web Management Interface

In order to use the manager webapp that comes with Tomcat, we must add a login to our Tomcat server. We will do this by editing the tomcat-users.xml file:

  • sudo vi /opt/tomcat/conf/tomcat-users.xml

This file is filled with comments which describe how to configure the file. You may want to delete all the comments between the following two lines, or you may leave them if you want to reference the examples:

tomcat-users.xml excerpt
<tomcat-users>
...
</tomcat-users>

You will want to add a user who can access the manager-gui and admin-gui (webapps that come with Tomcat). You can do so by defining a user similar to the example below. Be sure to change the username and password to something secure:

tomcat-users.xml — Admin User
<tomcat-users>
    <user username="admin" password="password" roles="manager-gui,admin-gui"/>
</tomcat-users>

Save and quit the tomcat-users.xml file. To put our changes into effect, restart the Tomcat service:

  • sudo systemctl restart tomcat

Access the Web Interface

Now that Tomcat is up and running, let's access the web management interface in a web browser. You can do this by accessing the public IP address of the server, on port 8080:

Open in web browser:
http://server_IP_address:8080

You will see something like the following image:

Tomcat root

As you can see, there are links to the admin webapps that we configured an admin user for.

Let's take a look at the Manager App, accessible via the link or http://server_IP_address:8080/manager/html:

Tomcat Web Application Manager

The Web Application Manager is used to manage your Java applications. You can Start, Stop, Reload, Deploy, and Undeploy here. You can also run some diagnostics on your apps (i.e. find memory leaks). Lastly, information about your server is available at the very bottom of this page.

Now let's take a look at the Host Manager, accessible via the link or http://server_IP_address:8080/host-manager/html/:

Tomcat Virtual Host Manager

From the Virtual Host Manager page, you can add virtual hosts to serve your applications from.

Conclusion

Your installation of Tomcat is complete! Your are now free to deploy your own Java web applications!


반응형
LIST
반응형

폴더 내 모든 압축파일 풀기


폴더 내 모든 압축파일 해제
폴더 내 모든 파일 압축풀기

명령어
find . -name '*.bz2' -exec tar xvf {} \;
실행예시
[root@zetawiki temp]# ll
total 7772
-rw-r--r-- 1 root root  813976 Nov 17 02:50 apr-1.5.0.tar.bz2
-rw-r--r-- 1 root root  695303 Nov 17 02:52 apr-util-1.5.3.tar.bz2
-rw-r--r-- 1 root root 5004719 Nov 23 02:49 httpd-2.4.7.tar.bz2
-rw-r--r-- 1 root root 1440869 May 28 17:16 pcre-8.33.tar.bz2
[root@zetawiki temp]# find . -name '*.bz2' 
./httpd-2.4.7.tar.bz2
./pcre-8.33.tar.bz2
./apr-1.5.0.tar.bz2
./apr-util-1.5.3.tar.bz2
[root@zetawiki temp]# find . -name '*.bz2' -exec tar xvf {} \;
... (생략)
apr-util-1.5.3/ldap/apr_ldap.mak
apr-util-1.5.3/encoding/
apr-util-1.5.3/encoding/apr_base64.c
[root@zetawiki temp]# ll
total 7788
drwxr-xr-x 27     1000  1000    4096 Nov 14 01:14 apr-1.5.0
-rw-r--r--  1 root     root   813976 Nov 17 02:50 apr-1.5.0.tar.bz2
drwxr-xr-x 19     1000  1000    4096 Nov 14 01:41 apr-util-1.5.3
-rw-r--r--  1 root     root   695303 Nov 17 02:52 apr-util-1.5.3.tar.bz2
drwxr-xr-x 11 testuser games    4096 Nov 20 02:36 httpd-2.4.7
-rw-r--r--  1 root     root  5004719 Nov 23 02:49 httpd-2.4.7.tar.bz2
drwxr-xr-x  7     1169  1169    4096 May 28 18:14 pcre-8.33
-rw-r--r--  1 root     root  1440869 May 28 17:16 pcre-8.33.tar.bz2



반응형
LIST
반응형
리눅스 파일 각각 압축
tar 파일 각각 압축

현재 폴더의 모든 txt 파일을 bz2로 압축


명령어
find . -name '*.txt' -exec tar cjvf {}.bz2 {} \;
실행예시
[root@zetawiki test]# ll
total 20
-rw-r--r-- 1 root root 7875 Jun 15 20:45 1.txt
-rw-r--r-- 1 root root 9450 Jun 15 20:45 2.txt
[root@zetawiki test]# find . -name '*.txt' -exec tar cjvf {}.bz2 {} \;
./2.txt
./1.txt
[root@zetawiki test]# ll
total 28
-rw-r--r-- 1 root root 7875 Jun 15 20:45 1.txt
-rw-r--r-- 1 root root 1189 Jun 15 20:45 1.txt.bz2
-rw-r--r-- 1 root root 9450 Jun 15 20:45 2.txt
-rw-r--r-- 1 root root 1193 Jun 15 20:45 2.txt.bz2

각각 압축 후 삭제[편집]

현재 폴더의 모든 txt 파일을 bz2로 압축한 후 txt 파일은 삭제

명령어
find . -name '*.txt' -exec sh -c "tar cjvf {}.bz2 {}; rm -f {};" \;
실행예시
[root@zetawiki test]# ll
total 20
-rw-r--r-- 1 root root 7875 Jun 15 20:45 1.txt
-rw-r--r-- 1 root root 9450 Jun 15 20:45 2.txt
[root@zetawiki test]# find . -name '*.txt' -exec sh -c "tar cjvf {}.bz2 {}; rm -f {};" \;
./2.txt
./1.txt
[root@zetawiki test]# ll
total 8
-rw-r--r-- 1 root root 1189 Jun 15 20:51 1.txt.bz2
-rw-r--r-- 1 root root 1193 Jun 15 20:51 2.txt.bz2


    로그 압축/삭제 샘플

    #!/bin/sh

    path=/home/hong/daemons
    find $path/* -type f -name "*.log-*.log" -mtime 2 -exec sh -c "tar cjvfP {}.bz2 {}; rm -f {};" \;
    find $path/* -type f -name "*.bz2" -mtime 7 -exec rm -f {} \;


    반응형
    LIST

    + Recent posts