How to install LEMP (Linux, nginx, MySQL, PHP) stack on openSUSE


how-to-install-lemp-linux-nginx-mysql-php-stack-on-opensuse-vpsIn today’s simple article we will cover the steps needed for installing and configuring a basic LEMP stack on a openSUSE VPS. A LEMP stack is a synonym of LEMP server or LEMP web server. It refers to a set-up which includes Linux, Nginx, MySQL (MariaDB) and PHP.

REQUIREMENTS

We will be using our SSD 1 Linux VPS hosting plan for this tutorial.

Log in to your server via SSH:

# ssh root@server_ip

Before starting, enter the command below to check whether you have the proper OS version installed on your machine:

# cat /etc/issue

which should give you the underneath output. Of course if you use another openSUSE version the output will show that version:

Welcome to openSUSE 13.1 "Bottle" - Kernel \r (\l)

And now without further ado, we can begin by removing the pre-installed Apache web-server since we are replacing it with Nginx.

To do that, first we’ll stop the service, disable it from autostart and then remove it. To do that, type the following commands in your command line interface:

# systemctl stop apache2.service
# systemctl disable apache2.service
# zypper rm apache2

UPDATE THE SYSTEM

Now that the Apache webserver has been removed, we can update the system. Type:

# zypper up

Once the updates are finished, you can start setting up the LEMP stack by installing MySQL. In our extensive experience in dealing with the beautiful operational system that is openSUSE we stumbled upon errors and couldn’t start MySQL due to missing PERL dependencies. In some cases we were getting the following error:

FATAL ERROR: please install the following Perl modules before executing /usr/bin/mysql_install_db:
Sys::Hostname
Creation of MySQL databse in /var/lib/mysql failed
mysql.service: control process exited, code=exited status=1

To avoid this error, just install the Sys::Hostname PERL dependency with the following command:

# zypper install perl-Sys-Hostname-Long

With that taken care of, you can now continue with the MySQL installation. Execute:

# zypper install -y mysql-community-server mysql-community-server-client

Enable MySQL to start on boot and then start the service:

# systemctl enable mysql.service
# systemctl start mysql.service

Do the initial configuration of MySQL. Follow the on-screen messages as follows:

# mysql_secure_installation steps:

- Enter current password for root (enter for none):
 - Set root password? [Y/n] Y
 - Remove anonymous users? [Y/n] Y
 - Disallow root login remotely? [Y/n] Y
 - Remove test database and access to it? [Y/n] Y
 - Reload privilege tables now? [Y/n] Y 

Next, let’s install Nginx. Type:

# zypper install -y nginx

Enable Nginx to start on boot:

# systemctl enable nginx.service

In case of ‘/sbin/insserv failed, exit code 1’ error, type:

# insserv syslog

and repeat:

# systemctl enable nginx.service

Then, start Nginx:

# systemctl start nginx.service

Now go to http://<yourdomain.com> or http://<your_ip_address> from your favorite browser. You should see the contents of the file index.html file that is stored in the document root for Nginx (/srv/www/htdocs/).

We will now edit the Nginx configuration file. Make sure it resembles to the one we are posting, except for the lines you are sure you want them adjusted according to your needs. Note that this is only an initial config and it may vary depending on the website/application you are about to host.

# vim /etc/nginx/nginx.conf
worker_processes  2;

    sendfile        on;

    keepalive_timeout  65;

    gzip  on;

    include conf.d/*.conf;

    server {
        listen       80;
        server_name  _;

        #charset koi8-r;

        location / {
            root   /srv/www/htdocs/;
            index index.php index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /srv/www/htdocs/;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            root           /srv/www/htdocs/;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
            deny  all;
        }
    }

Save and close the file. Test the Nginx config:

# nginx -t

If everything is OK make the Nginx service aware of the changes by reloading it with the following command:

# systemctl reload nginx.service

Your next step is to install PHP-FPM. You can do that with the below commands:

# zypper install -y php5-pear php5-gd php5-mbstring php5-mcrypt php5-sockets php5-xmlrpc php5-zlib php5-curl php5-bz2 php5-ftp php5-sysvsem php5-fpm php5-mysql

Start the PHP-FPM configuration by copying the config file:

# cp -v /etc/php5/fpm/php-fpm.conf.default /etc/php5/fpm/php-fpm.conf

Now edit the php-fpm.conf and modify the user and group values from nobody to nginx. Also enable error logging. Open the config file with your favorite text editor:

# vim /etc/php5/fpm/php-fpm.conf

Uncomment and edit the following line to set the correct log file path:

error_log = /var/log/php-fpm.log

After you change the user and group values, the respective lines should look like this:

; Unix  user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
user = nginx
group = nginx

With openSUSE 13.1 there is no php.ini file for PHP-FPM. Let’s change that. Copy the php.ini file from /etc/php5/cli/ to /etc/php5/fpm/ as shown below:

# cp /etc/php5/cli/php.ini /etc/php5/fpm/

Now edit the php.ini file:

# vim /etc/php5/fpm/php.ini

Change the cgi.fix_pathinfo parameter value to 0. By default it will be commented out with a semi-colon and the value set to 1 which practically ensures that PHP will attempt to execute the closest file available when a requested PHP file can’t be found. Save and close the file after editing.

Enable the service to start on boot and then start it:

# systemctl enable php-fpm.service

# systemctl start php-fpm.service

Then, create a test php file:

# vim /srv/www/htdocs/index.php
<?php
 phpinfo();
 ?>

Open http://<yourdomain.com> or http://<your_ip_address> in your web browser. If you followed closely, you should now see the phpinfo start page.

And, that’s it. We have successfully set up a LEMP stack on our openSUSE VPS.

Of course you don’t have to do any of this if you use one of our Linux VPS Hosting services, in which case you can simply ask our expert Linux admins to install and configure LEMP for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

Tutorials
How to install LimeSurvey on an Ubuntu 14.04 VPS
Tutorials
How to configure IMAP with SSL
Tutorials
How to install Vesta Control Panel on a Linux VPS