DigitalOcean Ubuntu LAMP Installation with php7.2

So here is again another guide for DigitalOcean Ubuntu LAMP Installation. This guide is also applied to AWS EC2 Ubuntu 16.04.

We will install Apache2, php7.2, MYSQL, PHPMyAdmin

Basically I just tried to mix up multiple guides and put together as per my experience. Our first step will start with update and upgrade. Remember normally we should do it only for first time.

Update system
So first step we should do is upgrade and auto-remove

apt-get update && apt-get dist-upgrade -y
apt-get autoremove -y

Add new user
then after main problem with DigitalOcean droplet is, its not providing default Ubuntu user, it will just have root user. So we should create ubuntu user and user it by default rather then root user, Just like EC2 is providing

useradd -d /home/ubuntu -m -s /bin/bash ubuntu
usermod -a -G adm,cdrom,sudo,dip,plugdev ubuntu

Run `visudo` and add the following line at the end of the file.


Export existing SSH Keyes to ubuntu user
If you provided your SSH key when creating the droplet, you can copy the authorized_keys to the new `username` to skip needing a password when connecting.

mkdir /home/ubuntu/.ssh
cp /root/.ssh/authorized_keys /home/ubuntu/.ssh/
chmod 600 /home/ubuntu/.ssh/authorized_keys
chown -R ubuntu:ubuntu /home/ubuntu/.ssh

Add swap partition

this is most important step for RAM optimization, normally new guys seems missing this step. But ubuntu is using it as surplus RAM.

> check for swap partition
        sudo swapon --show
    > Check Memory Space
        free -h
    > Check HDD Space
        df -h
    > Create swap file
        sudo fallocate -l 4G /swapfile
        > varify it with
            ls -lh /swapfile
        > enable swap file
            sudo chmod 600 /swapfile
            sudo mkswap /swapfile
            sudo swapon /swapfile
    > Make swap file permenant
        sudo cp /etc/fstab /etc/fstab.bak
        echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
> Set swappiness to 10 and Cache Pressure to 10
    sudo nano /etc/sysctl.conf
    > add line to it :

Install packages

I have tried to list out most common packages here, some them might not be useful for you, but there is no harf if it will be installed extra.

eg. tcs – while installing redis server its better to install latest version of tcl before installing redis

sudo apt-get install -y \
build-essential \
python-software-properties \
python \
g++ \
make \
fail2ban \
apache2-utils \
curl \
bc \
git \
htop \
ntp \
ntpdate \
zip \
unzip \
tcl \
bcrypt \
autoconf \
openssl \
libssl-dev \
libcurl4-openssl-dev \
pkg-config \
libsasl2-dev \

Set correct timezone (this is generally not needed as it will be already UTC)

sudo dpkg-reconfigure tzdata

Install Apache2

sudo apt-get install apache2

Enable Required apache2 modes

sudo a2enmod headers proxy_http xml2enc proxy ssl proxy_wstunnel rewrite

Note: rewrite  // this mode is to enable htaccess

for .htaccess to work enabled mode rewrite

    sudo nano /etc/apache2/apache2.conf

    <Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride All  //------ this will be none and should be All
        Require all granted

    from redirect www to non www
    put below in : sudo nano sudo nano /etc/apache2/sites-available/000-default.conf
    <IfModule mod_rewrite.c>
        RewriteCond %{HTTPS} !=on
        RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
        RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]

Install PHP 7.2

sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get install php7.2
sudo apt-cache search php7.2-*
sudo apt-get install php7.2-fpm
sudo apt-get install php7.2-mbstring php7.2-curl php7.2-json 
sudo apt-get install php7.2-gd php7.2-xml php7.2-mysql php7.2-zip
sudo apt-get install php-pear php7.2-dev

sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php7.2-fpm
sudo apt-get install libapache2-mod-php7.2

pear config-set php_ini /etc/php/7.2/apache2/php.ini
pecl config-set php_ini /etc/php/7.2/apache2/php.ini

Install mysql phpmyadmin

sudo apt-get install mysql-server mysql-client
sudo apt-get install phpmyadmin

Please add respective password of root user when asked in terminal for mysql root user.

finally, here your ubuntu server should be ready with php7.2.

Ubuntu LAMP Installation is pretty much common topic, yet I found few things missing in most of them is to add ubuntu user and adding SWAP Partition. Swap partition is must while we are using server with like 2GB RAM, it just saves ubuntu services from crashing due to memory issue.

Don’t miss the next post!


Related Posts