Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Ismaaa/6bda555a0a8048e43e7c316bb8acafad to your computer and use it in GitHub Desktop.
Save Ismaaa/6bda555a0a8048e43e7c316bb8acafad to your computer and use it in GitHub Desktop.
LAMP Server setup for Ubuntu 16.04 on Digital Ocean

Server setup for Ubuntu 16.04 on Digital Ocean

The setup installs the following software:

The setup installs the following software:

  • Apache
  • MySQL
  • PHP
  • Node
  • Composer
  • Git

Update system

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

Add new user

Instead of using root as the user, we add a new one.

adduser <username>
usermod -aG sudo <username>

Install packages & dependencies

apt-get install -y \
build-essential \
python-software-properties \
python \
g++ \
make \
fail2ban \
curl \
git \
htop \
ntp \
ntpdate \
unzip \
nano \
mcedit \

Set correct timezone

dpkg-reconfigure tzdata

Install Apache and Allow in Firewall

sudo apt-get install apache2
sudo ufw app list
sudo ufw allow in "Apache Full"
sudo ufw allow in "OpenSSH"
sudo ufw enable
sudo ufw status verbose

Set Global ServerName to Suppress Syntax Warnings

Open up the Apache main configuration file

sudo nano /etc/apache2/apache2.conf
Inside, at the bottom of the file, add a ServerName directive, pointing to your primary domain name or your server's public IP address
```
. . .
ServerName server_domain_or_IP
```

Test the config and restart apache

sudo apache2ctl configtest
sudo systemctl restart apache2

Enable Apache mod_rewrite

sudo a2enmod rewrite
sudo systemctl restart apache2

Install MySQL

sudo apt-get install mysql-server -y
sudo mysql_secure_installation

Install PHP & Dependencies

sudo add-apt-repository ppa:ondrej/php
sudo apt-get update

sudo apt-get install php7.1 php7.1-common -y
sudo apt-get install php7.1-curl php7.1-xml php7.1-zip php7.1-gd php7.1-mysql php7.1-mbstring -y

Tweak how apache serves files Change From DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm To DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm

Restart & Check status

sudo systemctl restart apache2
sudo systemctl status apache2

You now have LAMP Installed...

Install Composer Globally

This does all the tricks

curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer

Install NodeJS (just incase JS frontend things)

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.5/install.sh | bash

Note: On Linux, after running the install script, if you get nvm: command not found, simply close your current terminal, open a new terminal, and try verifying again.

Install npm

nvm install node

Creating a 4GB Swap File (Essential for servers below 2GB RAM)

Create the file

sudo fallocate -l 4G /swapfile

Enable the swap file and set the correct permissions

sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

Make the the Swap File Permanent, edit the following file

sudo nano /etc/fstab

Go to the bottom of the file and add the following line

/swapfile   none    swap    sw    0   0

Tweak how much of the swap the system uses

sudo nano /etc/sysctl.conf

Insert the following lines at the bottom of the file

vm.swappiness=10
vm.vfs_cache_pressure = 50

End Of Server Setup

(Good Idea to take the Snapshot of your droplet at this point)

Setup Project On the Server (Laravel/Lumen - Latest version)

Create a new project

sudo mkdir /var/www/html/projects
cd /var/www/html/projects
composer create-project laravel/laravel <my-project-name> --prefer-dist

Configure the correct right permissions

sudo chgrp -R www-data /var/www/html/projects
sudo chmod -R 775 /var/www/html/projects/<my-project-name>/storage/

Now configure the vhost for your project

cd /etc/apache2/sites-available
sudo nano <my-project-name>.conf

Paste this in the file

<VirtualHost *:80>
    ServerName <domain or server IP>

    ServerAdmin <my-email>
    DocumentRoot /var/www/html/projects/<my-project-name>/public

    <Directory /var/www/html/projects/<my-project-name>
        AllowOverride All
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Disable the default.conf & Enable the .conf file. Enable mod_rewrite & restart apache.

sudo a2dissite 000-default.conf
sudo a2ensite <my-project-name>.conf
sudo a2enmod rewrite
sudo service apache2 restart

Install Git

sudo apt-get install git

Setup Git

git config --global user.name "<your-name>"
git config --global user.email "<your-email>"

We can see all of the configuration items that have been set by typing

git config --list

As you can see, this has a slightly different format. The information is stored in your git configuration file, which you can optionally edit by hand with your text editor like this: nano ~/.gitconfig

Good people it is done, go to the ip address or domain to check if things are sane. If not look for minor things like db config, keys etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment