Breaking News
Home / How To / How to install Nginx with PHP and MariaDB (LEMP Stack) on Fedora 31

How to install Nginx with PHP and MariaDB (LEMP Stack) on Fedora 31



The LEMP software bundle is a group of open source software installed together to allow a server to host websites and apps. It is an abbreviation for L inux, E Nginx server, M ySQL (with MariaDB here), and P HP. [19659002] In this guide, you install a LEMP stack on a Fedora 31-based server. We will also install PHPMyAdmin, Redis and Opcache.

Prerequisites

  • A server running Fedora 31.

  • A non-root user using root.

  • Make sure everything is up to date.

      $ sudo dnf upgrade
    
  • Get packages that your system needs.

      $ sudo dnf install wget curl nano -y
    

    Some of these packages may already be installed on your system.

  • Disable SELinux.

      $ sudo setenforce 0
    

Configuring Firewall

The first step is to configure the firewall. The Fedora server comes with Firewalld firewall pre-installed.

Check if the firewall is running.

  $ sudo firewall-cmd - stat

You should get the following output.

  runs

Set the default firewall zone to public.

  $ sudo firewall-cmd - set-default-zone = public

Check the currently allowed services / ports.

  $ sudo firewall-cmd - zone = public --permanent - list-services

It should show the following output.

  dhcpv6-client mdns ssh

Allow HTTP and HTTPS ports.

  $ sudo firewall-cmd - zone = public --permanent --add-service = http
$ sudo firewall cmd - zone = public --permanent --add-service = https

Check the status of the firewall again.

  $ sudo firewall-cmd --zone = public --permanent - list-services

You should see a similar output.

  dhcpv6-client http https mdns ssh

Reload the firewall.

  $ sudo systemctl reload firewall

Install PHP

Fedora 31

as a standard vessel with PHP 7.3. But we want to install PHP 7.4 for which we need to add the REMI repository.

Install the REMI repository, which is the official Fedora repository to install PHP packages.

  $ sudo dnf -y install https: // rpms. remirepo.net/fedora/remi-release-31.rpm

Enable the remi and remi-php74 archives and disable the repi remi-modular . This enables the repository required to install PHP 7.4 packages.

  $ sudo dnf config-manager - set-enabled remi
$ sudo dnf config-manager - set-enabled remi-php74
$ sudo dnf config-manager - set-disabled remi-modular

Install PHP 7.4 along with some extra packages.

  $ sudo dnf install -y php-cli php-fpm php-mysqlnd

Check if PHP is working correctly.

  $ php version

You would see a similar output.

  PHP 7.4.3 (cli) (built: 18 Feb 2020 11:53:05) (NTS)
Copyright (c) PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

Installing MariaDB

MariaDB is a drop-in replacement for MySQL which means that commands for running and using MariaDB are the same as for MySQL.

Fedora 31 by default ships with MariaDB 10.3 but we can install the latest 10.4 (available at the time of writing this tutorial) as well. For this we need to add the official MariaDB layer.

Create MariaDB.repo in the /etc/yum.repos.d/ directory.

  $ sudo nano / etc / yum.repos.d / MariaDB.repo

Add the following code to it.

  # MariaDB 10.4 Fedora Storage List
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.4/fedora31-amd64
gpgkey = https: //yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck = 1

Save the file by pressing Ctrl + X and enter Y when prompted.

To install MariaDB, issue the following commands

  $ sudo dnf install MariaDB server - y

Notice the command. If you want to install the copy (10.3) in the Fedora repository, you should use sudo dnf install mariadb server but for the 10.4 version we use sudo dnf install MariaDB server

Check if MariaDB is installed correctly.

  $ mysql --version

You should see the following output.

  mysql Ver 15.1 Distrib 10.4.12-MariaDB, for Linux (x86_64) with EditLine wrapper

Activate and start the MariaDB service.

  $ sudo systemctl enable mariadb
$ sudo systemctl start mariadb

Run the following command to perform the default configuration such as providing a root password, removing anonymous users, rejecting remote root logins, and releasing test tables.

  $ sudo mysql_secure_installation

With MariaDB 10.4, you are now asked between using the root password or the unix_socket plugin. The Unix_socket plugin lets you log in to MariaDB with your Linux user credentials. It is considered more secure, but you need a traditional username / password to use third-party apps like PhpMyAdmin. We will stick to using the unix_socket plugin for this tutorial. You can still use PhpMyAdmin through all users that you create specific users for your databases.

Pressing Enter selects the default option (the case of capital letters, Y in this case).

  NOTE: RECOMMENDED TO ALL MariaDB
PRODUCT SERVICES USE READ EVERY EVERY STEP NOW!

To log in to MariaDB to secure it, we need the power
password for the root user. If you have just installed MariaDB, and
has not set the root password yet, you should just press enter here.

Enter the current root password (set for none): [PRESS ENTER]
OK, use password successfully, continue ...

Setting the root password or using unix_socket ensures that no one
can log in to MariaDB root user without proper permission.

You already have your root account protected, so you can safely reply & # 39; n & # 39 ;.

Switch to unix_socket authentication [Y/n] [PRESS ENTER]
  Activated successfully!
Reload privilege tables ...
... Success!

