Skip to content

Instantly share code, notes, and snippets.

@harryfinn
Last active May 17, 2022 14:12
Show Gist options
  • Save harryfinn/1d6904c2baf006ad4cf3cdec2704aa70 to your computer and use it in GitHub Desktop.
Save harryfinn/1d6904c2baf006ad4cf3cdec2704aa70 to your computer and use it in GitHub Desktop.
Ubuntu 16.04: LAMP w/ NodeJS & WP-CLI

Ubuntu 16.04 LAMP w/ NodeJS & WP-CLI

Setup LAMP stack

Apache

apt-get update
apt-get install apache2

nano /etc/apache2/mods-enabled/dir.conf

  • Add index.php to beginning of DirectoryIndex list (remove from place in list further down)

a2enmod rewrite headers expires

nano /etc/apache2/apache2.conf

  • Change AllowOverride None to AllowOverride All within <Directory /var/www/> block

service apache2 restart

PHP

apt-get install php libapache2-mod-php php-mcrypt php-mysql -y
apt-get install php-cgi php-cli php-common php-curl php-gd -y

MySQL

apt-get install mysql-server

mysql_secure_installation

  • n
  • n
  • y
  • y
  • y
  • y

Accessing remote MySQL databases

ssh www-data@SERVER_IP_ADDRESS -L 42001:localhost:3306

  • 3306 is default mysql port

  • 42001 is a free (unless you are running something else on this port, highly unlikely!)

  • allows local access to remote mysql server for easier creation of db's and users

NodeJS

curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
apt-get install -y nodejs

Install Yarn

Follow the instructions on the Yarn website to install yarn onto the server.

Git

apt-get install git

Adding sites to Apache

Note: tld means Top-Level Domain i.e. .com, .co.uk, .io etc, make sure to replace .tld with your domain's TLD extension

cd /etc/apache2/sites-available/

  • if creating one site on box:
cp 000-default.conf my-site.domain.tld.conf
nano my-site.domain.tld.conf
ServerName my-site.domain.tld
ServerAdmin harry@harryfinn.co.uk
DocumentRoot /var/www/html/
a2dissite 000-default.conf
a2ensite my-site.domain.tld.conf
  • if creating multiple (i.e. staging and qa):
cp 000-default.conf my-site-staging.domain.tld.conf
nano my-site-staging.domain.tld.conf
ServerName my-site-staging.domain.tld
ServerAdmin harry@harryfinn.co.uk
DocumentRoot /var/www/html/staging
cp 000-default.conf my-site-qa.domain.tld.conf
nano my-site-qa.domain.tld.conf
ServerName my-site-qa.domain.tld
ServerAdmin harry@harryfinn.co.uk
DocumentRoot /var/www/html/qa
a2dissite 000-default.conf
a2ensite my-site-staging.domain.tld.conf my-site-qa.domain.tld.conf
  • Ensure directories specified above (each DocumentRoot path) are created by www-data user, once this user is setup using the instructions below.

Setting up www-data user

nano /etc/passwd

  • www-data:x:33:33:www-data:/var/www:/bin/bash

passwd www-data

  • Enter www-data user password

chown -R www-data:www-data /var/www

Preventing root user from ssh'ing onto server

(As root user)

nano /etc/ssh/sshd_config

  • Change PermitRootLogin yes to PermitRootLogin no

service ssh restart

Disconnect from server and try root user ssh, will be given permission denied message. Use www-data user the su to swap to the root user when required.

Installing WP-CLI

curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar

su

  • swap to root in order to move wp-cli

mv wp-cli.phar /usr/local/bin/wp

exit

  • swap back to www-data

Update server hostname to FQDN

  • Run hostname to see what the currently set hostname is set to
  • Run hostnamectl set-hostname mysite.com to change the hostname (permanently, otherwise will reset on reboot)
  • Note: Important that this step is performed before setting you your mail server!

Setting up mail-out only (loopback) emails from server

apt-get install mailutils

  • Set mail configuration type to Internet Site
  • Set system mail name to FQDN i.e. mysite.com, ensure you DO NOT add any subdomains in front i.e. www. as this will invalidate the mail record
  • As root:
  • nano /etc/postfix/main.cf and change inet_interfaces to:
inet_interfaces = loopback-only
  • Then change mydestination to:
mydestination = localhost.$mydomain, $mydomain
  • Finally, restart postfix (mail sending service) service postfix restart
  • To check that the server is sending out emails correctly you can run the following command:
echo "This is the body of the email" | mail -s "This is the subject line" my_email@emailaddress.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment