Showing posts with label Amazon. Show all posts
Showing posts with label Amazon. Show all posts

Monday, January 15, 2018

.htaccess

Hello,

Recently I configured SSL on Amazon EC2 instance running Apache server. In this blog I am going to explain the procedure step by step.

Step 1 : Generate CSR and submit to CA authority for verification

To generate CSR login to your server with SSH and use following command.

openssl req -new -newkey rsa:2048 -nodes -keyout YourDomain.key -out YourDomain.csr

Once you execute this command it will ask for certain inputs and based on it it will generate csr file. That you have to submit to CA authority and generate

Step 2 : Edit Inbound rules on your EC2 Instance and allow HTTPS

Login to Amazon AWS console and go to EC2 dashboard. Click on instance and edit security rules. Select HTTPs and allow it from anywhere.


Step 3 : Upload your SSL certificate and key file to EC2 using SSH

 You can use SCP command to upload crt file and pem key file to server.

scp -i "YourKey.pem" "Cert.crt" ubuntu@YourIP:/home/ubuntu/Cert.crt
scp -i "YourKey.pem" "Key.key" ubuntu@YourIP:/home/ubuntu/Key.key

Step 4 : Edit Default SSL config file and Add Certificate In File

Go to  cd /etc/apache2/sites-available

Update default-ssl.conf file by using command

sudo nano default-ssl.conf

Add following lines in file

SSLEngine on
SSLCertificateFile      /home/ubuntu/Cert.crt
SSLCertificateKeyFile /home/ubuntu/Key.key

Save the file.

Step 5 : Enable SSL mode in Apache and set config file to default-ssl.conf

Now we have ssl config file set, we have to enable SSL mode in apache and set config file.

Use following command

sudo a2enmod ssl
sudo a2ensite default-ssl.conf
sudo service apache2 reload
sudo service apache2 restart

That's it and now you can access your web app on HTTPS. Hope this helps you.

Wednesday, September 27, 2017

Ubuntu Keep Process Running After SSH is Terminated

Hello,

Recently I was learning NodeJS and for that I faced an issue with node server. I installed node server via SSH and started server. But as soon as I closed terminal Node server stopped working. But I needed server up and running even if terminal is closed. So after struggle of sometime I found out solution. Here is what we have to do.

Basically when you type command on SSH and start server it runs on the foreground and wait for process to be finished. So as soon as you close terminal foreground process is also terminated and hence your server is stopped.

So in this case you have to run server as background process and if you are using ubuntu you can use nohup  and & to start running process in background hence it will not be terminated once the terminal is closed.

So here is the command you have to follow.

$ nohup "COMMAND" &

Here COMMAND is your command to start the server and since you have mentioned nohup and & it will run the process in background and will not be terminated when SSH is closed.

Friday, June 2, 2017

Magento Amazon SES - Transactional Emails Not Working with SMTP Pro

Recently in one of our project, we were using Amazon SES service with SMTP pro extension in Magento 1.9. After verification of domain and sender email address, when we to test in SMTP pro, it was working fine. But when we try to send transactional emails such as new order, invoice etc. It was not working. So on debugging we found following exception.

Email address is not verified. The following identities failed the check in region US-EAST-1: hdave10@gmail.com

Now that was bit strange as that raised a question that do we have to verify all the receiver email as well and that was practically not possible as in Magento we can have any number of customers. 

So I checked docs of Amazon SES and after reading for a while I got the issue. 

The issue was we have configured three regions in Amazon SES. But we were still in sandbox mode for two regions and mail was sending from the region where we were still running in sandbox mode. 

To solve this issue log in to your Amazon SES console and select region from top left corner.


Once you select region, Go to SES Sending Limits Increase case.

and submit for limit increase. It will take a while to process your request. Once your limit is increased, you will come out of sandbox mode. You have to do this process for all the regions.

Hope this helps you.

Wednesday, May 31, 2017

Amazon EC2 Create Swap Space

Hello,

Recently in one of my project we have laravel application deployed on amazon ec2 instance. When running composer update command, we were facing issue of swap space as it was not defined. So in this blog I will explain how you can create swap space in your amazon ec2 instance. Please note these steps are specifically for ubuntu instance. If you are running other version of liunx, please check commands first and then use it.

1) Create swap file

