<\/span><\/h2>\n\n\n\nLEMP is an acronym that describes a Linux OS with Nginx, MySQL (or MariaDB), and PHP. It’s a general stack of applications and servers that can provide the foundation for many software packages. Let’s install LEMP on our Ubuntu 20.04 VPS.<\/p>\n\n\n\n
Install Nginx<\/h3>\n\n\n\n
If there is an Apache web server installed on the server already, stop the Apache service and disable it from starting on server boot:<\/p>\n\n\n\n
systemctl stop apache2\nsystemctl disable apache2<\/pre>\n\n\n\nInstall Nginx with the following command:<\/p>\n\n\n\n
apt-get install nginx<\/pre>\n\n\n\nThat’s all we need to do with Nginx for now.<\/p>\n\n\n\n
Install MySQL<\/h3>\n\n\n\n
Next, we will install the MySQL database server, which will be used for storing the data of our Magento website, such as the products, categories, customers, and orders.
For the purposes of this tutorial, we will install and use MariaDB. To install the MariaDB database server, enter the following commands:<\/p>\n\n\n\n
apt-get install software-properties-common<\/pre>\n\n\n\nWe need to add a key in order to add our MariaDB repository.<\/p>\n\n\n\n
apt-key adv --recv-keys --keyserver hkp:\/\/keyserver.ubuntu.com:80 0xF1656F24C74CD1D8<\/pre>\n\n\n\nNow that we have the key, let’s install the repository:<\/p>\n\n\n\n
add-apt-repository 'deb [arch=amd64,arm64,ppc64el] http:\/\/mirror.lstn.net\/mariadb\/repo\/10.4\/ubuntu bionic main'<\/pre>\n\n\n\nUpdate your APT repositories so that the package lists get read.<\/p>\n\n\n\n
apt update<\/pre>\n\n\n\nFinally, we can install MariaDB.<\/p>\n\n\n\n
apt install mariadb-server<\/pre>\n\n\n\nOnce MariaDB installed, enable the MariaDB service to start on server boot:<\/p>\n\n\n\n
systemctl enable mariadb.service<\/pre>\n\n\n\nCheck the status of the MariaDB service:<\/p>\n\n\n\n
systemctl status mariadb.service<\/pre>\n\n\n\nIt should be running. That’s all we need to do with MariaDB for now.<\/p>\n\n\n\n
Install Elasticsearch<\/h3>\n\n\n\n
Switch to a directory where you can download a file temporarily.<\/p>\n\n\n\n
cd \/opt<\/pre>\n\n\n\nLet’s download the Elasticsearch package file.<\/p>\n\n\n\n
wget https:\/\/artifacts.elastic.co\/downloads\/elasticsearch\/elasticsearch-7.6.1-amd64.deb<\/pre>\n\n\n\nInstall it by running this next command:<\/p>\n\n\n\n
dpkg -i elasticsearch-7.6.1-amd64.deb<\/pre>\n\n\n\nStart the Elasticsearch service:<\/p>\n\n\n\n
systemctl start elasticsearch<\/pre>\n\n\n\nCheck the status:<\/p>\n\n\n\n
systemctl status elasticsearch<\/pre>\n\n\n\nCheck the version and other information about Elasticsearch:<\/p>\n\n\n\n
curl -XGET 'http:\/\/localhost:9200'<\/pre>\n\n\n\nIf it all looks normal up to this point, we can now install PHP.<\/p>\n\n\n\n
Install PHP 7.4<\/h3>\n\n\n\n
Add the PPA for PHP 7.4:<\/em><\/p>\n\n\n\nadd-apt-repository ppa:ondrej\/php<\/pre>\n\n\n\nNext, update the OS package list and install PHP 7.4 with the following command:<\/p>\n\n\n\n
apt-get update\napt-get install php7.4<\/pre>\n\n\n\nInstall all of the required PHP extensions with the following command:<\/p>\n\n\n\n
apt install php7.4-fpm php7.4-common php7.4-mysql php7.4-gmp php7.4-curl php7.4-intl php7.4-mbstring php7.4-xmlrpc php7.4-gd php7.4-xml php7.4-cli php7.4-zip php7.4-bcmath<\/pre>\n\n\n\nOnce the PHP extensions have been installed on the server, install a few other OS packages that are required for the Magento 2 installation:<\/p>\n\n\n\n
apt-get install git curl software-properties-common<\/pre>\n\n\n\nNext, open the main PHP configuration file \/etc\/php\/7.4\/fpm\/php.ini<\/code>:<\/p>\n\n\n\nnano \/etc\/php\/7.4\/fpm\/php.ini<\/pre>\n\n\n\nChange the following settings:<\/p>\n\n\n\n
file_uploads = On\nallow_url_fopen = On\nshort_open_tag = On\nmemory_limit = 256M\ncgi.fix_pathinfo = 0\nzlib.output_compression = On\nupload_max_filesize = 128M\nmax_execution_time = 600\nmax_input_time = 900\ndate.timezone = America\/Chicago<\/pre>\n\n\n\nSave and close the PHP configuration file.<\/p>\n\n\n\n
<\/span>Create a Database for Magento 2<\/span><\/h2>\n\n\n\nSecure your MariaDB installation by using the mysql_secure_installation<\/code> script. This script will remove anonymous users, disallow root login remotely, and remove the test database.<\/p>\n\n\n\nmysql_secure_installation<\/pre>\n\n\n\nWe recommend that you answer all of the questions as shown below:<\/p>\n\n\n\n
Enter current password for root (enter for none): Press [Enter] since no password is set by default\nSet root password? [Y\/n]: N (You can set a password if you like)\nRemove anonymous users? [Y\/n]: Y\nDisallow root login remotely? [Y\/n]: Y\nRemove test database and access to it? [Y\/n]: Y\nReload privilege tables now? [Y\/n]: Y<\/pre>\n\n\n\nNext, we will create our MySQL user and database for our Magento 2 website. Log in to your MySQL server with the following command and enter your MySQL root password when prompted:<\/p>\n\n\n\n
mysql -u root -p<\/pre>\n\n\n\nTo create a new database for our Magento 2 instance, run the following commands:<\/p>\n\n\n\n
mysql> set global log_bin_trust_function_creators=1;\nmysql> CREATE USER 'magento'@'localhost' IDENTIFIED WITH mysql_native_password BY 'strongPassword<\/span>';\nmysql> create database magentodb;\nmysql> GRANT ALL PRIVILEGES ON magentodb.* TO 'magento'@'localhost';\nmysql> flush privileges;\nmysql> quit<\/pre>\n\n\n\nMake sure that you give your magento<\/code> MariaDB user a real and strong password.<\/p>\n\n\n\n<\/span>Install Magento 2<\/span><\/h2>\n\n\n\nWe can now proceed with the Magento 2 installation. At the time of writing this article, the latest stable version of Magento is version 2.4.<\/p>\n\n\n\n
First, go to the GitHub repository and download the latest version of Magento with the following command:<\/p>\n\n\n\n
mkdir -p \/var\/www\/magento2\/\ncd \/var\/www\/magento2\/\ngit clone https:\/\/github.com\/magento\/magento2.git \/var\/www\/magento2\/<\/pre>\n\n\n\nNext, we will need to install Composer to install all necessary Magento components. You can install Composer by just running the following command:<\/p>\n\n\n\n
curl -sS https:\/\/getcomposer.org\/installer | php -- --install-dir=\/usr\/bin --filename=composer<\/pre>\n\n\n\nRun these next commands to configure composer.<\/p>\n\n\n\n
cd \/var\/www\/magento2\ncomposer install\nbin\/magento setup:install --base-url=http:\/\/yourdomain.com<\/span>\/ --db-host=localhost --db-name=magentodb --db-user=magento --db-password=strongPassword<\/span> --admin-firstname=FirstName<\/span> --admin-lastname=LastName<\/span> --admin-email=your@emailaddress.com<\/span> --admin-user=magentoadmin<\/span> --admin-password=strong-password<\/span> --language=en_US --currency=USD --timezone=America\/Chicago --use-rewrites=1\n<\/pre>\n\n\n\nDo not forget<\/strong> to replace the domain name<\/strong>, database password<\/strong>, admin username<\/strong>, password<\/strong> and admin email address<\/strong> accordingly.
Once the installation is complete, you should receive the following output:<\/p>\n\n\n\n[Progress: 699 \/ 701]
Post installation file permissions check...
For security, remove write permissions from these directories: '\/var\/www\/magento2\/app\/etc'
[Progress: 700 \/ 701]
Write installation date...
[Progress: 701 \/ 701]
[SUCCESS]: Magento installation complete.
[SUCCESS]: Magento Admin URI: \/admin_1k4pc0
Nothing to import.<\/pre>\n\n\n\nRemember the ‘Magento Admin URI’. You will need this info later as that’s the URL that you’ll be using to log in to the Magento back-end.<\/p>\n\n\n\n
Change the ownership of the magento2<\/code> directory to www-data<\/code> with the following command:<\/p>\n\n\n\nchown -R www-data:www-data \/var\/www\/magento2\/<\/pre>\n\n\n\n<\/span>Create an Nginx Configuration File<\/span><\/h2>\n\n\n\nCreate an Nginx configuration file with your text editor:<\/p>\n\n\n\n
nano \/etc\/nginx\/sites-available\/magento2<\/pre>\n\n\n\nThen add the following as the file’s content:<\/p>\n\n\n\n
upstream fastcgi_backend {\nserver unix:\/run\/php\/php7.4-fpm.sock;\n}\nserver {\nserver_name yourdomain.com<\/span>;\nlisten 80;\nset $MAGE_ROOT \/var\/www\/magento2;\nset $MAGE_MODE developer; # or production\naccess_log \/var\/log\/nginx\/magento2-access.log;\nerror_log \/var\/log\/nginx\/magento2-error.log;\ninclude \/var\/www\/magento2\/nginx.conf.sample;\n}<\/pre>\n\n\n\nMake sure that you set yourdomain.com<\/code> to your registered domain name.<\/p>\n\n\n\nRemove the default Nginx configuration file, if is not being used:<\/p>\n\n\n\n
rm -f \/etc\/nginx\/sites-enabled\/default<\/pre>\n\n\n\nEnable the newly created Nginx configuration file, then test the Nginx configuration and make sure that there are no errors:<\/p>\n\n\n\n
ln -s \/etc\/nginx\/sites-available\/magento2 \/etc\/nginx\/sites-enabled\/magento2<\/pre>\n\n\n\nnginx -t\nnginx: the configuration file \/etc\/nginx\/nginx.conf syntax is ok\nnginx: configuration file \/etc\/nginx\/nginx.conf test is successful<\/pre>\n\n\n\nThat’s it! Your web server is now successfully configured.<\/p>\n\n\n\n