How to Install LAMP + WordPress on Ubuntu 20 AWS with HTTPS

How to Install LAMP + WordPress on Ubuntu 20 AWS with HTTPS

If you have a server with clean Ubuntu 20 or Ubunto 18 OS and want to setup your WordPress website there, then this article is for you. You can setup it if you have AWS EC2 instance. You need to setup SSH key and ready it to access your server.install lamp wordpress ubuntu 20 aws instance linode https Access server via ssh, and run command:
sudo apt update -y
Then run
sudo apt upgrade -y
Then install apache web server
sudo apt install apache2 -y
After installing apache, check either it is working or not, so run this command
sudo systemctl status apache2
it will show you status active (running)… you can further verify by adding your ip address in web browser, it will show you apache2 Ubuntu default html file. e.g http://192.168.1.1 (Note: use your public ip address for the server instead of this) if your apache service is not running, you can run it by using this command
sudo systemctl start apache2
In case, you want that whenever you reboot server, then apache web server should start automatically Then you need to run this command:
sudo systemctl enable apache2
Now we need to install MariaDB server, so to install complete package including MariaDB Client, use this command:
sudo apt install mariadb-server mariadb-client -y
Now we need to start MariaDB server to configure it, here is the command:
sudo systemctl start mariadb
To check the status, run this command
sudo systemctl status mariadb
Now we need to secure our database and configure it with root password, so commands are:
sudo mysql_secure_installation
It will ask root password, as we have not setup any password, so just press ENTER without any entry Set root password? [Y/n] Y New password: Re-enter new password: …Success Remove anonymous users? Y Disallow root login remotely? Y Remove test database and access to it? Y Reload privilege tables now? Y Now we need to restart our MariaDB services. So we need to run this command:
sudo systemctl restart mariadb
Now we need to install php packages in our server.
sudo apt install php php-mysql php-cli php-gd php-common -y
Now we need to install wget and unzip, so we use the command:
sudo apt install wget unzip -y
Now we need to download latest wordpress files, this command will download latest version of wordpress: sudo wget https://wordpress.org/latest.zip We can verify this using “ls” command Unzip it
sudo unzip latest.zip
We will get a wordpress directory that we can see using ls command Now move all files to /var/www/html/
sudo cp -r wordpress/* /var/www/html/
check all files, run command:
cd /var/www/html
then run ls Now we need to change ownership of directories as by default, it has root ownership Run this command to change owner/group from root to www-data (apache default user)
sudo chown -R www-data:www-data /var/www/html/
check using ls –l again, you will see www-data as owner of all directories and files now, we need to delete apache default index.html file
sudo rm -rf index.html
Now if you will open IP address in web browser, it will show wordpress installation page instead of apache2 Ubuntu default page. Now we need to create database, user and grant all privilege to it.
sudo mysql -u root -p
use the password that you have used while securing database in above step. After using root password, you will see console like this: MariaDB [(none)]> Now create database using following command
create database wp_db1;
(you can use your own database name instead of wp_db1) Now create database user and password using this command:
create user "wp_usr1"@"%" identified by password "oO2>hA2)zI4,yN2)";
(You can generate strong password from https://strongpasswordgenerator.com/) Now we need to grant all privileges by using this command
grant all privileges on wp_db1.* to "wp_usr1"@"%";
Then use exit to goodbye MariaDB for now. Now, if we need to use our domain, then we need to setup virtual host for our domain name. Make sure that your domain is pointing to server IP address. Use this command:
sudo nano /etc/apache2/sites-available/yourdomain.com.conf
(change yourdomain.com with your own domain name) Add following code there:


    ServerName yourdomain.com

    ServerAlias www.yourdomain.com

    ServerAdmin name@yourmail.com

    DocumentRoot /var/www/html

    ErrorLog ${APACHE_LOG_DIR}/yourdomain.com-error.log

    CustomLog ${APACHE_LOG_DIR}/yourdomain.com-access.log combined





        Options -Indexes +FollowSymLinks

        AllowOverride All

    
Save the file by using Ctrl+X and then hit Y and Enter Now run this:
sudo a2ensite yourdomain.com
sudo apachectl configtest
sudo systemctl reload apache2
sudo systemctl restart apache2
Now you can install wordpress by going to address: http://yourdomain.com use the database name, username and password that you have created above and run installation. All will be done! To configure ssl: (Other than Lets Encrypt) Copy the Cert File contents, Private Key file contents Create file using:
sudo nano /etc/ssl/certs/yourdomain-com.pem
and paste your cert file contents there then:
sudo su
(you will be root now) Run this command:
sudo nano /etc/ssl/private/privkey.pem
and paste then contents of your Private Key file there (save both files using Ctrl+X and Y, then Enter) Then exit from root using: exit Now run this command
sudo nano /etc/apache2/sites-available/default-ssl.conf
Find the row SSLCertificateFile and set path /etc/ssl/certs/yourdomain-com.pem Find SSLCertificateKeyFile and set path /etc/ssl/private/privkey.pem Go below in file and uncomment (remove #) from line SSLCACertificatePath Press Ctrl+X, then type Y and hit Enter Now need to enable default ssl, command will be:
sudo a2ensite default-ssl
sudo systemctl reload apache2
sudo a2enmod ssl
sudo systemctl restart apache2
if you want to redirect all http traffic to https, then
sudo nano /etc/apache2/sites-available/yourdomain.com.conf
add these lines just below DocumentRoot /var/www/html
RewriteEngine On

    RewriteCond %{HTTPS} off

    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Then restart apache
sudo systemctl restart apache2
and You are done. For installing Let’s Encrypt:
sudo apt update && sudo apt install certbot python-certbot-apache
If you see any erro, then run:
sudo apt install certbot python3-certbot-apache
Now time to get SSL:
sudo certbot --apache
Now everything is setup, open wordpress link and install your wordpress.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.