WordPress LAMP setup on Digital Ocean
This is based on this article from Digital Ocean but modified to include, and addresses some .htaccess, directory structure issues as well as add support for FTP and phpMyAdmin. We also presume you have an existing domain registered somewhere that you want to point to your digital ocean server and you have an existing WordPress site you want to migrate.
Summary of steps:
- Initial server setup
- LAMP stack setup
- Install phpMyAdmin
- Set Up Local Hosts File for testing domain (windows)
- Create virtual host
- Configure files/permissions
- Install vsftp
- Update Wordpress wp-config and database
- Point host record
Initial server setup
Create a droplet, select Ubuntu 14.04.5 x32 512mb. We don’t need x64 as they’ve stated: “A 32-bit operating system is recommended for cloud servers with less than 3 GB of RAM”. If you wish to use ssh keys, do the below step, if not just ignore it.
We can use these as placeholder settings:
- IP Address: <-serveripaddress->
- Username: <-newusername->
- Password: <-userpassword->
- Existing Domain: <-domainname->
- Existing wp database: <-wpdatabase->
- Existing wp database user: <-wpdbusere->
- MySQL Root Password: <-mysqlpass->
- phpMyAdmin Password: <-phpmyadminpass->
Generate ssh key (optional)
Using command prompt as admin:
cd ~/.ssh
You might need to make the .ssh directory in your user directory if none exists then generate the keys:
ssh-keygen -C "<-domainname->"
Hit enter a few times to genenraterate key then copy the entire contents of id_rsa.pub to clipboard and past in the new ssh key box when creating a the droplet.
Locally Connect and add a user
ssh root@<-serveripaddress->
Create new user:
adduser <-newusername->
Enter a password and hit enter a few times. Add the user to the sudo group:
gpasswd -a <-newusername-> sudo
Disable remote login to the root account from this user (for security)
nano /etc/ssh/sshd_config
change this:
PermitRootLogin yes
to this:
PermitRootLogin no
To save hit CTRL-X, then Y, then ENTER. Restart ssh
service ssh restart
Test the connection in a new terminal window
ssh benc@<-serveripaddress->
To run commands from now on you use: sudo command_to_run
LAMP stack setup
Update links:
sudo apt-get update
Install apache:
sudo apt-get install apache2
Test by putting <-serveripaddress-> in the browser.
Install MySQL
sudo apt-get install mysql-server libapache2-mod-auth-mysql php5-mysql
Enter your desired MySQL root user passoword then activate MySQL:
sudo mysql_install_db
Run the secure installation setup script:
sudo /usr/bin/mysql_secure_installation
Enter your newly created password:
<-mysqlpass->
Type no then yes to the rest of the settings.
Install PHP
sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt
Add php to the directory index, to serve the relevant php index files:
sudo nano /etc/apache2/mods-enabled/dir.conf
Add index.php to the beginning of index files. The page should now look like this:
<IfModule mod_dir.c>
DirectoryIndex index.php index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>
Install recommended modules for wordpress:
sudo apt-get install php5-fpm php5-mysqlnd php5-imagick php5-curl
If more are desired you can list available modules:
apt-cache search php5-
preveiw our stack through a php page
Create a new file:
sudo nano /var/www/html/info.php
Add:
<?php phpinfo(); ?>
Save it and restart apache:
sudo service apache2 restart
Test in browser:
http://<-serveripaddress->/info.php
install phpMyAdmin
sudo apt-get install phpmyadmin
Important -> When the first prompt appears, apache2 is highlighted, but not selected. If you do not hit “SPACE” to select Apache, the installer will not move the necessary files during installation. Hit “SPACE”, “TAB”, and then “ENTER” to select Apache. Choose yes to all. Enter a phpmyadmin pass just for phpMyAdmin to use.
<-phpmyadminpass->
Enable this extension:
sudo php5enmod mcrypt
Restart apache:
sudo service apache2 restart
To test access:
http://<-serveripaddress->/phpmyadmin
Set Up Local Hosts File
This is for testing the domain from your local machine before you point it to your new setup. Once your setup is all done, we’ll remove this. Note that this is on Windows, but Mac/linux I hear is just as easy.
Altering the hosts file
Make a copy of the hosts file, so that you can restore it later if you want to:
copy C:\Windows\System32\drivers\etc\hosts C:\Windows\Temp
Open the hosts file in Notepad so you can edit it:
notepad C:\Windows\System32\drivers\etc\hosts
Put this in and save it:
<-serveripaddress-> <-domainname-> #testing <-domainname-> locally
Create Virtual Host
Back in your server shell, add a virtual host file in sites available dir:
sudo nano /etc/apache2/sites-available/<-domainname->.conf
Add this to the file:
<VirtualHost *:80>
ServerAdmin <-youremailaddress->
ServerName <-domainname->
ServerAlias www.<-domainname->
DocumentRoot /var/www/<-domainname->/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /var/www/<-domainname->/public_html>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Note:
Options -Indexes +FollowSymLinks +MultiViews
Prevents the browser from seeing/navigating your site file structure, and:
AllowOverride All
allows .htaccess files to work in your virtual host (And sub-directories). Next we enable the virtual host by telling apache to listen on that domain (Apache utility - a2ensite):
sudo a2ensite benchung.com
Configure mod rewrite:
sudo a2enmod rewrite
Restart apache:
sudo service apache2 restart
Configure files/permissions
Make site dir:
sudo mkdir -p /var/www/<-domainname->/public_html
Add symlink to user’s home folder to get to the web folder:
sudo ln -s /var/www/benchung.com/public_html /home/benc/benchung.com
Change permissions in the site files dir:
sudo chown <-newusername->:www-data /var/www/<-domainname->/public_html
Install bindfs for ease of permission issues:
sudo apt-get -y install bindfs
Create the mount point to mirror in the same location so user www-data and benc will both see itself as the owner of the files this way WordPress can write to it as well as the user (using FTP) without conflicts. (create-with-perms=0644:a+X) will set files to 644, and folders to 755 when they get created. (force-group) makes the group www-data no matter what:
sudo bindfs -o perms=0644:a+X,mirror=<-newusername->:www-data,force-group=www-data /var/www/<-domainname->/public_html /var/www/<-domainname->/public_html
To remove mount if made a mistake:
sudo umount /home/<-newusername->/sites/<-domainname->
Create test page:
sudo nano /var/www/benchung.com/public_html/index.html
Paste in:
<html>
<head>
<title>Welcome to Example.com!</title>
</head>
<body>
<h1>Success! The example.com virtual host is working!</h1>
</body>
</html>
Install vsftp
sudo apt-get install vsftpd
Open up the configuration file:
sudo nano /etc/vsftpd.conf
Change to (don’t enable public accesss to files):
anonymous_enable=NO
Uncomment these to enable (chroot makes sure all local users are denied access to other server parts):
local_enable=YES
write_enable=YES
chroot_local_user=YES
allow_writeable_chroot=YES
To prevent the recent vsftpd upgrade error: “refusing to run with writable root inside chroot”. Do this:
sudo nano /etc/vsftpd.conf
Add this to the file:
allow_writeable_chroot=YES
systemctl restart vsftpd
Restart vsftp:
sudo service vsftpd restart
Test connection:
ftp 159.203.59.15
user
<-newusername->
<-userpassword->
ls
Upload your files fia FTP to the html dir. If you need to import an existing WordPress database, put it in th html dir in a folder called DB. If needed, change permissions for all the uploaded files and directories:
sudo find /var/www/<-domainname->/public_html -type f -exec chmod 644 {} +
sudo find /var/www/<-domainname->/public_html -type d -exec chmod 755 {} +
Restore the database
Enter MySQL:
mysql -u root -p
Enter your MySQL password then create a user:
CREATE USER '<-wpdbuser->'@'<-serveripaddress->' IDENTIFIED BY '<-wpdbuserpass->';
Check:
SELECT User FROM mysql.user;
Create database:
CREATE DATABASE <-wpdatabase->;
Select the database:
USE <-wpdatabase->;
Import schema from our uploaded folder:
SOURCE /var/www/<-domainname->/public_html/DB/<-wpdatabase->.sql
Update the site url if needed. First find the option id for the site url:
SELECT * FROM wp_options WHERE option_value = 'http://<-yourolddomain->';
Find the option_id’s (probably 1 & 2), then for each one:
UPDATE wp_options SET option_value = 'http://<-domainname->' WHERE option_id = <-theoptionid->;
Exit MySQL
exit
Update wp-config
Change to these settings wp-config.php if needed:
define('DB_NAME', '<-wpdatabase->');
define('DB_USER', '<-wpdbuser->');
define('DB_PASSWORD', '<-wpdbuserpass->');
define('DB_HOST', 'localhost');
Last steps
Point host record
Point your ‘A’ host record to <-serveripaddress-> then set the TTL to 1000 to speed up propogation time (once propogated you can set it back to 14400).
Restore Hosts File
You can now restore your hosts file on your local machine by removing the line we added in the “Set Up Local Hosts File” step.