How to install Tomcat 8.5 on remote CentOS 7 server

In the previous post I told you how I installed Java on my production server. Now it’s time to install Tomcat. There are different ways you can do it. I must warn you that the way I did it might not be the best way, because I skipped all the security related steps. So, what you are about to read is not a definitive guide to Tomcat installation (if you are looking for that kind of guide check out this article). But I think this post can be a good starting point for a complete noob who doesn’t what to overwhelm himself with too much detail right from the start. Here we’ll just focus on installing Tomcat and making it work somehow.

My first attempt

At first I tried to just

sudo yum install tomcat

But this command installed Tomcat 7, while I wanted to install the latest stable version, which is currently Tomcat 8.5.9. So I had to remove Tomcat 7 with

sudo yum remove tomcat

(Note that I am logged to my remote server as sudo user by the name of karen)

By the way, if you install Tomcat 7 this way and then don’t see the default Tomcat page in browser, you need to install some additional stuff:

sudo yum install tomcat-admin-webapps.noarch tomcat-docs-webapp.noarch tomcat-javadoc.noarch tomcat-systemv.noarch tomcat-webapps.noarch

Also don’t forget to configure your ports. More on this later.

Before making my second attempt to install Tomcat I found a video tutorial, and went through it step by step. We will go through this tutorial later in this post, but before installing Tomcat we will need to setup FTP server. I’m sure there are other ways to transmit installation file to the remote server, but we will still need the FTP server later on for uploading our apps. So why not start with FTP server?

Setting up FTP server

I used this tutorial and I installed vsftpd like this:   

sudo yum install vsftpd

sudo yum install ftp

I’m not sure if the second line was necessary, because I didn’t quite understand the tutorial. Did I need to install ftp on my local machine or was it needed on the remote server? I just thought it wouldn’t hurt to install it also, so that’s what I did.

On my local computer I have FileZilla, which I installed a long time ago, so I used it to connect to the server.

At this point you’re not going to be able to connect to the FTP server. You need to open the port 21 in the server’s firewall:

sudo firewall-cmd –permanent –add-port=21/tcp

sudo firewall-cmd –reload

Now you can open FileZilla, specify url of your server, your name and password, and try to connect. It is going to connect, but it will throw an error soon after. The reason is that the actual data is transferred through a range of ports which are all closed in your firewall. The port 21 is just for the commands. For all the data you need to open some other several hundred ports. Also you need to tell the FTP server which ports it should use.

To understand why you need to go through all these steps you can read these articles:

http://www.mdjnet.dk/ftp.html

http://www.mdjnet.dk/router.html

http://serverfault.com/questions/421161/how-to-configure-vsftpd-to-work-with-passive-mode

http://slacksite.com/other/ftp.html

Lets first configure the FTP server. Open the configuration file:

sudo vi /etc/vsftpd/vsftpd.conf

Now paste these three lines:

pasv_enable=Yes

pasv_max_port=10900

pasv_min_port=10100

What we did here is we enabled the passive mode (read about it in the articles above) and set the range of ports that FTP server will use for transmitting the data. We need at least several hundred ports.

After saving and exiting the file don’t forget to restart your FTP server:

sudo /bin/systemctl restart  vsftpd.service

Now we need to open the port range in the server firewall:

sudo firewall-cmd –permanent –zone=public –add-port=10100-10900/tcp

sudo firewall-cmd –reload

Finally installing Tomcat

This is how I did it. I went to the official site of Tomcat and downloaded this file: apache-tomcat-8.5.9.zip

Then, using FileZilla, I uploaded it to my server. Now the file was in my home folder on the server.

I logged in to the server and moved the file to the new folder:

sudo mkdir /opt/tomcat

sudo mv apache-tomcat-8.5.9.zip /opt/tomcat

Then I checked the md5 sum:

cd /opt/tomcat

md5sum apache-tomcat-8.5.9.zip

Then I unzipped the file:

sudo unzip apache-tomcat-8.5.9.zip

Actually I couldn’t unzip it at first, because I didn’t have the necessary program, so I installed it:

sudo yum install zip unzip -y

The reason I used specifically zip file instead of tag.gz is that that’s what was used in the video tutorial. I decided to not change anything. I think tag.gz would do just as well. 

Now I set right permissions:

cd apache-tomcat-8.5.9/bin

sudo chmod 700 *.sh

It means I can execute *.sh files.

And the last step is to create a couple of links:

sudo ln -s /opt/tomcat/apache-tomcat-8.5.9/bin/startup.sh /usr/bin/tomcatup

sudo ln -s /opt/tomcat/apache-tomcat-8.5.9/bin/shutdown.sh /usr/bin/tomcatdown

Now I can start Tomcat like this:

sudo tomcatup

And stop it like this:

sudo tomcatdown

If you followed all the steps and started Tomcat, now you can point your browser to your_server_ip:8080. You should see the default page of Tomcat:

Facebooktwitterredditpinterestlinkedinmail

How to deploy Java application to production server

After playing around with server side development (see my posts about it: post1, post2 and post3) I feel like I could develop back-end if I really needed to. Of course it would take a lot of time and I’d be googling a lot, but at least back-end development is not a mystery for me anymore. But there is one piece of knowledge regarding back-end development that I lack: I don’t know how to deploy my application to the production server.

So, I decided to address this issue, and rented a VDS (virtual dedicated server). I’m going to use it for my experiments and eventually for hosting back-ends of my future apps.

My goals are:

  1. Install Tomcat
  2. Install MySQL
  3. Deploy some Java application to the server.
  4. Learn how to use the server for sending Apple Push Notifications.

Here is what I have done already. I started reading about how to install Tomcat and found out that I should install Java first. And even before that I needed to create a sudo user.

Lets go step by step and see what I did.

Creating sudo user

After the purchase of VDS my hosting provider sent me the IP address of my server and the password of root user.  I logged in to the server via SSH.

local$ ssh root@server_ip_address

It is a good practice to not connect to the server as a root user. So I created another user.

# adduser karen

And set his password

# passwd karen

Then I added this new user to the wheel group to make him a sudo user.

# usermod -aG wheel karen

Then I switched to the new user account

# su – karen

Then I verified that I could use sudo

karen$ sudo ls -la /root

This command just lists the contents of /root folder which is normally only accessible by the root user.

I used this article as a reference.

Installing Java

Installing Java was easy. I did it with this one command:

sudo yum install java-1.8.0-openjdk

I verified that Java installed correctly with this command:

java -version

This article helped me.

To be continued.

In the next post I will tell you how I installed Tomcat on my production server.

Facebooktwitterredditpinterestlinkedinmail

How to detect orientation change (Swift 3.0)

Let’s study this example: every time you rotate your iPad, you need to recalculate cell heights in your table view. You only need to do this on iPad, because iPhone version of the app is going to have only portrait orientation.

So, we need to detect orientation change. Once orientation change is detected we can call tableView.reloadData() method. Let’s code.

First of all, we need to add an observer to our view controller. Add this line in viewDidLoad() method:

Also, implement rotated() method.

You only need to detect orientation change between two orientations: Landscape and Portrait. But there are a couple more device orientations. We also have FaceUp and FaceDown orientations. We don’t want to reload data in table view every time we go from, say, Landscape to FaceUp, because in that case the interface orientation shouldn’t change.

Also, if we change orientations like this:

Portrait – FaceUp – Portrait

we don’t want to reload table view.  So, when we go from FaceUp to Portrait again, we need to know that the last orientation that counts was Portrait also. To store the last orientation we need a variable.

Create this variable in your view controller:

Instantiate it in viewDidLoad():

Now we can rewrite rotated() method:

So, that’s how it is done. Or at least how I did it in my Stream Journal app. I wonder if it is the best possible way to detect change between Portrait and Landscape. What if, instead of device orientation change, we could detect user interface orientation change directly? That would save us all the additional coding. If you know of such a way, please, comment.   

Here is something free for you

