How To Install WordPress With LAMP/LEMP In Ubuntu 18.04


(Rishi) #1

Hi :slightly_smiling_face:

As we know, serverpilot no more offer free plan and vesta control panel may not be satisfactory for some. In case you may want to try the best alternative of serverpilot. Although there are many free control panels available there but self-configured LAMP/LEMP may prove best for those who want 100% control on the server.


This is tutorial on how to properly configure LAMP (Linux, Apache2, PHP and MySQL/MariaDB) or LEMP (Linux, Nginx, PHP and MySQL/MariaDB) in Ubuntu 18.04 server and install WordPress on it.

This tutorial is divided into two parts, the first part deals with LAMP and the second one with LEMP stack.



Step1. Install Apache 2

First of all, create Ubuntu 16+ server at a suitable location ( location of targetted traffic is recommended).

For security reasons, I would suggest disabling password authentication to the server. Create a SSH key using putty or terminal and use it to access server.

  • So access server using SSH and install apache2:-
sudo -s 
apt-get update
apt-get upgrade

apt-get install apache2 

  • Then grant necessary permissions to /var/www/html (this is the location where we will store WordPress or any site).
chown -R www-data:www-data /var/www/
sudo chmod -R 755 /var/www 

  • After that restart apache using:-
systemctl stop apache2
service apache2 restart

Step2. Configure Firewall

Now we had successfully installed apache2, just we need to configure the firewall (ufw) to allow HTTP (port 80) and HTTPS (port 443) traffic.

ufw enable

ufw allow ssh
ufw app info "Apache Full"
ufw allow in "Apache Full"
ufw status

service apache2 restart

  • To check if apache2 is working properly, open browser and paste IP address of the server. It must show the apache2 Ubuntu default page.

Step3. Install PHP

No its time to install PHP with all necessary modules to support WordPress CMS. Use below command for the latest version of PHP (currently 7.0).

apt-get install php libapache2-mod-php php-mysql php-cli php-cgi php-gd php-mbstring php-json php-xml php-xmlrpc php-zip php-pear

Step 4. Configure Sequence Of Processing

By default, apache2 looks for index.html in any directory then index.php. But here in the case of WordPress, we must look for index.php first. So open configuration file and paste the code below.

nano /etc/apache2/mods-enabled/dir.conf

<IfModule mod_dir.c>
    DirectoryIndex index.php index.html index.cgi index.xhtml index.htm

Use ctrl+o to write out and ctrl+x to exit.

Step 5. Test PHP

Let us test that Apache can process PHP or not. Run below command to create a simple PHP file.

echo "<?php phpinfo(); ?>" >> /var/www/html/info.php

Now open http://ip-address-of-server/info.php and you must see default PHP info page.

Step 6. Install MySQL

MySQL is a structured query language based database management system. WordPress uses MySQL to store all necessary information.

Install MySQL in Ubuntu using below command. At end of Mysql installation, choose a strong root password. Don’t forget to note down MySQL root password.

apt-get install mysql-client mysql-server

Step 7. Secure MySQL

MySQL offers a secure installation plugin to enhance security. We can run that plugin using the command:-


After starting the secure installation plugin, enter root pass. Then select the security level and remove all unnecessary defaults as shown in the image below.

Step 8. Create MySQL Database And User For WordPress

Now its turn to create a database for WordPress. We will use these details while installing WordPress (WordPress stores database related information in Wp-config.php file).

  • Access mysql using root user and root password.

mysql -u root -p 
  • Use the command below to create database and user.

create database database-name-here;
CREATE USER user-name-here@localhost IDENTIFIED BY 'password-here';
GRANT ALL PRIVILEGES ON databse-name-here.* TO user-name-here@localhost;

For example, suppose database name is “myblog1”, username is “myuser1” and password is ‘Secure2018#’, then we use the following command.

create database myblog1;
CREATE USER myuser1@localhost IDENTIFIED BY 'Secure2018#';
GRANT ALL PRIVILEGES ON myblog1.* TO myuser1@localhost;

Step 9. Install WordPress

At this point, LAMP stack is active on your server, so its time to install WordPress.

cd /var/www/html

apt-get install unzip

cd wordpress
mv * .[^.]* ..

cd /var/www/html
rmdir wordpress
rm index.html 
rm info.php

apt-get update
service apache2 restart
systemctl restart mysql

After running above commands, open browser and navigate to IP-address of the server. You will see WordPress database configuration window (hit Let’s Go button).

  • In the next window, enter database details (created in step 8).

  • Then proceed to next step and install WordPress :wink: All done!

