WordPress is a popular, open-source content management system (CMS) that allows users to create, customize, and manage content on their website. A CMS provides the basic infrastructure to build a website, which is useful for users who may not have the knowledge to build and code their own website from scratch. The WordPress CMS has many customization tools, such as an administrative dashboard with a user-friendly interface to create new web pages, add media, and more. For these reasons, WordPress is one of the most used CMS in the market today
Log into your server as your sudo user and continue to the first step.
Install Linux, Apache, MySQL, and PHP (LAMP) Stack on Ubuntu.
The LAMP stack (Linux, Apache, MySQL, and PHP) is a popular open-source software bundle used for web hosting and application development. This guide will walk you through installing and configuring LAMP on an Ubuntu system.
If you want to install WordPress using automatic script which I made using ChatGPT then follow the below steps:
- Login to server.
- Create a file. [nano install_wordpress.sh]
- Download/Open the script from here and copy paste the entire content.
- Give permission. [chmod +x install_wordpress.sh]
- Run the script and follow the prompts. [sudo ./install_wordpress.sh]
If you want to install WordPress manually using command line then follow the below steps:
Step 1: Update Your System
sudo apt update && sudo apt upgrade -y
Step 2: Install Apache Web Server
sudo apt install apache2 -y
Start and enable Apache:
sudo systemctl start apache2
sudo systemctl enable apache2
Verify Apache is running:
sudo systemctl status apache2
Test Apache by opening a browser and navigating to:
http://your-server-ip
You should see the Apache default welcome page.
Step 3: Install MySQL Database Server
sudo apt install mysql-server -y
When the installation is finished, it’s recommended that you run a security script that comes pre-installed with MySQL. This script will remove some insecure default settings and lock down access to your database system.
Secure MySQL installation:
sudo mysql_secure_installation
Follow the prompts to:
Set a root password
Remove anonymous users
Disable remote root login
Remove test databases
Start and enable MySQL:
sudo systemctl start mysql
sudo systemctl enable mysql
Verify MySQL is running:
sudo systemctl status mysql
Step 4: Install PHP
PHP is the scripting language used to generate dynamic web pages.
Install PHP along with common extensions:
sudo apt install php libapache2-mod-php php-mysql php-cli php-curl php-gd php-mbstring php-xml php-zip -y
Verify PHP installation:
php -v
You have successfully installed the LAMP stack on Ubuntu. You can now start deploying your PHP-based applications using Apache and MySQL.
Changing Apache’s Directory Index (Optional)
In some cases, you’ll want to modify the way that Apache serves files when a directory is requested. Currently, if a user requests a directory from the server, Apache will first look for a file called index.html. We want to tell the web server to prefer PHP files over others, to make Apache look for an index.php file first. If you don’t do that, an index.html file placed in the document root of the application will always take precedence over an index.php file.
To make this change, open the dir.conf configuration file in a text editor of your choice. Here, we’ll use nano:
sudo nano /etc/apache2/mods-enabled/dir.conf
It will look like this:
<IfModule mod_dir.c>
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>
Move the PHP index file (highlighted above) to the first position after the DirectoryIndex specification, like this:
<IfModule mod_dir.c>
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>
When you are finished, save and close the file by pressing CTRL+X. Confirm the save by typing Y and then hit ENTER to verify the file save location.
After this, restart the Apache web server in order for your changes to be recognized. You can do that with the following command:
sudo systemctl restart apache2
Step 5: Creating a Virtual Host for your Website:
When using the Apache web server, you can create virtual hosts to encapsulate configuration details and host more than one domain from a single server. In this guide, we’ll set up a domain called your_domain, but you should replace this with your own domain name.
Apache on Ubuntu has one virtual host enabled by default that is configured to serve documents from the /var/www/html directory. While this works well for a single site, it can become unwieldy if you are hosting multiple sites. Instead of modifying /var/www/html, we’ll create a directory structure within /var/www for the your_domain site, leaving /var/www/html in place as the default directory to be served if a client request doesn’t match any other sites.
Create the directory for your_domain as follows:
sudo mkdir -p /var/www/html/chess.mdlab.co.in/public_html
Next, assign ownership of the directory with the $USER environment variable, which will reference your current system user:
sudo chown -R $USER:$USER /var/www/html/chess.mdlab.co.in/public_html
Then, open a new configuration file in Apache’s sites-available directory using your preferred command-line editor. Here, we’ll use nano:
sudo nano /etc/apache2/sites-available/your_domain.conf
This will create a new blank file. Add in the following bare-bones configuration with your own domain name:
<VirtualHost *:80>
ServerName your_domain
ServerAlias www.your_domain
ServerAdmin webmaster@localhost
DocumentRoot /var/www/your_domain
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
For example, it will look like this:
<VirtualHost *:80>
ServerAdmin admin@chess.mdlab.co.in
ServerName chess.mdlab.co.in
ServerAlias www.chess.mdlab.co.in
DocumentRoot /var/www/html/chess.mdlab.co.in/public_html
ErrorLog ${APACHE_LOG_DIR}/chess_error.log
CustomLog ${APACHE_LOG_DIR}/chess_access.log combined
</VirtualHost>
Save and close the file when you’re done. If you’re using nano, do that by pressing CTRL+X, then Y and ENTER.
With this VirtualHost configuration, we’re telling Apache to serve your_domain using /var/www/your_domain as the web root directory.
Now, use a2ensite to enable the new virtual host:
sudo a2ensite your_domain
You might want to disable the default website that comes installed with Apache. This is required if you’re not using a custom domain name, because in this case Apache’s default configuration would override your virtual host. To disable Apache’s default website, type:
sudo a2dissite 000-default
To make sure your configuration file doesn’t contain syntax errors, run the following command:
sudo apache2ctl configtest
Finally, reload Apache so these changes take effect:
sudo systemctl reload apache2
Step 6:Installing WordPress:
WordPress uses MySQL to manage and store site and user information. You have MySQL installed already, but need to make a database and a user for WordPress to use.
To get started, log into the MySQL root (administrative) account by issuing the following command (note that this is not the root user of your server):
mysql -u root -p
Within the database, create a dedicated database for WordPress to control. You can call this whatever you would like, but we will be using the name wordpress in this guide. Create the database for WordPress by running the following command:
CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
We will use the name wordpressuser in this guide, but feel free to use any name you prefer for this use.
You can create this user by running the following command. Remember to choose a strong password here for your database user where we have password:
CREATE USER 'wordpressuser'@'%' IDENTIFIED WITH mysql_native_password BY 'password';
Next, let the database know that your wordpressuser should have complete access to the database you set up
GRANT ALL ON wordpress.* TO 'wordpressuser'@'%';
You now have a database and user account, each made specifically for WordPress. You need to flush the privileges so that the current instance of MySQL knows about the recent changes made:
FLUSH PRIVILEGES;
Exit out of MySQL by writing the following:
EXIT;
Installing Additional PHP Extensions:
sudo apt install php-curl php-gd php-mbstring php-xml php-xmlrpc php-soap php-intl php-zip
This will lay the groundwork for installing additional plugins on your WordPress site.
You’ll need to restart Apache to load these new extensions.
sudo systemctl restart apache2
Enabling .htaccess Overrides:
Currently, the use of .htaccess files is disabled. WordPress and many WordPress plugins use these files extensively for in-directory tweaks to the web server’s behavior.
Open the Apache configuration file for your website with your preferred text editor. Here, we’ll use nano:
sudo nano /etc/apache2/sites-available/wordpress.conf
To allow .htaccess files, you need to set the AllowOverride directive within a Directory block pointing to your document root. Add the following content inside the VirtualHost block in your configuration file, making sure to use the correct web root directory:
<VirtualHost *:80>
. . .
<Directory /var/www/wordpress/>
AllowOverride All
</Directory>
. . .
</VirtualHost>
When you are finished, save and close the file. In nano, you can do this by pressing CTRL and X together, then Y, and ENTER.
Enabling the Rewrite Module:
Next, you can enable mod_rewrite so that you can use the WordPress permalink feature:
sudo a2enmod rewrite
This allows you to have more human-readable permalinks to your posts, like the following two examples:
http://example.com/2012/post-name/
http://example.com/2012/12/30/post-name
Enabling the Changes:
Before implementing the changes you’ve made, check to make sure you haven’t made any syntax errors by running the following test:
sudo apache2ctl configtest
If all okay then you will get something like this "Syntax OK"
Restart Apache to implement the changes. Make sure to restart now even if you have restarted earlier in this tutorial:
sudo systemctl restart apache2
Downloading WordPress:
Now that your server software is configured, you can download and set up WordPress. For security reasons, it is always recommended to get the latest version of WordPress from their site.
First, change into a writable directory (we recommend a temporary one like /tmp):
cd /tmp
Then download the compressed release with the following curl command:
curl -O https://wordpress.org/latest.tar.gz
Extract the compressed file to create the WordPress directory structure:
tar xzvf latest.tar.gz
You’ll be moving these files into your document root momentarily. Before doing so, you can add a dummy .htaccess file so that this will be available for WordPress to use later.
Create the file by running the following:
touch /tmp/wordpress/.htaccess
You’ll also copy over the sample configuration file to the filename that WordPress reads:
cp /tmp/wordpress/wp-config-sample.php /tmp/wordpress/wp-config.php
Additionally, create the upgrade directory so that WordPress won’t run into permissions issues when trying to do this on its own following an update to its software:
mkdir /tmp/wordpress/wp-content/upgrade
Now you can copy the entire contents of the directory into your document root. We are using a dot at the end of our source directory to indicate that everything within the directory should be copied, including hidden files (like the .htaccess file we created). Ensure that you replace the /var/www/wordpress directory with the directory you have set up on your server:
sudo cp -a /tmp/wordpress/. /var/www/wordpress
You’re now ready to configure your WordPress directory.
Before starting the web-based WordPress setup, you need to adjust some items in your WordPress directory.
Start by giving ownership of all the files to the www-data user and group. This is the user that the Apache web server runs as, and Apache will need to be able to read and write WordPress files in order to serve the website and perform automatic updates.
Update the ownership with the chown command which allows you to modify file ownership. Be sure to point to your server’s relevant directory:
sudo chown -R www-data:www-data /var/www/wordpress
Next, run two find commands to set the correct permissions on the WordPress directories and files. This first find command sets every directory within the /var/www/<>^wordpress<^> directory and sets each one’s permissions to 750:
sudo find /var/www/wordpress/ -type d -exec chmod 750 {} \;
This one finds each file within the directory and sets their permissions to 640:
sudo find /var/www/wordpress/ -type f -exec chmod 640 {} \;
Now, you need to make some changes to the main WordPress configuration file.
When you open the file, your first task will be to adjust some secret keys to provide a level of security for your installation. WordPress provides a secure generator for these values so that you do not have to try to come up with good values on your own. These are only used internally, so it won’t hurt usability to have complex, secure values here.
To grab secure values from the WordPress secret key generator, run the following:
curl -s https://api.wordpress.org/secret-key/1.1/salt/
You will receive unique values that resemble output similar to the following:
Warning! It is important that you request unique values each time. Do NOT copy the following example values!
Put an image here
These are configuration lines that you can place directly into your configuration file to set secure keys. Copy the output you received now.
Next, open the WordPress configuration file:
sudo nano /var/www/wordpress/wp-config.php
Find the section that contains the example values for those settings:
Put an image here
Delete those lines and insert the values you copied from the command line:
Put an image here
Next, you’re going to modify some of the database connection settings at the beginning of the wp-config.php file. You need to adjust the database name, the database user, and the associated password that you configured within MySQL.
The other change you need to make is to set the method that WordPress should use to write to the filesystem.
This setting can be added below the database connection settings, or anywhere else in the file:
define('FS_METHOD', 'direct');
Save and close the file when you are finished.
Now that the server configuration is complete, you can complete the installation through the web interface.
In your web browser, navigate to your server’s domain name or public IP address:
https://server_domain_or_IP
You will be prompted to select the language you would like to use:
Next, you will come to the main setup page.
Select a name for your WordPress site and choose a username. It is recommended to choose something unique and avoid common usernames like “admin” for security purposes. A strong password is generated automatically. Save this password or select an alternative strong password.
Enter your email address and select whether you want to discourage search engines from indexing your site:
When you click ahead, you will be taken to a page that prompts you to log in:
Once you log in, you will be taken to the WordPress administration dashboard:
Congratulations, WordPress is now installed and is ready to be used.