Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Otienoh
Last active January 4, 2022 04:53
Show Gist options
  • Star 28 You must be signed in to star a gist
  • Fork 22 You must be signed in to fork a gist
  • Save Otienoh/6431b247d1bddfddb12f3dda436615d0 to your computer and use it in GitHub Desktop.
Save Otienoh/6431b247d1bddfddb12f3dda436615d0 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 

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
sudo mysql_secure_installation

Install PHP & Dependencies

You should check the instructions for the php version you need

sudo apt-get install php libapache2-mod-php php-mcrypt php-mysql

Tweak how apache serves files

sudo nano /etc/apache2/mods-enabled/dir.conf
 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

Install php-cli

sudo apt-get install php-cli

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 -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
apt-get install -y nodejs

Install npm

npm update -g

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 5.2)

Upload your laravel project to the webroot folder and configure the correct right permissions

sudo chgrp -R www-data /var/www/html/project
sudo chmod -R 775 /var/www/html/project/storage

Now configure the vhost for your project

cd /etc/apache2/sites-available
sudo nano laravel.conf

Paste this in the file

<VirtualHost *:80>
    ServerName domain or server_ip

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/project/public

    <Directory /var/www/html/project>
        AllowOverride All
    </Directory>

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

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

sudo a2dissite 000-default.conf
sudo a2ensite laravel.conf
sudo a2enmod rewrite
sudo service apache2 restart

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.

Quick Gotchas

If you get errors when running comppser install, this steps will do the trick

sudo apt-get update
sudo apt-get install mcrypt php7.0-mcrypt
sudo apt-get upgrade
sudo apt-get install php-mbstring
sudo apt-get install phpunit

The above step installs the missing dependecies.

##Thanks #Notes

  • Try using laravel forge or cloudways if this process was a pain to you. I highly recommend Ploi.io
  • Check out Docker
  • Test extensively and create a snapshot or create a backup of this point as well
@azazqadir
Copy link

I find installing PHP on debian based DigitalOcean server quite easy. Debian has a bigger and helpful community. Also, I am using a managed server platform that allows me easily install PHP on DigitalOcean (https://www.cloudways.com/blog/host-php-on-digitalocean/ ) without having to manually install OS and server.

@ibnuh
Copy link

ibnuh commented Oct 16, 2017

Thank you very much, help me alot

@Emmathem
Copy link

I just installed my laravel project on Digital Ocean, ubuntu 16.04, how can the url from the normal ip address to something like websit.dev (virtual host)

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