parallax background

Install wordpress on amazon ubuntu server 14.0 ec2

Best WordPress Plugins
Best WordPress Plugins
14/01/2016
Human-Resources-767
Examples Log Queries & Find on Ubuntu
26/07/2016

Quick Launch

1 login with ubuntu user and run this commands:

sudo apt-get update
sudo apt-get -y install lamp-server^
sudo apt-get -y install mysql-server mysql-client libmysqlclient15-dev
sudo apt-get -y install apache2 apache2-doc apache2-mpm-prefork apache2-utils libexpat1 ssl-cert
sudo apt-get -y install libapache2-mod-php5 php5 php5-common php5-curl php5-dev php5-gd php5-idn php-pear php5-imagick php5-mcrypt php5-mysql php5-ps php5-pspell php5-recode php5-xsl
sudo apt-get -y install phpmyadmin (select Apache2)
mysqladmin -u root -p create dbname;
cd /var/tmp/
sudo wget http://wordpress.org/latest.tar.gz
sudo tar -zxvf latest.tar.gz
sudo cp -avr /var/tmp/wordpress/* /var/www/html
sudo rm /var/www/html/index.html
sudo chown -R www-data /var/www/html
sudo ls -la /var/www/html (check permissions)
go to www.yoursite.xxx and finish wordpress setup

download bash file

Run this command to allow copy and delete files via SFTP over SSH:
sudo chown sshuser /var/www/html

Create new db USER for wordpress db connection:

sudo mysql -u root -p
mysql> CREATE USER admindb@localhost IDENTIFIED BY 'password';
mysql> GRANT ALL PRIVILEGES ON dbname.* TO 'admindb'@'localhost';
mysql> FLUSH PRIVILEGES;
mysql> show databases;
mysql> exit

one command:
sudo mysql -u root -p mysql -e "CREATE USER admindb@localhost IDENTIFIED BY 'password';GRANT ALL PRIVILEGES ON dbname.* TO 'admindb'@'localhost';FLUSH PRIVILEGES;"

2 After installing wordpress:

 FIX Permalink /%postname%/ 404 not found
sudo sed -i 's/AllowOverride None/AllowOverride All/g' /etc/apache2/apache2.conf 
sudo a2enmod rewrite
sudo service apache2 restart
 FIX WP Memory Limit
sudo sed -i "/, false);/adefine( 'WP_MEMORY_LIMIT', '64M' );" /var/www/html/wp-config.php
FIX .hthaccess Parameters
sudo sed -i '/IfModule>/aphp_value upload_max_filesize 50M' /var/www/html/.htaccess
sudo sed -i '/IfModule>/aphp_value post_max_size 50M' /var/www/html/.htaccess
sudo sed -i '/IfModule>/aphp_value max_execution_time 500' /var/www/html/.htaccess
sudo sed -i '/IfModule>/aphp_value max_input_time 500' /var/www/html/.htaccess
sudo sed -i '/IfModule>/aphp_value max_input_vars 5000' /var/www/html/.htaccess

download bash file

3.a Restore Wordpress:

Backup the existing site
sudo apt-get -y install zip
sudo zip -r /var/tmp/oldweb.zip /var/www/html/
mysqldump -u root -p dbname > /var/tmp/db-backup.sql
Delete old web folder:
sudo rm -rf /var/www/html/*
sudo rm /var/www/html/.htaccess
Copy new site files & DB to temp folder:
copy newsite.zip to \var\tmp via FTP
copy newsite-db.sql to \var\tmp  via FTP
Restore web files:
sudo cp /var/tmp/newsite.zip /var/www/html/
cd /var/www/html
sudo unzip newsite.zip
sudo rm newsite.zip
 

3.b Restore Wordpress:

Restore DB:
mysqladmin -u root -p create newsitedb
sudo mysql -h localhost -u root -p --max_allowed_packet=1073741824 newsitedb < /var/tmp/newsite-db.sql
FIX Permission:
sudo chown -R www-data /var/www/html
Edit wp-config.php for sql connection:
sudo nano /var/www/html/wp-config.php
Change web url (if necessary):
http://newsite/phpmyadmin
edit table 'wp-options' field siteURL & Home

4.a Install Apps:

Task manager:
sudo apt-get install htop
htop
Net Monitor:
sudo apt-get install iptraf
sudo iptraf
FTP Server:
sudo apt-get -y update
sudo apt-get -y install vsftpd
sudo nano /etc/vsftpd.conf

Add the following lines to vsftpd.conf:
write_enable=YES
local_umask=022
chroot_local_user=YES
allow_writeable_chroot=YES
pasv_enable=Yes
pasv_min_port=40000
pasv_max_port=40100
local_root=/var/www/html

sudo service vsftpd restart
sudo useradd -m ftpuser1 -s /usr/sbin/nologin
sudo passwd ftpuser1
sudo nano /etc/shells & add /usr/sbin/nologin
sudo groupadd ftpusers
sudo usermod -a -G ftpusers ftpuser1 
sudo chmod -R 775 /var/www/html 
sudo chgrp -R ftpusers /var/www/html 
sudo ls -la /var/www/html 
open TCP Ports 13000 - 13100 & TCP Ports 20 - 21
sudo usermod -a -G www-data ftpuser1
check group sudo getent group www-data
Enable \ disable services:
sudo apt-get-y install sysv-rc-conf
sudo sysv-rc-conf
open print screen
Display bandwidth usage:
sudo apt-get install iftop
sudo iftop -p -n
open print screen
set Directory Lister CSS for /var/www/html/index.php
sudo apt-get -y install zip
sudo mv /var/www/html/index.html /var/tmp/
cd /var/tmp/
sudo wget https://www.yanivp.net/wp-content/uploads/DirectoryLister-2.6.1.zip
sudo unzip  DirectoryLister-2.6.1.zip
sudo cp -avr /var/tmp/DirectoryLister-2.6.1/* /var/www/html
sudo rm /var/www/html/index.html
open print screen

4.b Install Apps:

SMTP Service for sending email:
sudo apt-get -y install mailutils
System Monitor web-based:
sudo apt-get -y install rrdtool perl libwww-perl libmailtools-perl libmime-lite-perl librrds-perl libdbi-perl libxml-simple-perl libhttp-server-simple-perl libconfig-general-perl libio-socket-ssl-perl
wget http://www.monitorix.org/monitorix_3.8.1-izzy1_all.deb
sudo dpkg -i monitorix_3.8.1-izzy1_all.deb
sudo service apache2 restart
sudo service monitorix restart 
open 8080 port
http://site.xxx:8080/monitorix/
FireWall Enable
sudo ufw allow ssh/tcp
sudo ufw logging on
sudo ufw enable
sudo ufw status
sudo ufw allow http/tcp
sudo ufw delete deny 22 ( delete rule )
sudo ufw allow proto tcp from 192.168.0.2 to any port 22 ( allow access from specific hosts )
sudo ufw allow proto tcp from 192.168.0.0/24 to any port 22 allow from specific subnet )
sudo ufw app list ( applications list )
sudo ufw allow Samba ( allow applications )
sudo ufw allow from 192.168.0.0/24 to any app Samba ( allow applications )
sudo ufw app info Samba ( info )
sudo nano /var/log/ufw.log ( firewall log location )
sudo ufw status verbose ( check level log )
sudo ufw logging full (all packages )
sudo ufw logging medium ( all allowed packets not matching the default )
sudo ufw logging low ( all blocked packets )
block ping: sudo nano before.rules
change from ACCEPT to DROP :

# ok icmp codes for INPUT
-A ufw-before-input -p icmp --icmp-type destination-unreachable -j DROP
-A ufw-before-input -p icmp --icmp-type source-quench -j DROP
-A ufw-before-input -p icmp --icmp-type time-exceeded -j DROP
-A ufw-before-input -p icmp --icmp-type parameter-problem -j DROP
-A ufw-before-input -p icmp --icmp-type echo-request -j DROP

sudo service ufw restart ( restart firewall to apply )
Fix log time to server timezone
sudo service cron restart
sudo service rsyslog restart

5 Useful command lines:

sudo nano /var/www/html/.htaccess
sudo nano /var/www/html/wp-config.php
sudo cp /var/tmp/.htaccess /var/www/html/
sudo service apache2 status
sudo service apache2 restart
sudo reboot
sudo rm -rf /var/www/html/xxxx
sudo rm /var/www/html/.htaccess
sudo chmod -R 777 /var/www/html  ( full control )
sudo chmod -R 774 /var/www/html ( public = read )
sudo chgrp -R groupname /var/www/html ( add group to folders )
sudo chattr +a /var/www/html/ ( create files inside but  not be able to delete them )
sudo chattr -a /var/www/html/ ( disable: create files inside but  not be able to delete them )
sudo chattr +i /var/www/html/aaa/ ( delete all files except aaa folder )
sudo chattr +i /var/www/html/aaa/ disable: delete all files except aaa folder )
sudo chown ubuntu /var/www/html
sudo ls -la /var/www/html ( view permissions )
sudo useradd username ( create new user)
sudo groupadd groupname ( create new group )
sudo usermod -a -G groupname username ( add user to group )
grep groupname /etc/group ( show users in group )
sudo passwd username ( set password )
sudo service cron restart
sudo timedatectl set-timezone America/New_York ( set time zone )
sudo apt-get -y  update && sudo apt-get -y upgrade ( install packages updates )
sudo touch /etc/motd.tail ( reset updates list after packages updates )
sudo sed -i 's/AllowOverride None/AllowOverride All/g' /etc/apache2/apache2.conf 
mail ( show mail list ) to delete mails type "mail" ? delete *
FIX permissions folders #1: sudo chmod 2775 /var/www
FIX permissions folders #2:find /var/www -type d -exec sudo chmod 2775 {} \;
FIX permissions files: find /var/www -type f -exec sudo chmod 0664 {} \;
zip -r file.zip /var/www/html/
service --status-all ( status of all the services )
pwd ( folder location )
sudo netstat -tulpn ( show open ports )
sudo -i ( switch to sudo user )
su ubuntu ( switch to ubuntu user )
netstat -at ( show open tcp ports )
netstat -atu ( show open tcp & udp ports )
netstat -atun ( show open tcp & udp ports numbers )
sudo netstat -tupln ( show open tcp & udp ports numbers #2 )
netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n ( show list ip address )
watch -n 30 "netstat -ntu | awk '{print \$5}' | cut -d: -f1 | sort | uniq -c | sort -n" ( show list ip address auto )
sudo du -sh /var/www/html/wp-content/ ( check folder size )
sudo du -hs * | sort -h ( sort by size )
du -h | perl -e 'sub h{%h=(K=>10,M=>20,G=>30);($n,$u)=shift=~/([0-9.]+)(\D)/;
return $n*2**$h{$u}}print sort{h($b)<=>h($a)}<>;' ( view size, only folders )
perl -e'%h=map{/.\s/;99**(ord$&&7)-$`,$_}`du -h`;die@h{sort%h}'( view size 2 levels  )
sudo du -k * | sort -nr | cut -f2 | xargs -d '\n' du -sh ( sort by size )
df -h ( disk size )
sudo cp latest.tar.gz $PWD/ ( cp to current directory )
sudo apt-get --purge remove htop ( uninstall app htop )
ctrl + r  ( reverse history search )
who is logged in ( w )
grep word /var/www/html/* ( fine word in files )
grep word /var/www/html/* |uniq ( fine word in files #2)


6 AWS - Copy files from EC2 to S3:

Create a Bucket in AWS S3 https://www.yanivp.net//console.aws.amazon.com/s3 like files.s3.backup
Create IAM User in AWS https://www.yanivp.net//console.aws.amazon.com/iam/home?region=eu-west-1#users/ like test1 and copy the Access Key ID: & Secret Access Key:
Create custom policy and paste:
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "s3:*",
            "Resource": [
                "arn:aws:s3:::files.s3.backup/*",
                "arn:aws:s3:::files.s3.backup"
            ]
        }
    ]
}
From EC2 run> sudo apt-get update
sudo apt-get -y install awscli
sudo -i
aws configure

AWS Access Key ID [None]: XXXX
AWS Secret Access Key [None]: XXXX
Default region name [None]: eu-west-1
Default output format [None]: json

Test copy files from ec2 to s3 > aws s3 cp /var/tmp/ s3://files.s3.backup/webs --recursive
Test delete files from s3 > aws s3 rm s3://files.s3.backup/webs --recursive
Test copy only changed files from ec2 to s3 > aws s3 sync /var/tmp/ s3://files.s3.backup/webs
install wordpress

Create a Bucket

install wordpress

Create IAM User

install wordpress

Create Custom Policy

install wordpress

Paste Policy

install wordpress

AWS Configure

7 Set up a Cron job to copy files from EC2 to S3

sudo nano /etc/job1.sh

copy & paste: copying only the files that have changed

 

 
#!/bin/bash

  aws s3 sync /var/tmp/ s3://files.s3.backup/webs --region eu-west-1

sudo apt-get -y install awscli
aws configure

AWS Access Key ID [None]: XXXX
AWS Secret Access Key [None]: XXXX
Default region name [None]: eu-west-1
Default output format [None]: json

sudo nano /etc/crontab
copy & paste: ( run every day at 11 pm )
 
 
  0 23 * * * ubuntu /etc/job1.sh
 


sudo chmod +x /etc/job1.sh

Cron Generator #1
Cron Generator #2
Install wordpress on amazon ubuntu server 14.0 ec2

Ubuntu /etc/job1.sh

Cron Examples

1Run every minute
* * * * * ubuntu /etc/job1.sh
2Run every hour
0 * * * * ubuntu /etc/job1.sh
3Run every day at 3 am
0 3 * * * ubuntu /etc/job1.sh
4Run every Friday at 22 pm
0 22 * * 5 ubuntu /etc/job1.sh
5Run every 2 hours
0 */2 * * * ubuntu /etc/job1.sh
6Run every Monday and Thursday at 11 am
0 11 * * 1,4 ubuntu /etc/job1.sh

