Install Nginx, PHP, MySQL (Mariadb) on Ubuntu

-Would you be kind to me in which direction to go?
-In the famous you, – replied the cat.
-I don’t know it.
-So, in the unknown. In any case, it is known that in a known time you will find yourself therrre or herrre,-purring cat.

Nginx Installation:

apt update
apt install nginx

Installing PHP and the MariaDB driver

apt install php-fpm php-mysqlnd php-xml

Creating Virtual Domains

mkdir /var/www/domain.com
chmod -R 755 /var/www/domain.com

In the /etc/nginx/sites-available directory, we create a domain.com.conf file

The contents of the domain.com.conf file

server {
     listen 80;
     server_name domain.com;

     root /var/www/domain.com;

     # Add index.php to the list if you are using PHP
     index index.php index.html index.htm index.nginx-debian.html;
 
     server_name domain.com;
 
    location / {
               try_files $uri $uri/ =404;
               if (!-e $request_filename) {
                  rewrite ^.+/?(/wp-.*) $1 last;
            rewrite ^.+/?(/.*\.php)$ $1 last;
                  rewrite ^(.+)$ /index.php?q=$1 last;
      }
}

location ~ \.php$ {
               include snippets/fastcgi-php.conf;
               fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
       }
}

Now you need to reboot the Webserver

service nginx restart

Checking PHP work

In /var/www/domain.com , we will create the index.php file and write in it:

<?php phpinfo(); ?>

Next, open your website in the browser, in the example we use domain.com and if everything is done correctly, we see this picture:

Install MARIADB

sudo apt install mariadb-server mariadb-client

Security settings

mysql_secure_installation

Answer the questions:

Create a database and a user.

Entering the MARIADB:

mysql -u root

If you set the password to root:

mysql -u root -p
CREATE DATABASE domaindb;
CREATE USER 'nameus'@'localhost' IDENTIFIED BY 'user_password';

For the user_password place, enter the user password.

GRANT ALL ON domaindb. * to nameus @ localhost;
Quit

Deploying WordPress

cd /var/www/domain.com
curl -O https://wordpress.org/latest.tar.gz
tar xzvf latest.tar.gz
mv wordpress/* .
rmdir wordpress
chown -R www-data:www-data /var/www/domain.com
chmod -R 755 /var/www/domain.com

Deploying Nginx + PHP on the Ubuntu server

NGinx-is a quick and easy Web-server, to install it you need to enter several commands in the terminal:

sudo apt update
sudo apt install nginx php-fpm

Check the health of the Web server:

systemctl status nginx

In this case, change the file /etc/nginx/sites-available/default

server {
	listen 80 default_server;
	listen [::]:80 default_server;

	root /var/www/html;

	index index.html index.htm index.php;

	server_name _;

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

	location ~ \.php$ {
		try_files $uri = 404;
		include /etc/nginx/fastcgi.conf;
		fastcgi_pass unix:/run/php/php7.0-fpm.sock;
	}
}

Next, reboot Nginx

nginx -s reload

 

NGINX from all subdomains redirect 301 to the primary domain

Today I faced this problem, when configuring virtual hosts Nginx, after the customizations made and creating the appropriate configs, my subdomains began to refer through 301 Redirect to the main domain of the second level.

First I decided to recheck the correctness of the configuration and whether CloudFlare or SSL certificate could provoke this problem. But it turned out that the problem was not so much in the server, but in Google Chrome. It is therefore necessary to verify. Create a tab in Incognito mode and load the subdomain, if the subdomain is loaded, and if you are not in Incognito mode and pops up 301 redirect on the main domain, then the redirects were cached.

The only working method that was found is to remove the site caches in the following sequence:

  1. Go to Google Chrome developer mode
  2. Push F12
  3. At the top of the panel, you will find the vertical three dots, symbolizing the context menu.
  4. In the context menu select – Settings
  5. Network or Network tab, depending on localization
  6. Activate the tick – do not cache the page in developer mode.

After the steps are taken, try to open your subdomain with the developer panel.

These actions will remove all cached redirects, and a redirect of 301 to the main domain won’t work.