Step 10. Connecting Domain To The Server

In apache, domain names are handled by concept of virtual hosts. So let us create a virtual hosts file (also delete default one).

  • Run below command to delete default configurations:-
rm /etc/apache2/sites-available/000-default.conf
  • Create new virtual hosts let it be example.conf (you can use your own domain name).
nano /etc/apache2/sites-available/example.conf
  • Paste below code in example.conf with following changes:-

1. Replace your domain name in place of example (at line 2, 3 and 4).
2. Replace your own wordpress directory (at line 5 and 6).

<VirtualHost *:80>
        DocumentRoot /var/www/html

        <Directory /var/www/html/>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

  • Activate virtual hosts using command:-
a2ensite example.conf
  • After that, Go to Cloudflare and add one A record and one CNAME pointing towards IP-address of the server. Then go to WordPress admin >> general settings and update the site URL.

Step 11. Configuring SSL

You can secure server using let’s encrypt free SSL. Check out tutorials here:-

Step 12. Adding New Domain

Domains in apache2 are handled by the concept of virtual hosts. To add new domain just create a new directory and new virtual host file. For more details check out:-

Step 13. Creating Swap Memory

Swap memory is a virtual memory in the hard disk of the server. It is something like ReadyBoost feature of windows. If you are using a server with RAM under 1GB, it is recommended to create a swap file to avoid accidental server crash.

Check out this tutorial to create swap memory:-

Step 14. Securing LAMP

Security of LAMP stack can be improved by some tweaks. You can checkout this guide for more information:-

Step 15. Install PHPMyAdmin

PHPMyAdmin is very useful to edit MySQL databases. It is a free and opensource tool. To install it, use command below. During installation, installer will ask server type, select Apache and proceed.

apt-get install phpmyadmin

sudo ln -s /usr/share/phpmyadmin /var/www/html

You can also try alternative method here:-


Step 16. Install Nginx Web Server

Nginx is a web server designed for modern browsers. It is fast, efficient and consumes comparatively fewer resources.

  • Login to the server using putty and install Nginx.

sudo -s
apt-get update

apt-get install nginx

chown -R www-data:www-data /var/www/
sudo chmod -R 755 /var/www 

service nginx status

  • Enable firewall and allow web traffic.
ufw enable
ufw allow ssh
ufw app info "Nginx Full"
ufw allow 'Nginx Full'
ufw status

service nginx restart

  • Type IP-address of the server in browser and Nginx default page will appear.


Step 17. Install PHP-FPM

Nginx can process PHP files efficiently using PHP-FPM (PHP FastCGI Process Manager).

  • Install PHP-FPM using the command:-
apt-get install php-fpm php-mysql php-curl php-mbstring php-gd php-json php-xml php-xmlrpc php-cgi php-zip php-pear -y

You can checkout version of PHP by using below command. Note down php-fpm version, it will help in upcoming steps.

ls /etc/php/

Step 18. Secure PHP-FPM

By default, php-fpm allow executing php files which even don’t exist in the server. It can be disabled by editing the php.ini file (located at /etc/php/7.2/fpm/php.ini).

You can do it manually by setting cgi.fix_pathinfo=0 in the php.ini file. Or you can run below command to do it automatically.

sed -i 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g' /etc/php/7.2/fpm/php.ini
systemctl restart php7.2-fpm.service

Step 19. Install And Secure MySQL

Please Follow step6, step7 and step8 mentioned above.

Step 20. Configure Nginx To Process PHP Files

By default, Nginx can’t process dynamic files like PHP. To do this, you have to create a server block and specify how to process php.

Server block is a very important file, because it contains domain information, caching information, security details, file processing sequence and root directory.

  • First of all, delete the default server block.

rm /etc/nginx/sites-available/default
rm /etc/nginx/sites-enabled/default
  • Then create new server block at /etc/nginx/sites-available. Let name of server block is example (you can use name of your own domain).
cd /etc/nginx/sites-available/
nano example
  • In server block, paste configuration below with necessary changes:-
  1. Update the root directory folder (at third line).
  2. Update domain name (at fifth line). Alternatively, you can also use localhost as a domain name to run WordPress directly using IP-address of server.
  3. Update php-fpm version (at 13th line).