Don’t close this page yet. Before you go, maybe you’d like to check out my app. If you want to control your life and be focused on your goals this app is definitely for you. You create your own list of questions and then answer those questions every day. And recently I added the ability to create multiple lists of questions.

  Check it out on App Store

Facebooktwitterredditpinterestlinkedinmail

How to keep your ass healthy as a developer

I once read an article about some recent research on preventing negative effects of sitting. I don’t have the link now but there are a lot of articles on this topic, and you can Google them if you are interested. So, what was it all about?

I’m sure, you know that prolonged sitting has negative effects on your health. Sitting is a new smoking, as they say nowadays. Different studies have shown that sitting for extended periods of time increases risks of a number of health issues, among which are heart disease, diabetes and cancer, to say nothing of some ‘lighter’ stuff like hemorrhoids and prostatitis.

As a developer, and previously a civil engineer working at the office, I sit a lot. So, the health risks associated with sitting have always concerned me. That’s why the article I mentioned above interested me in the first place.

What that article suggested, based on some recent study, is that to prevent most of the negative effects of sitting all you have to do is to walk for 2 minutes every hour. It didn’t explain why it was so, but for me it made a lot of sense. And here is why.

It is easy to imagine that when you sit, some of your internal organs are compressed and blood doesn’t flow normally in some parts of your body. That might cause no harm if you sit for just a short period of time. But if you sit for prolonged periods of time it is once again easy to imagine that some negative processes start to occur in the compressed part of your body. Like some cells dying off. Or whatever. I’m sure it’s more complicated than that.

But what if you don’t wait till something bad starts to happen in your body and start walking after one hour of sitting? Then you blood starts to flow normally, your internal organs relax. And maybe 2 minutes of walking are really just enough to prevent some of the negative processes in your body from happening. Then you can sit for another hour and repeat.

On the other hand, if you tried to offset the negative effects of sitting by just doing some exercises after work hours, it wouldn’t help. Because by that time you would have already done the damage to your body from uninterrupted hours of sitting.

How I implemented this in my life

I just have the alarms in my iPhone set for every hour of my work day. When the alarm goes off, I would stand up, immediately set a timer for 2 minutes, and start walking around the office. At first I was a little self-conscious. What would my colleagues think of me wandering here and there 8 times per day instead of sitting and working like everyone else? But I figured that my health is more important to me than what anybody’s opinion of me might be, so I kept doing my little ritual. The funny thing is nobody cared. I never had to explain myself. Well, actually, I did explain what I was doing to one of my colleagues, but I really didn’t have to, because he didn’t ask me.   

And now that I work from home I do just the same thing. I use alarms and timers and walk for 2 minutes every hour. I suggest you try it if you are interested in staying healthy.

Facebooktwitterredditpinterestlinkedinmail

Is programming a right choice for you? (career advice)

Can you become an iOS developer?  Or a any other type of programmer? No. You can’t. Just kidding.

If you are not a programmer yet and are only thinking about going into IT, then this article is for you. You see, when I was in that situation (when I worked as a civil engineer) I had my doubts. I wasn’t sure if the IT was the right thing for me. And I had invested a bunch of years into my engineering career already. Do I just throw all those years away?

Also, when I’d chosen my first career I thought it would be a perfect fit for me. I was wrong. What if I was likewise mistaken about programming? What if I was deluding myself for the second time?

As it turned out, programming is a much better fit for me than engineering. But the funny thing is that it now  seems to me, if I only knew the things I know now, let’s say several years ago, the transition to programming could have become unnecessary. I could have made it in engineering. I could have found an interesting and fulfilling job there.

Now I think that whether you are fulfilled at your job or not doesn’t depend only on the type of work you are doing. It has more to do with your whys. I spent most of my twenties not knowing what I want from life. I didn’t have any goals. Now I do have goals. And I imagine that if I started out not as an engineer but as a programmer right from the beginning, without goals, I might have become just as miserable a programmer as I was engineer. And on the other hand if I had goals right from the get go, I could have ended up a pretty good engineer, fulfilled with my work. Who knows?

