How to Install Nginx, PHP and MySQL (LEMP Stack) on OpenSUSE Leap 42.1

How to Install Nginx, PHP and MySQL (LEMP Stack) on OpenSUSE Leap 42.1

LEMP or Linux, Engine-x, MySQL, and PHP is a collection of software installed on the Linux operating system to get your PHP based web applications up and running on the fast Nginx web server. The LEMP stack provides a fast and reliable basis for hosting web applications. LEMP is a collection of open source software which has a complete documentation set so you can easily learn more about the different applications and the way they fit together.

このチュートリアルで, I will show you how to install Nginx, MariaDB, and PHP-FPM on openSUSE leap 42.1. Then I will configure the OpenSUSE firewall with SuSEfirewall2 to allow access to the Nginx web server and show you how you can add a new virtual host configuration on the Nginx web server.

最終的に, we will install phpMyAdmin as an easy to use database administration tool and secure it by changing the web directory and enabling HTTP authentication for the phpMyAdmin directory.

前提条件

  • OpenSUSE Leap 42.1.
  • ルート権限.
  • Understanding zypper command.

ステップ 1 - Install and Configure SuSEfirewall2

SuSEfirewall2 is a script which generates iptables rules based on the configuration file "/etc/sysconfig/SuSEfirewall2". We will install and configure it to prevent network attacks on the server port.

Install SuSEfirewall2 with zypper:

zypper in SuSEfirewall2

When the installation is finished, we have to open some ports for the web applications and SSH. We will open port 22 for ssh service and port 80 そして 443 for http and https. Go to the "/etc/sysconfig/" directory and edit the "SuSEfirewall2" file:

cd /etc/sysconfig/
vim SuSEfirewall2

列をなして 253, add the port numbers of the services, separated by a white space:

FW_SERVICES_EXT_TCP="22 80 443"

ファイルおよび出口を除けば.

Restart the SuSEfirewall2 and the SSH service, then test the port configuration by connecting with telnet to the server.

Restart SuSEfirewall2 and SSH service:

/sbin/rcSuSEfirewall2 restart
systemctl restart sshd

Testing ssh connection with telnet on port 22:

telnet 192.168.1.101 22

Access the server with SSH.

ステップ 2 - インストールし、Nginx を構成

Nginx or engine x is a high-performance HTTP- and proxy server with low memory consumption. It is used by large scale websites like Netflix, Pinterest, CloudFlare, Github etc. Nginx has an easy to learn configuration syntax and can act also as a load balancer with health checks and reverse proxy with caching features.

この手順では, we will install nginx and add it to start at boot time. Install it with the "zypper in" command:

zypper in nginx

Start nginx and enable it to be started at boot time:

推奨:  How to install htop

systemctl start nginx
systemctl enable nginx

Nginx is started now, but if you try to access it through the web browser, あなたが取得します 403 禁断のエラー. This error occurs because there is no standard index document in the web root folder. この問題を解決するために, create a new index html in the root web directory "/srv/www/htdocs". Go to the directory and create the index.html file:

cd /srv/www/htdocs/
echo "<h1>This is Nginx OpenSUSE Leap 42.1</h1>" > index.html

Open your web browser and type your server IP and you will get the index page:

http://192.168.1.101/

Nginx is working in the Browser.

ステップ 3 - インストールし、MariaDB を構成

MariaDB is open source RDBMS (Relational Database management System) forked from MySQL under the GNU GPL license. このチュートリアルで, we will install MariaDB and configure the root password for the MariaDB shell.

Install MariaDB with the zypper command:

zypper in mariadb mariadb-client

Start MariaDB/MySQL and add it to boot startup with the systemctl command below:

systemctl スタート mysql
systemctl enable mysql

次に, configure the admin password for MariaDBb/MySQL with the mysqladmin command. Another option that you can use for this purpose is the "mysql_secure_installation". この手順では, I will use mysqladmin:

/usr/bin/mysqladmin -u root password 'aqwe123'

メモ:

"aqwe123" is the new password for MariaDB/MySQL.

Then login to the mysql shell with user root and password aqwe123:

mysql u ルート-p
TYPE PASSWORD 'aqwe123'

Test MySQL login.

Now MariaDB/MySQL is installed and configured with our new password.

ステップ 4 - Install and Configure PHP-FPM

PHP-FPM or FastCGI Process Manager is an alternative for the older PHP FastCGI with provides additional features and speed improvements. PHP-FPM is well suited for small and large sites based on the PHP programming language.

この手順では, we will install php5-fpm with some aditional extensions required by phpMyAdmin. Install php5-fom and the extensions with zypper:

zypper in php5 php5-mysql php5-fpm php5-gd php5-mbstring

インストールが完了したとき, go to the php5-fpm directory and copy the configuration file:

cd /etc/php5/fpm/
cp php-fpm.conf.default php-fpm.conf

Edit the configuration with vim command:

vim php-fpm.conf

行のコメントを解除します。 32 to enable php-fpm log, the default prefix is /var and if you have any problem with php5-fpm, you can check the log file "/var/log/php-fpm.log".

error_log = log/php-fpm.log

列をなして 148 we will configure the owner of the nginx process to the Nginx user. Change user and group of process to nginx:

ユーザー= nginxの
グループ= nginxの

ライン 159: configure php-fpm to run under a socket file, not the port. Change that line like this:

推奨:  アーチの強打をインストールする方法?

listen = /var/run/php-fpm.sock