sudo fallocate -l 1G /swapfile

2) Verify it

ls -lh /swapfile

It should show following output.

-rw-r--r-- 1 root root 1.0G Apr 25 11:14 /swapfile

3) Enable it and make it accessible by only root user.

sudo chmod 600 /swapfile

sudo mkswap /swapfile

sudo swapon /swapfile

That's it now you have swap space configured in your instance. 

Sunday, January 8, 2017

.htaccess Doesn't Work in Amazon AWS EC2 Instance with Ubuntu

Recently I was configuring Laravel application in Amazon AWS EC2 instance which was having Ubuntu and Apache installed. The problem was htacess file was not working so Laravel routes were not working. Instead of parsing laravel routes it always shows error URL not found.

The problem is by default in apache installed on ubuntu doesn't have .htaccess enabled so first you have to manually enable it in config file and restart apache.

Here are the steps.

1) Login to your EC2 instance with SSH.

ssh -i "Yourkey.pem" ubuntu@yourpublicip

2) Enable Mode Rewrite, this is just incase if it's not enabled.

sudo a2enmod rewrite

3) Go to following directory.

cd /etc/apache2/sites-available

If you use LS command here you will see following file.

000-default.conf

That's the file with default apache configuration which are applied to your sites in /var/www/html folder.

4) Open this file for editing.

sudo nano 000-default.conf

5) Add following lines after DocumentRoot /var/www/html line.

<Directory />
     Options FollowSymLinks
     AllowOverride All
</Directory>
<Directory /var/www/html>
     Options Indexes FollowSymLinks MultiViews
     AllowOverride All
     Order allow,deny
     allow from all
</Directory>

6) Save the file and restart the apache.

sudo service apache2 restart

that's it and now your .htaccess file will work and your laravel application will start working.

Saturday, October 1, 2016

Laravel 5.x Store Uploaded Files to Amazon S3

Recently in one of my Laravel 5.2 project, we used Amazon s3 to store static files and user uploaded files. So here in this blog I am going to explain how to do this.

1) First of all install following plugin through composer.

composer require league/flysystem-aws-s3-v3 ~1.0

2) Now add your S3 credentials to your environment (.env file)

S3_KEY=YOUR_KEY
S3_SECRET=YOUR_SECRET
S3_REGION=YOUR_REGION
S3_BUCKET=YOUR_BUCKET

3) Open config.filesystems.php and configure s3 driver as follow.

's3' => [
            'driver' => 's3',
            'key'    => env('S3_KEY'),
            'secret' => env('S3_SECRET'),
            'region' => env('S3_REGION'),y.
            'bucket' => env('S3_BUCKET'),
        ]

4) Now open controller where you have code to manage uploaded file and add following dependency.

use Illuminate\Contracts\Filesystem\Filesystem;

5) Use following code to store your file to S3.

$image = $request->file('user_uploaded_image');
$imageFileName = time() . '.' . $image->getClientOriginalExtension();
$s3 = \Storage::disk('s3');
$filePath = '/mypath'/.$imageFileName;
$s3->put($filePath, file_get_contents($image), 'public');
$uploadedFileS3Path = 'https://'.env('S3_BUCKET').'/'.$filePath;

Please note in my case I configured S3 bucket to be direct URL of uploaded file. In case your case if you have not configured it then your path will be as follow.

$uploadedFileS3Path = 'http://s3-'.env('S3_REGION').'.amazonaws.com/'.env('S3_BUCKET').'/'.$filePath;

This path you can use to display file and you can store it to database as well.


Special Case 

If you are using it in localhost using XAMPP in your windows system. You have to do following few steps.

Download certificate pem file from this link. https://curl.haxx.se/ca/cacert.pem and store it to some where in your C drive or whatever drive you have your xampp installed.

For example I put it in  C:\code\cacert.pem

Now open your php.ini file and bottom of the file add following line.

curl.cainfo = C:\code\cacert.pem

That's it and it will work in your local environment too. Hope this helps you.

Wednesday, September 23, 2015

Upload File From Local Machine to Remote Server with SSH

Hello,

Recently while working on SSL on my Amazon EC 2 server I had to upload few certificates files from my local machine to EC 2 server. I had to spent some time to figure out this so thought of publishing blog on it so this may help others

