Ubuntu Web Server Setup
As an intern at 4site Studios, one of the tasks assigned to me was to learn how to set up Ubuntu servers on Amazon Web Services. After watching a number of tutorials and reading over countless forums, I have created this step by step documentation on how to set up the server.
Prerequisites:
-
An AWS account
Step 1: Launching an EC2 Instance
-
On AWS Management Console launch an EC2 Instance
-
Install Ubuntu Server 16.04 LTS
-
Add security rules for HTTP and HTTPS access and make sure the ports are 80 and 463 respectively
-
In the SSH rule port 22, set the source to your IP address and add a description e.g. “My IP”
Step 2: Configuring and Installing Apache
-
In your terminal run the commands “sudo apt update” and
-
“sudo apt install apache2” in order to install apache
-
Create a folder for the site by running the command “sudo mkdir /var/www/websitename/”
-
Now in order to set up the virtual configuration file, you must go to the configuration file directory by using the command “cd /etc/apache2/sites-available/”
-
To access the virtual configuration file use the command “sudo cp 000-default.conf websitename.conf” then use “sudo nano websitename.conf” to edit it
-
Modify the necessary sections of the document (server administrator, document root, and server name)
-
Now you need to enable the site by activating the virtual host configuration file. Make sure you are in the configuration file directory (Run the command cd /etc/apache2/sites-available/) and run the command “sudo a2ensite websitename.conf”
-
To activate the new configuration, run: “sudo service apache2 reload”
-
Restart apache by running the command “service apache2 reload”
Step 3: Securing your site using Let’s Encrypt
-
Add the repository using the command “sudo add-apt-repository ppa:certbot/certbot”
-
Update the package contents using the command “sudo apt-get update”
-
Install the certbot using the command “sudo apt-get install python-certbot-apache”
-
To obtain a certificate for the domain use the command “sudo certbot –apache -d websitename.com”
Step 4: Redirecting HTTP to HTTPS (Virtual Host Method)
-
Open your site’s virtual host file and add “Redirect / https://www.yourdomain.com/”
-
Beneath the closing tag(</VirtualHost>) for the port 80 configurations add the tag <VirtualHost_default:433> and add the following:
ServerName www.yourdomain.com DocumentRoot /usr/local/apache2/htdocs SSLEngine On # etc...
-
Close the tag then save and close the file
-
Restart the server by running the command “sudo systemctl restart apache2”
Redirecting HTTP to HTTPS (.htaccess Method)
-
Make sure that mod_rewrite is enabled by running the command “sudo a2enmod rewrite”
-
Edit or create the .htaccess file in your domain root directory and add these lines:
RewriteEngine On RewriteCond %{HTTPS} !=on RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
-
Restart the server by running the command “sudo systemctl restart apache2”
Step 5: Installing MySQL
-
To install my MySQL run the following commands “sudo apt-get update” and “sudo apt-get install mysql-server”
-
You will be prompted to create a password, so make one that is secure and that you will remember
-
To configure MySQL, run the command “mysql_secure_installation”
-
You will be asked a series of questions, answer yes to all of them
-
To test if MySQL is running, enter the command “systemctl status mysql.service”
-
If it turns out that it is not running, enter the command “sudo systemctl start mysql”
Step 6: Installing PHP 7.2
-
First update your server running the command “sudo apt-get update && apt-get upgrade”
-
Install the package by entering the command “sudo apt-get install python-software-properties”
-
Add the PHP repository by running the command “sudo add-apt-repository ppa:ondrej/php”
-
Update your package list by running the command “sudo apt-get update”
-
Install PHP 7.2 by running the command “sudo apt-get install php7.2”
-
You may need some other modules, so the most common ones can be installed running the command “sudo apt-get install php-pear php7.2-curl php7.2-dev php7.2-gd php7.2-mbstring php7.2-zip php7.2-mysql php7.2-xml”
Step 7: Installing WordPress
-
Install the package by running the command “wget -c http://wordpress.org/latest.tar.gz”
-
Extract it by running the command “ sudo tar -xzvf latest.tar.gz”
-
Move the file to the apache root directory (/var/www/websitename/) by running the command “sudo rsync -av wordpress/* /var/www/websitename/”
-
To change permissions for the directory use the command “find /var/www/websitename -type d -exec chmod 750 {} ;”
-
To change permissions for the file use the command “find /var/www/websitename -type f -exec chmod 640 {} ;”
Step 8: Creating a WordPress Database
-
Use the command “mysql -u root -p” to open the MySQL shell
-
Create a new user using the command “CREATE USER ‘username’@’localhost’ IDENTIFIED BY ‘password’;”
-
Use the following commands to create the database, plugging in your values where necessary: “CREATE DATABASE databasename;”
-
Next, give the user privileges on the database by typing “GRANT ALL PRIVILEGES ON databasename.* TO ‘username’@’localhost’ IDENTIFIED BY ‘password’;”
-
“FLUSH PRIVILEGES;”
-
“ EXIT;”
-
Go to the /var/www/websitename/ directory and change the name of the wp-config-sample.php file to wp-config.php by using the command “sudo mv wp-config-sample.php wp-config.php”
-
Update the file with your database information under the MySQL settings section
-
Restart the web server and MYSQL using the commands “sudo system1 restart apache2.service” and sudo system1 restart mysql.service”
Resources:
Setting up the Instance: https://medium.com/@jameshamann/setting-up-an-ubuntu-ec2-instance-from-scratch-78a166167a22
https://www.youtube.com/watch?v=v0g1M5bb9u4&authuser=1
How to SSH into the Instance:
https://www.youtube.com/watch?v=4WQe_-DAn1E&authuser=1
Installing Apache:
https://tutorials.ubuntu.com/tutorial/install-and-configure-apache#4
Securing the site:
Installing MySQL:
https://www.digitalocean.com/community/tutorials/how-to-install-mysql-on-ubuntu-16-04
Installing PHP 7.2:
https://thishosting.rocks/install-php-on-ubuntu/
Installing WordPress:
https://www.tecmint.com/install-wordpress-on-ubuntu-16-04-with-lamp/
https://www.techrepublic.com/article/how-to-install-wordpress-on-ubuntu-16-04/
Install the Lamp:
https://www.tecmint.com/install-wordpress-on-ubuntu-16-04-with-lamp/