My websites where hosted on a shared webhosting environment for years. As mentioned in earlier blog posts I migrated everything last year to my own VPS at Digital Ocean. The reason for this is more flexibility and better performance. But what steps did I take and how I set it up?
Digital Ocean and Serverpilot
I’ve created a droplet at Digital Ocean for $10 a month with 1GB memory, 1 core and a 30GB SSD disk. I can recommend to name your server with a hostname, so instead of “royduineveld”, name it “royduineveld.nl”, it’s important to do so for a PTR record. I’d like to use Serverpilot so I’ve created a account and followed the steps. A alternative for Serverpilot is Laravel Forge, I’ve created a comparison between them.
Server setup
A lot of things are configured for you by Serverpilot but some things you’ll have to do yourself, just read through the docs and see what’s handy for you. Things I’ve done:
General setup
- Change the timezone, it’s important to restart your server after changing it so other services like MySQL and crontab adapt it:
sudo dpkg-reconfigure tzdata
andsudo reboot
Security steps
- Setup SSH public key authentication, it’s more secure and easier then using passwords:
echo "MY SSH KEY" >> /srv/users/serverpilot/.ssh/authorized_keys
- Disable SSH password authentication, you can always access your server from the console at Digital Ocean. Edit
/etc/ssh/sshd_config
and setPasswordAuthentication no
and restart the service:sudo service ssh restart
Monitoring
I like New Relic for monitoring, I’ve setup APM and Browser to monitor my PHP applications, Synthetics to get notified when something goes down and Server to keep track on statistics.
- Install New Relic Server (replace the license key):
echo deb http://apt.newrelic.com/debian/ newrelic non-free >> /etc/apt/sources.list.d/newrelic.list wget -O- https://download.newrelic.com/548C16BF.gpg | apt-key add - apt-get update apt-get install -y newrelic-sysmond nrsysmond-config --set license_key="YOUR LICENSE KEY" /etc/init.d/newrelic-sysmond start
- Install New Relic APM for PHP 7.0 (select option 2 in the installer, provide your license key when asked and replace your server name)
wget -O - https://download.newrelic.com/548C16BF.gpg | sudo apt-key add - sudo bash -c "echo deb http://apt.newrelic.com/debian/ newrelic non-free > /etc/apt/sources.list.d/newrelic.list" sudo apt-get update sudo apt-get install -y newrelic-php5 sudo NR_INSTALL_PATH=/opt/sp/php7.0/bin newrelic-install sed -i "/^newrelic.appname =/s/=.*/= \"YOURSERVERNAME\"/" /etc/php7.0-sp/conf.d/newrelic.ini sudo service php7.0-fpm-sp restart
Afterwards create a
.user.ini
in your app directories:~/apps/APPNAME/public/.user.ini
with the application name in it:newrelic.appname = "APPNAME"
Application specific
Some applications of mine are using Redis as cache backend, I’m also using the Gulp build tool in some projects which requires Node.js and for Magento 1 projects Magerun is a very handy tool.
Redis Server with Redis for PHP 7.0
sudo apt-get install -y redis-server sudo apt-get install -y gcc make autoconf libc-dev pkg-config sudo pecl7.0-sp install redis sudo bash -c "echo extension=redis.so > /etc/php7.0-sp/conf.d/redis.ini" sudo service php7.0-fpm-sp restart
Node.js with NPM
sudo apt-get update sudo apt-get install -y nodejs sudo apt-get install -y npm sudo apt-get install -y nodejs-legacy chown serverpilot:serverpilot /srv/users/serverpilot/tmp/
I’m using Node.js to run Gulp on my server, on your local machine you probably installed Gulp globally, don’t do that on your server! After running npm install
you can run Gulp with node_modules/.bin/gulp
or for example with Laravel Elixir you can run npm run prod
Magerun
wget https://files.magerun.net/n98-magerun.phar -O /usr/local/bin/magerun chmod +x /usr/local/bin/magerun chown serverpilot:serverpilot /usr/local/bin/magerun
Backups
I’ve written another article about server backups and how I’ve set it up, see: Backup your server with AutoMySQLBackup and Duplicity.
For email I’ve also written a dedicated article: Free email forwarding.
Conclusion
Moving from shared hosting to a VPS gives you flexibility, you can do whatever you want with your server. But it requires some server/linux knowledge, when you’re using Serverpilot a lot is taken care of, including updates! But when something goes wrong you need to fix it yourself.