server {
  listen 80;

  root /var/www/html;
  index index.php index.html index.htm;

  client_max_body_size 0;

  error_page 404 /404.html;
  error_page 500 502 503 504 /50x.html;

location / {
    try_files $uri $uri/ /index.php?q=$uri&$args;

  location ~* \.php$ {
    if ($uri !~ "^/uploads/") {
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
    include         fastcgi_params;
    fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
location = /favicon.ico { 
    log_not_found off;
    access_log off;
  location = /robots.txt {
    log_not_found off;
    access_log off;
    allow all; 
  location ~* .(css|gif|ico|jpeg|jpg|js|png)$ {
    expires 30d;
    log_not_found off;

Press ctrl+o to save and ctrl+x to exit

  • Activate sever block:-

To activate server block, you need to create symbolic links between two directories (/etc/nginx/sites-available/ and /etc/nginx/sites-enable). A symbolic link is nothing, just interlinking of two folders so that the content of one appear in other.

ln -s /etc/nginx/sites-available/example /etc/nginx/sites-enabled/
sudo nginx -t
  • Restart Nginx, php-fpm, and MySQL:-

service nginx restart
systemctl restart php7.2-fpm.service
systemctl restart mysql
apt-get update

Step 21. Test PHP-FPM

Please check step 5. Output will something like image below.

Step 22. Install WordPress In LEMP

Now LEMP environment is configured, just need to install WordPress.

cd /var/www/html
tar -xvzf latest.tar.gz
mv -v wordpress/* /var/www/html
rmdir wordpress
rm latest.tar.gz

apt-get update
service nginx restart
systemctl restart php7.2-fpm.service
systemctl restart mysql

After running above commands, enter database details and proceed WordPress installation. For more detail please check step 9.

Step 23. Add New Domain Name

To add a new domain, just create a new directory and new server block with required details.

Step 24. Install PHPMyAdmin in LEMP

Please follow step15 to install PHPMyAdmin.

Step 25. Create SWAP Memory

To create a swap file in Nginx, checkout step13.

Step 26. Configuring SSL

Check out this tutorial to install let’s encrypt in LEMP stack.

Step 27. Enable HTTP2

By default, LAMP/LEMP stack use HTTP1 protocol to deliver content. To improve loading speed, you must enable HTTP2. Checkout this tutorial:-

Step 28. Increase Maximum Upload Limit

By default, maximum upload limit is 2MB which may cause trouble while uploading big files. Checkout this tutorial to increase maximum upload limit:-

Hope this tutorial helped you to install and configure WordPress in Ubuntu (LAMP/LEMP) server. Any query or need help? Please comment below or PM me .

Thanks and Regards. :smile:

Please help me with CloudWays
Godaddy Hosting Review
How To Install Let's Encrypt SSL On Ubuntu 18.04 (LAMP/LEMP)

Thank you so much for the amazing step-by-step tutorial. I hope this would be helpful for everyone.
I really liked the way you formatted the tutorial. I do appreciate your valuable time.


(Rishi) #9

Thanks :relaxed:@BLOGGING_SURGEON @Abhijeet @gulshankumar

I agree with you. LAMP + NGINX >> LEMP > LAMP

(Rishi) #14

In case, why control panels like vesta use Nginx as reverse proxy with Apache2?

Still vesta is fast and not heavy on server. Even vesta run smoothly on 512MB RAM.



You need to do siege from other vps to check … what is better …

In my tests Nginx Only is better than other combinations.

(Rishi) #20


Added tutorial on LEMP also :blush:


(Rishi) #24

Some steps were similar, that’s why merged both LAMP and LEMP in same.

All thanks to @discobot, he suggested me. :stuck_out_tongue:

What are the Best free web panel for VPS or Dedicated server for Wordpress Hosting?

This is the most detailed guide I see about this topic. Thank you.

(Rishi) #27

THANKS :hugs:

(I am happy to see you sad) #28

Tell me which is faster… LAMP or LEMP?


LEMP is 5ms faster in every php request response… Than LAMP
Static content is approx equal response

But people chooses LEMP because it consume way less memory…

(Gopal) #32

Satisfactory Guide!

It requires production ready security techniques.

(Gopal) #33

Apache with Nginx is a bad idea…

In Linux you must know which command is running for which task, running blind command faces security risks.

(Gopal) #34

Unmanaged hosting requires some practical experiences with servers…


Configure SSH keys! None can hack!

(Gopal) #36

These are one of the security practice but it will not fully protect your servers from hackers… It requires some additional security techniques…


Like what?

(Gopal) #38

I can’t explain here for more details read security guidelines provided by PCI DSS, HIPAA and NIST.


Oh God!

Considering I have SSH keys and Cloudflare configure with my WordPress blog, none can hack my server, unless my laptop is hacked or I am leaking my server information intentionally!