After create the EC2 set up in Ubuntu, connection the server through ssh connection.
Update and Install Required Packages
Update the system:sudo apt update && sudo apt upgrade -y
Install Apache:
sudo apt install apache2 -y
Install PHP:
sudo apt install php libapache2-mod-php php-mysql -y
Install MySQL:
sudo apt install mysql-server -y
Secure MySQL Installation: Run the MySQL security script:
sudo mysql_secure_installation
Create a MySQL Database for WordPress
Login to MySQL:sudo mysql -u root -p
Create Database and User:
CREATE DATABASE demo_wordpress;
CREATE USER 'demo_wordpress_user'@'localhost' IDENTIFIED BY 'demo_wordpress_password'; GRANT ALL PRIVILEGES ON demo_wordpress.* TO 'demo_wordpress_user'@'localhost';
FLUSH PRIVILEGES; EXIT;
Install and Configure WordPress
Navigate to the web directory:cd /var/www/html
Download WordPress:
sudo wget https://wordpress.org/latest.tar.gz
sudo tar -xvzf latest.tar.gz
sudo mv wordpress/*
sudo rm -rf wordpress latest.tar.gz
Set Permissions:
sudo chown -R www-data:www-data /var/www/html/
sudo chmod -R 755 /var/www/html/
Configure WordPress:
Rename the WordPress config file:
sudo mv wp-config-sample.php wp-config.php
Edit the wp-config.php file to include your MySQL database details:
sudo nano wp-config.php
Add the following lines:
define('DB_NAME', 'wordpress'); define('DB_USER', 'wpuser'); define('DB_PASSWORD', 'your_password'); define('DB_HOST', 'localhost');
Install phpMyAdmin
Install phpMyAdmin:sudo apt install phpmyadmin -y
During the installation, choose Apache2 as the web server and configure phpMyAdmin with the MySQL root password.
Enable phpMyAdmin:
sudo ln -s /usr/share/phpmyadmin /var/www/html/phpmyadmin
Assign an Elastic IP to Your Instance
Update Apache to Work with Elastic IP: Edit the Apache configuration file:sudo nano /etc/apache2/sites-available/000-default.conf
Add your server's Elastic IP:
<VirtualHost *:80>
ServerName your-elastic-ip
</VirtualHost>
Restart Apache:
sudo systemctl restart apache2
Remove index.html
sudo rm index.html
Increase File Upload Size
Locate the php.ini file for PHP 8.3.6, typically located at /etc/php/8.3/apache2/php.ini on Ubuntu.sudo nano /etc/php/8.3/apache2/php.ini
Update the following settings:
upload_max_filesize = 64M post_max_size = 64M max_execution_time = 300 max_input_time = 300 memory_limit = 128MSave and Close the File: Save changes (Ctrl + O), then exit (Ctrl + X). Restart Apache to Apply the Changes:
sudo systemctl restart apache2
Search in nano editor
To search within the Nano editor, you can use the following steps:- Open Nano and Enter Search Mode:
- Press Ctrl + W to start searching.
- Type Your Search Query:
- After pressing Ctrl + W, type the text you want to find and press Enter.
- Move to the Next Match (if any):
- To find the next instance of your search term, press Ctrl + W again and hit Enter without typing anything new.
- Exit Search:
- Continue editing as usual after finding your desired location.
If only the homepage is working and all other pages are returning a 404 error in WordPress
If only the homepage is working and all other pages are returning a 404 error in WordPress, this is likely due to a permalink or .htaccess configuration issue. Here’s how you can troubleshoot and fix it:- Reset Permalinks in WordPress
- Log into Your WordPress Admin Dashboard.
- Go to Settings > Permalinks
- Select Your Preferred Permalink Structure (e.g., Post Name).Click Save Changes to refresh the permalinks.
- Click Save Changes to refresh the permalinks.
sudo nano /var/www/html/.htaccess
Ensure the File Has the Following Basic WordPress Rules:
# BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPressSave and Close the File:
Save the changes (`Ctrl + O`), then exit (`Ctrl + X`).
Set the Correct Permissions on .htaccess:
sudo chown www-data:www-data /var/www/html/.htaccess
sudo chmod 644 /var/www/html/.htaccess
Enable Mod Rewrite in Apache (if not already enabled)
Enable the Rewrite Module:
sudo a2enmod rewrite
Restart Apache to Apply Changes:
sudo systemctl restart apache2
Check Apache Configuration
Open the default site configuration file for Apache:
sudo nano /etc/apache2/sites-available/000-default.conf
Verify That `AllowOverride` is Set to `All`:
Ensure the following line appears in the `<Directory /var/www/html/>` section:
<Directory /var/www/html/> AllowOverride All </Directory>
Save and Restart Apache:
sudo systemctl restart apache2
Setup a Domain or Subdomain in the Server
First backup and zip the all wordpress files from /var/www/html to /var/www Navigate to the /var/www/html directorycd /var/www/html
Zip All Files Inside the html Folder
Install Zip
sudo apt update
sudo apt install zip -y
Use the zip command to compress all files and subdirectories within html into a single zip file:
sudo zip -r ../wordpress_backup.zip ./*
Download the zip file and later will upload the zip file through filezilla
Move the Backup to a Safe Location
To Move to Another Directory:
sudo mv wordpress_backup.zip /path/to/backup/location
To Download Locally Using SCP: Run this command from your local terminal:
scp -i /path/to/your-key.pem ubuntu@your-server-ip:/var/www/wordpress_backup.zip /local/path/to/save
Create the DNS Record for the Subdomain
- Log into your domain registrar’s control panel (e.g., GoDaddy, Namecheap, or AWS Route 53).
- Add an A Record for the subdomain:
- Name: subdomain (replace with your actual subdomain, e.g., blog for blog.yourdomain.com).
- Type: A
- Value: Your server’s IP address.
- TTL: Leave at default (usually 300 seconds).
- Save the DNS changes.
- It may take some time (up to 24 hours) for DNS changes to propagate.
sudo mkdir -p /var/www/subdomain.yourdomain.com
Assign correct permissions for this directory:
sudo chown -R www-data:www-data /var/www/subdomain.yourdomain.com
sudo chmod -R 777 /var/www/subdomain.yourdomain.com
Create a Virtual Host Configuration for the Subdomain in Apache
Create a new virtual host configuration file for the subdomain:
sudo nano /etc/apache2/sites-available/subdomain.yourdomain.com.conf
Add the following configuration (replace subdomain.yourdomain.com and /var/www/subdomain.yourdomain.com with your actual subdomain and directory paths):
<VirtualHost *:80> ServerName subdomain.yourdomain.com DocumentRoot /var/www/subdomain.yourdomain.com <Directory /var/www/subdomain.yourdomain.com> AllowOverride All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/subdomain_error.log CustomLog ${APACHE_LOG_DIR}/subdomain_access.log combined </VirtualHost>Save and close the file (press Ctrl + O to save, then Ctrl + X to exit). Enable the New Virtual Host and Restart Apache Enable the new site with the following command:
sudo a2ensite subdomain.yourdomain.com.conf
Reload or restart Apache to apply the changes:
sudo systemctl reload apache2
Create and Setup Free SSL in Zero SSL
Create and set up a free SSL certificate from ZeroSSL on your server, follow these steps: Prerequisites Ensure you have:- A domain name (e.g., subdomain.yourdomain.com) properly pointed to your server's IP.
- An email account for SSL registration.
- Access to SSH on the server.
- Visit ZeroSSL and create a free account.
- After registering, navigate to the Dashboard.
- Start a New Certificate Order:
- Click on "Create Free SSL Certificate".
- Enter your subdomain (e.g., subdomain.yourdomain.com).
- Continue to the next step.
- Choose Validation Method:
- Select DNS (CNAME) or HTTP File Upload as the validation method.
- DNS (CNAME): You will need to add a CNAME record to your DNS settings.
- HTTP File Upload: You will need to upload a specific file to your web server for verification.
- Complete Domain Verification:
- Follow ZeroSSL’s instructions for the chosen validation method:
- For DNS (CNAME): Add the provided CNAME record in your domain registrar’s DNS settings.
- For HTTP File Upload: Upload the specified file to /var/www/subdomain.yourdomain.com/.well-known/pki-validation/ on your server.
- Once done, click Verify Domain in ZeroSSL.
- Download Certificate Files:
- After verification, download the certificate files from ZeroSSL, which should include:
- certificate.crt
- ca_bundle.crt
- private.key
Configure Apache to Use the SSL Certificate
Transfer Certificate Files to Your Server: Upload the certificate.crt, ca_bundle.crt, and private.key files to your server, ideally to the /etc/ssl/subdomain.yourdomain.com directory:sudo mkdir -p /etc/ssl/subdomain.yourdomain.com
sudo mv certificate.crt /etc/ssl/subdomain.yourdomain.com/
sudo mv ca_bundle.crt /etc/ssl/subdomain.yourdomain.com/
sudo mv private.key /etc/ssl/subdomain.yourdomain.com/
Update Apache Virtual Host Configuration:
Open or create a configuration file for your subdomain (e.g., /etc/apache2/sites-available/subdomain.yourdomain.com-ssl.conf):
sudo nano /etc/apache2/sites-available/subdomain.yourdomain.com-ssl.conf
Add SSL Configuration to the File:
Add the following configuration to enable SSL:
apache
<IfModule mod_ssl.c> <VirtualHost *:443> ServerName subdomain.yourdomain.com DocumentRoot /var/www/subdomain.yourdomain.com SSLEngine on SSLCertificateFile /etc/ssl/subdomain.yourdomain.com/certificate.crt SSLCertificateKeyFile /etc/ssl/subdomain.yourdomain.com/private.key SSLCertificateChainFile /etc/ssl/subdomain.yourdomain.com/ca_bundle.crt <Directory /var/www/subdomain.yourdomain.com> AllowOverride All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/subdomain_error.log CustomLog ${APACHE_LOG_DIR}/subdomain_access.log combined </VirtualHost> </IfModule>Enable the SSL Virtual Host and SSL Module:
sudo a2enmod ssl
sudo a2ensite subdomain.yourdomain.com-ssl.conf
Restart Apache to Apply Changes:
sudo systemctl restart apache2
Redirect HTTP to HTTPS (Optional)
To redirect all HTTP traffic to HTTPS, open the original non-SSL Virtual Host file for your subdomain (e.g., /etc/apache2/sites-available/subdomain.yourdomain.com.conf):
Add the following lines to redirect HTTP to HTTPS:
apache
<VirtualHost *:80> ServerName subdomain.yourdomain.com DocumentRoot /var/www/subdomain.yourdomain.com RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] </VirtualHost>Save the file and restart Apache:
sudo systemctl restart apache2