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

how-to-install-lemp-linux-nginx-mysql-php-stack-on-opensuse-vpsIn this simple how-to you will learn how to setup and initially configure basic LEMP stack on a RoseHosting openSUSE VPS.

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:

 

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

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

# zypper up

We will start setting up our LEMP stack by installing mysql. To do that, type the following:

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

enable & start it,

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

and go on with 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

Type the next command to enable the nginx webserver at startup:

# systemctl enable nginx.service

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

# insserv syslog

and repeat:

# systemctl enable nginx.service

to enable the webserver service, and

# systemctl start nginx.service

to start it.

Configuration of the nginx webserver:

Create test index.html file using your favorite text editor. In this case, vim:

# vim /srv/www/htdocs/index.html

and write some test stuff in it. We are not going to use this file, its purpose is to just show us that we’ve made all the right steps so far:

testing the webserver. no php installed yet.

Go to http://<yourdomain.com> or http://<your_ip_address> in your browser. You should see the contents of the file.

Now, open and edit the nginx config file. Make sure it resembles the one i’m 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/webapplication you are about to host.

# vim /etc/nginx/nginx.conf

worker_processes  2;

listen       80;
server_name  _;

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/;
}

location ~ \.php$ {
try_files $uri =404;
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;
}

location ~ /\.ht {
deny  all;
}
}

Save the file and make the nginx service aware of the changes by reloading it with the following command:

# systemctl reload nginx.service

Finally – install php-fpm:

# 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

It is only smart to 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

Enable the service, and 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 LEMP stack on our openSUSE VPS. The next time we will see the steps to set up some of the most popular CMS systems on this very setup.

Leave a Reply