You already have your root account protected, so you can safely reply & # 39; n & # 39 ;.

Change the root password? [Y/n] [ANSWER n]

  By default, a MariaDB installation has an anonymous user, which allows anyone
to log in to MariaDB without having to create a user account for
them. This is only for testing and for installation
go a little smoother. You should remove them before moving to one
production environment.

Delete anonymous users? [Y/n] [PRESS ENTER]
  ... Success!

Normally, root should only be allowed to connect from & # 39; localhost & # 39 ;. This
makes sure that no one can guess the root password from the network.

Remove root login remotely? [Y/n] [PRESS ENTER]
  ... Success!

By default, MariaDB comes with a database called & # 39; test & # 39; as anyone can
access. This is also for testing only and should be removed
before moving into a production environment.

Delete the test database and access it? [Y/n] [PRESS ENTER]
   - Release Test Database ...
... Success!
 - Delete permissions in the test database ...
... Success!

If you reload the permission tables, you will ensure that all changes have been made so far
takes effect immediately.

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

Cleaning up ...

All ready! If you have completed all the above steps, your MariaDB
installation should now be secure.

Thank you for using MariaDB!

That's it. Next time you want to log in to MySQL, use the following command

  $ sudo mysql

Enter your root password when prompted.

Install Nginx

Fedora 31 as a standard vessel with Nginx's stable version (1.16.1). If you want to install the main version of Nginx, follow our guide to build Nginx from the source. Make sure you get the latest version of Nginx and the dependencies mentioned in the tutorial. The rest of the instructions will remain the same (change the path name according to the command ./ configure ). For this tutorial, we will stick to the stable version of Nginx.

Install Nginx server.

  $ sudo dnf install nginx-y

Check if it is working properly.

  $ nginx -v

You should see the following output.

  nginx version: nginx / 1.16.1

Start and enable Nginx.

  $ sudo systemctl start nginx
$ sudo systemctl enable nginx

Open the server's IP address in a web browser to see the following page. This means that Nginx works correctly.

 Fedora Nginx Test Page

Configure Nginx

Set up directories where the server blocks will live.

  $ sudo mkdir / etc / nginx / sites-available
$ sudo mkdir / etc / nginx / sites-enabled

Create the directory where your site will reside.

  $ sudo mkdir /var/www/example.com/html -p

Using the -p directive creates parent directories that did not exist before.

Run the following command to add a configuration file for your site.

  $ sudo nano / etc / nginx /sites-available/example.com.conf

Paste the following code into the editor.

  server {
listen *: 80;
server name example.com;
root /var/www/example.com/html;
index index.php index.html;

location / {
try_files $ uri $ uri / = 404;
}

access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;

location ~  .php $ {
try_files $ uri = 404;
fastcgi_pass unix: /run/php-fpm/www.sock;
fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name;
fastcgi_index index.php;
include fastcgi_params;
}
}

This file assumes that we will host example.com in the directory / var / www / html . If you will not use any domain and configure your server to be accessible only through the IP address / localhost, you must remove the corresponding server block settings from the nginx.conf file otherwise it will mess with it server blocks you create.

Activate this configuration file by linking it to the sites-enabled directory.

  $ sudo ln -s /etc/nginx/sites-available/example.com .conf / etc / nginx / sites-enabled /

Open file /etc/nginx/nginx.conf for editing.

  $ sudo nano /etc/nginx/nginx.conf

Paste the following lines after the row include /etc/nginx/conf.d/*.conf Chapter19459013 Premiere19659097 include /etc/nginx/sites-enabled/*.conf;
server_names_hash_bucket_size 64;

Change the value of types_hash_max_size from 2048 to 4096.

  types_hash_max_size 4096;

Press Ctrl + X to close the editor and press Y when prompted to save the file. Test the Nginx configuration.

  $ sudo nginx -t

You should see the following output indicating that your configuration is correct.

  nginx: configuration file /etc/nginx/nginx.conf syntax is ok
nginx: The /etc/nginx/nginx.conf test configuration file is successful

Reload the Nginx service.

  $ sudo systemctl reload nginx

Configure PHP-FPM

Open file /etc/php-fpm.d/www.conf .

  $ sudo nano /etc/php-fpm.d/www.conf

We need to set Unix users / group of PHP processes to nginx . Find the line user = apache and = apache in the file and change them to nginx.

  ...
; Unix user / group of processes
; Note: The user is required. If the group is not set, the default user group is
; will be used.
; RPM: apache users chosen to access the same directories as httpd
user = nginx
; RPM: Keep a group allowed to enter log dir.
group = nginx
...

Also set owner permissions for unix socket to nginx and remove ; in front of them.

  list.owner = nginx
list.group = nginx

Save the file by pressing Ctrl + X and enter Y when prompted.

Restart the PHP-fpm process.

  $ sudo systemctl restart php-fpm

To test your PHP setting, create a file test.php in the html directory.

  $ sudo nano /var/www/example.com/html/test .php

Add the following content to it and save the file by pressing Ctrl + X and enter Y when prompted.

  <? Php phpinfo ();

Start http: // /test.php in your browser and you should see the following.

 PHP Info Page

Conclusion

That's all for this tutorial. Your LEMP installation is complete and you can start creating and hosting your websites and applications.


Source link