And on line 170, change the permissions for the unix socket to the nginx user and group with mode 0660.

listen.owner = nginx
listen.group = nginx
listen.mode = 0660

ファイルを保存し、エディターを終了.

次に, go the PHP cli directory and edit the php.ini file with vim:

cd /etc/php5/cli/
vim php.ini

Change the value of cgi.fix_pathinfo to zero for security reasons. ライン 178:

cgi.fix_pathinfo=0

保存して終了..

Copy the php.ini to conf.d directory:

cp php.ini /etc/php5/conf.d/

The PHP-FPM Configuration is done at this stage, but we still need to configure Nginx. We have to setup Nginx to work with php-fpm.

Go to the nginx configuration directory and make a backup of the configuration file with the cp command:

cd /etc/nginx/
cp nginx.conf nginx.conf.backup

Edit nginx.conf with vim:

vim nginx.conf

Add index.php on line 48:

index index.php index.html index.htm;

Add the new php configuration section at line 68, this is the configuration for handling .php file requests.

場所 ~ .php$ {
root /srv/www/htdocs;
try_files $ URI = 404;
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $ DOCUMENT_ROOT $ fastcgi_script_name。
}

ファイルおよび出口を除けば.

次に, test the nginx configuration file syntax to ensure that there are no errors. Then start the php-fpm daemon and restart Nginx:

nginx t
systemctl start php-fpm
systemctl 再起動 nginx

To make sure the php-fpm and nginx is working properly, we will add a new php test file. Go the DocumentRoot directory and create a phpinfo file with the name info.php:

cd /srv/www/htdocs/
echo "<? php phpinfo();?>" > info.php

Open your web browser type your server ipaddress:

http://192.168.1.101/info.php

PHP is running.

Nginx and php-fpm are working now.

ステップ 5 - Configure a Nginx Virtualhost

この手順では, I will show you how to add a virtual host configuration for a website. We will configure a new domain "www.example.com" and the webroot directory in "/srv/www/example/". Please replace example and example.com with your own domain name.

GNginx nginx directory and create a new directory for the virtualhost configuration.

cd /etc/nginx/
mkdir vhosts.d/
cd vhosts.d/

次に, create new file example.conf for the domain name "example.com" virtualhost.

vim example.conf

paste virtualhost configuration below:

サーバー{
server_name example.com;
戻り値 301 $scheme://www.example.com$request_uri;
}
サーバー{
耳を傾ける 80;
root /srv/www/example;
index index.php index.html index.htm;
場所 / {
try_files $uri $uri/ =404;
}
場所 ~ .php$ {
try_files $ URI = 404;
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $ DOCUMENT_ROOT $ fastcgi_script_name。
}
}

ファイルおよび出口を除けば.

Now create the directory for the site "example.com" in the /srv/www/ directory, and create a new index file.

mkdir -p /srv/www/example/
cd /srv/www/example/
echo "<h1>This is www.example.com site.</h1>" > index.html
echo "<? php phpinfo();?>" > info.php

推奨:  Ubuntu は容易にする最新の Nvidia の Linux ドライバーをインストールするのにはしたいです。

Now open your web browser and visit the domain:

example.com

You will be redirected to www domain, and now access the php file:

http://www.example.com/info.php

PHP Info.

ステップ 6 - Install and Configure phpMyAdmin

PhpMyAdmin is a PHP based application to manage MySQL or MariaDB databases from a web browser. この手順では, I will configure phpMyAdmin to run under php-fpm and then make PHPMyAdmin secure by restricting access the phpMyAdmin login page.

Install phpMyAdmin with the zypper command:

zypper in phpMyAdmin

The command will install all packages needed by phpMyAdmin, including apache2-utils that is required for creating the password file.

Now we have phpMyAdmin installed on the system. Next we will create a new htpasswd file with the htpasswd command.

create password file htpasswd:

htpasswd -c /etc/nginx/htpasswd megumi
TYPE YOUR PASSWORD for user megumi

次に, we have to configure Nginx to use HTTP basic authentication for the phpmyadmin directory. We will restrict access to the phpMyAdmin login page with auth_basic, and only users that are in the "htpasswd" file can log in to the phpMyAdmin.

Go to the Nginx configuration directory and edit the nginx.conf file:

cd /etc/nginx/
vim nginx.conf

Define the web root directory inside of the server section by adding a new line under line 40 (server_name):

server_name localhost;
root /srv/www/htdocs; #newline config

Then add the phpMyAdmin configuration for nginx at line 60:

location ~ ^/phpMyAdmin/.*.php$ {
auth_basic "Restricted Access";
auth_basic_user_file /etc/nginx/htpasswd;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $ DOCUMENT_ROOT $ fastcgi_script_name。
fastcgi_params; が含まれて
}

ファイルを保存し、エディターを終了.

Test the nginx configuration and restart nginx:

nginx t
systemctl 再起動 nginx

次に, open your web browser and visit the phpMyAdmin URL, you will be asked for a username and password for the login page.

http://192.168.1.101/phpMyAdmin/

Basic HTTP auth for PHPMyAdmin.

Then log in with your MySQL username and password.

PHPMyAdmin on Nginx.

phpMyAdmin has been successfully installed and only the user in the htpasswd can log in.

参照

http://nginx.org/en/docs/
https://www.howtoforge.com/basic-http-authentication-with-nginx
https://www.ruby-forum.com/topic/4411851

広告

返信を残します