AWS Server configuration

Amazon   Linux  2  OS

Step-0: (Create EC2 instance)

  • Create 2 security groups(Instance, ELB)
  • Create Amazon Linux 2 (x86) Instance
  • Create an  elastic IP address and  associate it to the instance
  • Create 1 target group for http only (even if you want https in yourdomain.com) and add target to the instance.
  • Create 1 load balancer(HTTP/HTTPS) with all vpc. configure routing with http. Add target to the instance on port 80.
  • Convert the downloaded example.pem  to example.ppk with puTTygen software.

Step-1 (nginx, php and  phpmyadmin)

 Follow  all  the steps from amazon's tutorial link  below but note that
  1. Instead of  step 4  Run two commands:
    1. sudo amazon-linux-extras install nginx1
    2. sudo yum install -y mariadb-server
  2. In  all other  place  use nginx instead of httpd
  3. Your root directory will be  /usr/share/nginx/html
  4. create  phpmyadmin in  /usr/share/nginx/html/public  (for  laravel)
Now  follow: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-lamp-amazon-linux-2.html.

Note: During permission setting, don't be confused with apache group. Because php-fpm uses apache groupd nad www user for permission (See /etc/php-fpm.d/www.conf).

Step-2  (phpmyadmin  config)

Inside  phpMyAdmin (that was  created  in  step-1) folder create config.inc.php with
cat  >  config.inc.php
and populate  with
<?php
// use here a value of your choice at least 32 chars long
$cfg['blowfish_secret'] = '1{dd0`<Q),5XP_:R9UK%%8\"EEcyH#{o';

$i=0;
$i++;
$cfg['Servers'][$i]['auth_type']     = 'cookie';
$cfg['TempDir'] = '/tmp';
// if you insist on "root" having no password:
// $cfg['Servers'][$i]['AllowNoPassword'] = true;

Step-3 (nginx.conf  for laravel)

Nginx  does not require  any .htaccess file and  it should  not be. So  delete .htaccess  from  public  folder.

Open /etc/nginx/nginx.conf and look  for  http and  update server inside that  http  as like below:

server {

listen       80;

listen       [::]:80;

server_name  yourdomain.com www.yourdomain.com;

root         /usr/share/nginx/html/public;

# Load configuration files for the default server block.

include /etc/nginx/default.d/*.conf;

location / {

try_files $uri $uri/ /index.php?$query_string;

}

}

Step-4: (Increase file upload size if needed)

1. Change in /etc/nginx/nginx.conf

Add the follwing line within http for all server.

client_max_body_size 100M;

2. Changes in /etc/php.ini (search and change)

upload_max_filesize = 100M

post_max_size = 100M

Step-5 : (Route 53)

Create hosted zone and two records with

  1. example.com
  2. www.example.com
From  here copy 4 nameservers to the namechep/godaddy or anywhere else your domain registered from.

Step-6: (Cloudfront distribution)

Create cloudfront distribution with:
  1. Whitelist Headers: Host
  2. Forward Cookies: all (to solve token mismatch)
  3. Query String Forwarding and Caching: Forward all, cache based on all (to cache based on url parameters)

Step-7:  (Laravel truested proxies)

Link: https://laravel.com/docs/8.x/requests#configuring-trusted-proxies
  1. protected $proxies = '*';
  2. protected $headers = Request::HEADER_X_FORWARDED_AWS_ELB

Others

#Install  composer
cd ~
sudo curl -sS https://getcomposer.org/installer | sudo php
sudo mv composer.phar /usr/local/bin/composer
sudo ln -s /usr/local/bin/composer /usr/bin/composer
sudo composer install
#Install nodejs
sudo amazon-linux-extras install epel 
sudo yum install nodejs npm --enablerepo=epel

#Install  git
sudo  yum  install git

Reinstall mariadb

  1. yum remove mariadb mariadb-server
  2. rm -rf /var/lib/mysql If your datadir in /etc/my.cnf points to a different directory, remove that directory instead of /var/lib/mysql
  3. rm /etc/my.cnf the file might have already been deleted at step 1
  4. Optional step: rm ~/.my.cnf
  5. yum install -y mariadb-server

Problems

  • database server (mariadb) stops working after a few days of my fresh server installation

    or, mariadb Cannot allocate memory

    Solution:RAM size of the server is not enough to run all the applications. That's why mariadb server is getting stopped.
    Use the command: free -hm to check the free space in RAM and wheather swap space was configured or not.
    There are two solutions:

    • Increasing the RAM size
    • Creating a swap space

    I choose the second option and followed amazon's documentation: https://aws.amazon.com/premiumsupport/knowledge-center/ec2-memory-swap-file

Labels: , , , ,

© copyright-2020 Rejaul