Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save robertopc/f3573196032db7ecd5a0d4230752a341 to your computer and use it in GitHub Desktop.
Save robertopc/f3573196032db7ecd5a0d4230752a341 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 \
mailutils # ferramentas de envio de email

Set correct timezone

dpkg-reconfigure tzdata

Keep date up to date

update-rc.d ntp enable

Set correct locale

dpkg-reconfigure locales

Install Apache and Allow in Firewall

sudo apt-get install apache2
sudo ufw app list
sudo ufw allow in "Apache Full" # WWW in debian server
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

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

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

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)

Quick Gotchas

If you get errors when running comppser install or composer create, this SO solution from James Njuguna will do the trick

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.

##Thanks #Notes

  • Try using laravel forge or cloudways if this process was a pain to you
  • Check out Docker
  • Test extensively and create a snapshot or create a backup of this point as well
@robertopc
Copy link
Author

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