So, if you are in a similar situation, if you don’t like your job or profession, ask yourself, is your job really a problem or maybe the problem is you, or more precisely your lack of goals.

Having said that, I still don’t know what whys I could have found for myself in the field of construction engineering. But I can tell you some of my programming whys.

Benefits of being a programmer

When I finally quit my engineering job and decided to learn iOS development I was considering two major benefits programming was going to bring into my life.

First is location independence. Second is the potential to make passive income. First one needs no explanation. If you are a programmer, theoretically you can work from anywhere in the world. All you need is a laptop and internet connection. As of now, I’m not sure if traveling is that important for me. I’d say that I like the idea of freedom to travel anywhere I want anytime I want more than I actually like traveling.

As for passive income, my thoughts were as follows. In order to make money you provide value to the marketplace. And if you want to get money passively all you need to do is to build a system that will provide value to the marketplace passively. And what is software? It is exactly that. A system that provides value passively. When it comes to providing value, software doesn’t differ much from any other thing made by man. Almost everything people create is designed to be useful in one way or another. But software, on top of that, is also designed to  work passively. Basically it works on its own and doesn’t require much maintenance once built. So, if you are interested in building streams of passive income it wouldn’t hurt you to learn programming. Basically every programmer is a professional creator of money machines. Ironically most of the programmers have to work just for their wages making money machines for other people.

Other benefits of programming

So, those were two things that excited me about becoming a programmer. Later I found other benefits of being a programmer. Like this one: I discovered that as a programmer you can show your work easily. You can build something – a site, an app – release it and share it with the world. Now everybody can see what you are capable of doing.

It wasn’t like that in engineering. Theoretically I could have stolen some CAD drawings from my previous employer to present them on my next job interview. Or I could have created some fake project. But it’s too much work and it would still be something fake. In construction engineering you can’t just create something on your own. Some authority has to approve your creation, otherwise it doesn’t count.

The situation in programming is totally different. For example, when I was learning iOS development, I spent one year unemployed, but I still had a lot to show for that year, because during that year I created several apps. Apart from being able to show your work easily you can work on your own projects just for the sake of honing your craft. For example, in the company I currently work for, we haven’t started using Swift yet. So I write my own apps in Swift. That’s how I keep my skills up to date with what is going on in the industry. Also, creating something on your own is just so interesting and fulfilling in itself. I found out that no matter how many hours a day I overwork on my main job I always have energy left for my own projects.

Basically what I’m trying to say here is that programming as an industry provides a very friendly environment for a creative type of person. And with a little bit of hard work your can get everything you ever wanted: autonomy (did I mention, I work from home?), location independence, money, creativity, heck, you can even start your business and be your own boss. I want all of that. That’s what motivates me to do my job.

If all those things motivate you too, than programming might be a good choice for you. Anyway, you need to know you whys. Without them you are going to be miserable in any profession.

Is programming a good fit for you?

I can’t tell you that. But I can tell you how I found the answer to that question for myself. Maybe you’ll even get something useful out of it. At first I had doubts. I wasn’t completely new to programming. I have programmed some stuff occasionally all through my life. It was just for fun and it wasn’t systematic. All I knew was that I liked to code. But I wondered if I would feel the same about programming if I was forced to code every day for 8 hours. There was no obvious answer to that. Also, how would I feel if I had to work on projects I might not necessarily care much about. So, I spent a lot of time hesitating.