First lets see how to upload file to remote server with permission on directory. We will use scp command for it. Following is the syntax.

$ scp /path/to/local/file sshuser@host:/path/to/remote/server

Once you run command it will prompt you for password. When you enter password it will upload file.

Now lets see what if you don't have password and have public or private key. Following is the command.

$ scp -i "yourkey" /path/to/local/file sshuser@host:/path/to/remote/server

If you have specified correct key it will upload your file.

Now here comes the real problem with EC 2. I was trying to upload crt file to system directory which was not allowed. So what you have to do is upload that file default ssh directory with following command

$ scp -i "yourkey" /path/to/local/file sshuser@host:filename

And then go to that directory. Usually in amazon EC 2. It's /home/ubntu directory. Then you can copy files from here using cp command with sudo.

I hope this helps you and dave your time.




Monday, September 14, 2015

Amazon EC2 File Upload Permission

Hello,

Recently in one of my project we hosted our PHP website on Amazon EC 2 which was running Apache on ubuntu. In our project we have file upload functionality which was not working. First I tired to set permission to 777 for the upload directory but still it was not working, so I went into details and found out this was the user permission issue. In this blog I am going to explain how to solve this.

Your website is running in Apache on Ubuntu. So there is a apache user, which is actually handling file operations. By default apache user does not have write permission and it does not own your website folder. So there are three basic steps to resolve this issue.

1) Find out your apache user.

Fist we have to find out name of apache user. Run following command in terminal.


ps -ef | grep apache

Check the left most column of the output. See the image below. That is name of your apache user.

In my case it was www-data user. 

2) Change directory owner to Apache user.

Change owner of your upload directory.

sudo chown www-data /var/www/html/uploads

3) Setup permission on your directory.

sudo chmod 755 /var/www/html/uploads

That's it. Now you can upload file from your web application. Hope this helps you and save your time.





Monday, August 31, 2015

Install and Configure PHP/Apache on Amazon EC 2 with Ubuntu (With SFTP)

Hello,

Recently for one of my project we moved our web application to Amazon EC 2. So we had to configure Aapche and setup SFTP there. I faced some difficulties in setting this up so here in blog I am going to explain you the process so it will help you.

First of all login to SSH on your Amazon EC 2 instance. For that first you have to download public key from your amazon EC 2 instance.  You can use public IP of your EC 2 instance to login to SSH.

Open your terminal and type following command.

ssh -i "YourKey.pem" ubuntu@your_ip_address

once you are logged in first of we have to install apache. For that run following command.

First we will install apache with PHP 7 and configure it.

sudo apt-get install apache2 php7.0 libapache2-mod-php7.0
Now verify it


a2query -m php7.0
Load PHP module.


sudo a2enmod php7.0

Restart apache.


sudo service apache2 restart

New version of ubuntu comes with PHP 7 but to access it you have to install php command lines. Run following command.


sudo apt-get install php7.0-cli

Now test it with

php -v

This will install apache, php with single command. Once installation is done type public ip in browser and it should show apache startup page. If you see this page that means apache installed. Now lets test PHP.

Go to your apache root directory with following command

cd /var/www/html

There will be one index.html file. We have to remove this and create new file for testing.

sudo rm index.html

sudo vim index.php

This will open vim editor, press i and now you can type in file. Let's create simple php file

<?php
     echo "Hello AWS";
?>


press esc and : and type wq this will save file. Now again type public IP in browser and it should show Hello AWS on browser.

Now let's create new SFTP user. But for that first we have to allow password authentication for SSH. Type following command.

cd /etc/ssh
sudo vim sshd_config

Find allowPasswordAuthetnciation. It will be set as no, make it yes and save the file and restart SSH service.


sudo service ssh restart

Now we can add user with password.

sudo adduser --home /var/www/html username

It will ask for password. Now user is created with same group name. Now we have to make this user, owner of /var/www/html so we can edit files via FileZilla. Now lets fist change ownership of folders and files.

sudo chown -R root:username /var/www/html
sudo find /var/www/html -type f -exec chmod 664 {} \;
sudo find /var/www/html -type d -exec chmod 775 {} \;
sudo find /var/www/html -type d -exec chmod g+s {} \;

That's it now you can use this username and password with SFTP in FileZilla. Hope this helps you.