Tutorial

How To Install (LEMP) nginx, MySQL, PHP stack on Arch Linux

Published on November 3, 2012
How To Install (LEMP)  nginx, MySQL, PHP stack on Arch Linux

About Lemp

LEMP stack is a group of open source software to get web servers up and running. The acronym stands for Linux, nginx (pronounced Engine x), MySQL, and PHP. Since the server is already running Arch Linux, the linux part is taken care of. Here is how to install the rest.

Setup

The steps in this tutorial require the user to have root privileges. You can see how to set that up in the Initial Server Setup Tutorial in steps 3 and 4.

Step One—Pacman

Because pacman, the arch package manager, has a rolling package release, we should update Arch and its repositories before proceeding with any other steps:

sudo pacman -Syu

Step Two—Install MySQL

Once everything is fresh and up to date, we can start to install the server software, beginning with MySQL.

sudo pacman -S mysql

Once MySQL installs, start both mysql and the secure installation process. You will also be able to set the MySQL root password during the installation.

sudo systemctl start mysqld && mysql_secure_installation

When initially prompted for the MySQL root password, you can go ahead and press enter, as it has not yet been set. Your installation should look like this:

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorization.

Set root password? [Y/n] y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...



All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!

Follow up by restarting MySQL:

sudo systemctl restart mysqld

Step Three—Install nginx

Once MySQL is all set up, we can move on to installing nginx on the VPS.

sudo pacman -S nginx

nginx does not start on its own. To get nginx running, type:

sudo systemctl start nginx

You can confirm that nginx has installed an your web server by directing your browser to your IP address.

You can run the following command to reveal your VPS's IP address.

curl -s icanhazip.com

Step Four—Install PHP-FPM

In order to process php application, we will need to install php-fpm.

sudo pacman -S php-fpm

Once its installed, start it up.

sudo systemctl start php-fpm

Finally, we need to tell nginx to run php using php-fpm. To accomplish this, first open up the nginx configuration file:

sudo nano /etc/nginx/nginx.conf 

Find the location block that deals with php applications and replace the text in the section with the following:

location ~ \.php$ {
      fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;
      fastcgi_index  index.php;
      root   /srv/http;
      include        fastcgi.conf;
 }

Save, exit, and restart nginx:

sudo systemctl restart nginx

Step Five—Create a PHP Info Page

We can quickly see all of the details of the new php configuration.

To set this up, first create a new file:

sudo nano /srv/http/info.php

Add in the following line:

<?php
phpinfo();
?>

Then Save and Exit.

Restart nginx

sudo systemctl restart nginx

You can see the nginx and php-fpm configuration details by visiting http://youripaddress/info.php

Your LEMP stack is now set up and configured on your virtual private server.

Step Six—Configure the Daemons to Start at Boot

To ensure that all of the LEMP programs start automatically after any server restarts:

sudo systemctl enable nginx mysqld php-fpm

With that, LEMP is installed.

By Etel Sverdlov

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
10 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

guys please say guides for which Arch Linux version? not specific

I want the LEMP Nginx FastCGI WordPress for Arch thank you

greate best website of the world

Hello. I’m a total newbie about LEMP. Following the tutorial to the letter, I hit a roadblock. Restarting NGINX after editing /etc/nginx/nginx.conf and adding the appropriate code about .php returns the following errors:

Job for nginx.service failed. See 'systemctl status nginx.service' and 'journalctl -xn' for details.

● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled)
   Active: failed (Result: exit-code) since Fri 2014-07-04 00:17:41 PHT; 6min ago
  Process: 16665 ExecStart=/usr/bin/nginx -g pid /run/nginx.pid; error_log stderr; (code=exited, status=1/FAILURE)
 Main PID: 14284 (code=exited, status=0/SUCCESS)

Jul 04 00:17:41 archlinux nginx[16665]: 2014/07/04 00:17:41 [emerg] 16665#0: open() "/etc/nginx/fastcgi_conf" failed (2: No such file or directory) in /etc/nginx/nginx.conf:69
Jul 04 00:17:41 archlinux systemd[1]: Failed to start A high performance web server and a reverse proxy server.

I would also wanted to point out that after doing that last step, upon doing sudo systemctl --type=service, nginx.service is shown as loaded, failed, failed under the LOAD, ACTIVE, SUB columns repectively. There must be something wrong with what I’m doing for the service to fail like that. Please point me in the right direction.

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
August 8, 2013

@lucas.dechow: The commands listed in this article should substitute mysql with mariadb automatically so it’s fine.

The new recommended DB is mariadb, are there any changes in the code to install mysql?

got it working, dont know what i did :O. i uninstalled and re-installed and went back to the default config about 10 time. then i set in php.in extension=mysqli.so extension=mysql.so

and now its working. as strange as it can be.

still error 200 in access.log, error.log empty, php_fpm.log empty and blank page without code. There something wrong but. can’t find it :(

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
June 26, 2013

@webmaster_bloode try forcing php to display errors as a blank page usually means there was a php error:

Put this at the top of phpbb’s index.php:

error_reporting(-1); ini_set(‘display_errors’, true);

php-fpm.log is empty. still wont work :(

Think am going to give up.

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
June 22, 2013

Try setting “fastcgi_param SCRIPT_FILENAME” to “$document_root$fastcgi_script_name;” Also check /var/log/php-fpm.log for errors.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.