But when I finally made a decision and started my new career, I found out that programming was a much better fit for me than I’d ever imagined. The demands this profession puts on a person matched a number of my personal qualities. Here is a list of them.

  1. I’m smart. OK, it might sound arrogant. What I mean by that is that I can figure things out. Like in a geeky way. I may be dumb in many other ways, but when it comes to learning new stuff, it just comes naturally to me. All through my school and university years I was a good student. All the cheaters in the class copied my homework. And working as a programmer can be compared to being a student. You just show up, do your tasks and get good grades. I’ve done that successfully for many years. So, if you were not good at school, maybe, just maybe, programming might not be your thing.
  2. I like to learn. Remember, I told you that in my twenties I didn’t know what I wanted to do with my life? In that period of time I had a hobby which was to learn languages. I’d learn Spanish, French, Armenian languages to various degrees. I even started to learn Chinese language. Then it all ended when I decided to learn programming. I figured why learn languages that you don’t even use when you can learn programming languages and get paid for it.
  3. Technical background. Sometimes in my work I take on tasks of such difficulty that I wonder if I’d be able to solve them without my technical education. But I have to say, such tasks are very rare. Most of the time you can just google your way to the solution of a problem.
  4. Communication skills. I always knew that I was an introvert. And it was so bad that people actually made remarks to me about that. They asked me why I rarely smiled and why I didn’t talk much, which made me uncomfortable. When I started working in an IT company all that changed. There are so many weird people in IT that I feel like a normal person for the first time in my life. In my new profession I had to deal with people who couldn’t talk normally. Usually you talk to such a person not because you want to but because you need to get some piece of information from him. And he just can’t express his thoughts. He doesn’t even look you in the eye when he talks. You can see how painful it is for him to talk to you and you start wishing you didn’t have to deal with that person in the first place. So, communication is important in programming. And I have just enough communication skills for this job.

I realise that this entire post was about me. But I wrote it hoping that it could be useful to other people also. Even though I’m talking here about me and my experiences, you can still get some insights from this article. You can compare your situation with mine or your personal qualities with mine and try to figure out if programming might be the right choice for you.

And finally I’d like to talk about another benefit I get from programming. And it is recognition. When I started working as an iOS developer, my colleagues immediately started to praise me for my work. They were surprised at how good my code was and how fast I could write it. They praised me so much that it even made me feel uncomfortable. For some time I even thought they were kidding or making fun of me. But they were not. It just so happens that programming is such a good fit for me, that I can actually realise my potential in this profession and not be indistinguishable from the masses like I was in my previous job.

I can give you an advice. If you want to change your profession or you are a young person just trying to figure out what you want to become when you grow up, pick a field not just by the benefits it can bring you. Pick a field that matches your personal qualities. Pick a field where you can become the best. And don’t aim to be average. Aim to be the best in your field. If you doubt that you can be the best in your field, probably you picked the wrong field. Whatever your pick, hard work is required. But it is better to work hard to become the best, than to work hard to be average.

But then again I already told you that I feel I could have made it in engineering if I only acted differently. Maybe this whole career picking is not that important. Maybe you just have to pick something and put in the hard work.

Here is a good book you can read on this topic.

Facebooktwitterredditpinterestlinkedinmail

Xcode crushes every time you open it. How to fix it?

I had this problem today when I decided to open Xcode and start working. It just crushed every time I tried to open it. What to do in this situation? Easy. Just delete xcuserdata from the project. To do this:

  1. go to your project folder in Finder
  2. right click .xcodeproj file (or .xcworkspace file if you use Cocoapods) and choose Show Package Contents
  3. delete xcuserdata folder
Facebooktwitterredditpinterestlinkedinmail

How to calculate height of a multiline String in Swift

Yesterday I spent like 2 hours trying to find answer to this question. Eventually I had to figure it out on my own using a bunch of Stackoverflow answers, each of which, if used alone, didn’t produce the desired result.

So, here is what I came up with. It works!

 

Facebooktwitterredditpinterestlinkedinmail

Popping to specific view controller (Objective-C)

I just wrote this piece of code for the project I’m currently working on.

You can use it if you know for sure that view controller of SpecificController class exists somewhere in the Navigation stack. Otherwise your app will crush when index goes down to -1.

 

Facebooktwitterredditpinterestlinkedinmail

How to save an image to photo library (Swift snippet)

This is how I did it in Sigma app with Swift 3.0

 

Facebooktwitterredditpinterestlinkedinmail

How to send an image via email (Swift snippet)

This is how I did it in Sigma app with Swift 3.0.

First you import MessageUI. Then you declare that your class adopts MFMailComposeViewControllerDelegate protocol. Then implement these two methods.

 

Facebooktwitterredditpinterestlinkedinmail