8 Setup  SMTP to send emails:

sudo apt-get -y update
sudo apt-get -y install postfix
sudo apt-get -y install mailutils
echo "This is the body of the email" | mail -s "This is the subject line" xx@xx.xxx
echo "This is the body of the email" | mail -s "This is the subject line"xx@xx.xxx < /var/tmp/testfile.txt

9 Creating an AWS snapshot:

Create IAM User in AWS https://www.yanivp.net//console.aws.amazon.com/iam/home?region=eu-west-1#users/ like test1 and copy the Access Key ID: & Secret Access Key:
Create custom policy and paste:
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "ec2:Describe*",
                "ec2:CreateSnapshot",
                "ec2:DeleteSnapshot"
            ],
            "Resource": "*"
        }
    ]
}
aws configure
AWS Access Key ID [None]: XXXX
AWS Secret Access Key [None]: XXXX
Default region name [None]: eu-west-1
Default output format [None]: json
sudo apt-get -y install ec2-api-tools
sudo apt-get -y install awscli
aws ec2 create-snapshot  --volume-id=vol-7f74888a --description "test 15 cron( run snapshot )
aws ec2 delete-snapshot --snapshot-id snap-xxxxxxxx ( delete snapshot )

Delete all snapshot except the last 5 snapshots:

ec2-describe-snapshots --aws-access-key XXXX --aws-secret-key YYYY --region eu-west-1 --filter "volume-id=vol-7f74888a" | sort -r -k 5,5 | sed 1,5d | awk '{print $2}' | xargs -I{} ec2-delete-snapshot {} --aws-access-key XXXX --aws-secret-key YYYY --region eu-west-1 
sudo nano /etc/job1.sh
copy & paste: create snapshot and delete all snapshots except the last 5 snapshots
 
#!/bin/bash
aws ec2 create-snapshot  --volume-id=vol-7f74888a --description "test 15 cron"
ec2-describe-snapshots --aws-access-key XXXX --aws-secret-key YYYY --region eu-west-1 --filter "volume-id=vol-7f74888a" | sort -r -k 5,5 | sed 1,5d | awk '{print $2}' | xargs -I{} ec2-delete-snapshot {} --aws-access-key XXXX --aws-secret-key YYYY --region eu-west-1

 

sudo nano /etc/crontab
copy & paste: ( run every day at 3 am )
 
0 3 * * * ubuntu /etc/job1.sh
sudo chmod +x /etc/job1.sh
Cron Generator #1
Cron Generator #2

Comments are closed.