Ubuntu Server 16.04 Installation of LEMP/LNMP Detailed Tutorial

Keywords: PHP MySQL Nginx Database

Label of this article: ____________ Install LEMP/LNMP tutorial Ubuntu LEMP MySQL Nginx UbuntuServer Internet Random Talk

LEMP Refers to Linux + Nginx (pronunciation engine x, so here is the abbreviation of E rather than N) + MySQL + PHP, some parts of the country are called LNMP (because LNMP can not be read out, and LEMP can be pronounced directly, so in the future the tutorials of this site will always write LEMP)

The following operations are all done under root user. Please use sudo-i to switch to root operation or add sudo command by yourself.

Installation of Nginx 1.10.X

Because Nginx Updates are frequent, and Ubuntu Server has been updating slowly for a long time and can not support new functions, so we use Nginx official PPA instead of the default source installation.

First, add Nginx's PPA

apt-get install software-properties-common -y
add-apt-repository ppa:nginx/stable
apt-get update

If you like to experience the new features of Nginx, you can use add-apt-repository ppa:nginx/development to install the Mainline version of Nginx instead of the default version of Stable.

Then install some common software (some VPS vendors'templates are too concise) and Nginx

apt-get install curl vim wget unzip nginx -y

If you need more Nginx functionality, you can use apt install nginx-extras instead of the default nginx

II. Installation of PHP 7.0.X

This site is running in PHP 7.0, although some old programs and plug-ins do not support PHP 7.0, but the speed of goods is really fast, strongly appeal to developers to gradually transfer to the development of PHP 7.0, as for some domestic programs, we can only haw.

After execution, install some common software and PHP 7.0.X

apt-get install php-fpm php-mysql php-curl php-gd php-mbstring php-mcrypt php-xml php-xmlrpc php-zip -y

If your program needs additional PHP components, it can be found by apt-cache search php7.0 command. By default, Ubuntu Server 16.04 already uses PHP 7.0. All components can be replaced by php-xxx, for example, by using apt-cache search php7.0 command. apt install php-cli to install php7.0-cli

user@example:~$ sudo apt-cache search php7.0
libapache2-mod-php7.0 - server-side, HTML-embedded scripting language (Apache 2 module)
php-all-dev - package depending on all supported PHP development packages
php7.0 - server-side, HTML-embedded scripting language (metapackage)
php7.0-cgi - server-side, HTML-embedded scripting language (CGI binary)
php7.0-cli - command-line interpreter for the PHP scripting language
php7.0-common - documentation, examples and common module for PHP
php7.0-curl - CURL module for PHP
php7.0-dev - Files for PHP7.0 module development
php7.0-gd - GD module for PHP
php7.0-gmp - GMP module for PHP
php7.0-json - JSON module for PHP
php7.0-ldap - LDAP module for PHP
php7.0-mysql - MySQL module for PHP
php7.0-odbc - ODBC module for PHP
php7.0-opcache - Zend OpCache module for PHP
php7.0-pgsql - PostgreSQL module for PHP
php7.0-pspell - pspell module for PHP
php7.0-readline - readline module for PHP
php7.0-recode - recode module for PHP
php7.0-snmp - SNMP module for PHP
php7.0-sqlite3 - SQLite3 module for PHP
php7.0-tidy - tidy module for PHP
php7.0-xml - DOM, SimpleXML, WDDX, XML, and XSL module for PHP
php7.0-xmlrpc - XMLRPC-EPI module for PHP
libphp7.0-embed - HTML-embedded scripting language (Embedded SAPI library)
php7.0-bcmath - Bcmath module for PHP
php7.0-bz2 - bzip2 module for PHP
php7.0-enchant - Enchant module for PHP
php7.0-fpm - server-side, HTML-embedded scripting language (FPM-CGI binary)
php7.0-imap - IMAP module for PHP
php7.0-interbase - Interbase module for PHP
php7.0-intl - Internationalisation module for PHP
php7.0-mbstring - MBSTRING module for PHP
php7.0-mcrypt - libmcrypt module for PHP
php7.0-phpdbg - server-side, HTML-embedded scripting language (PHPDBG binary)
php7.0-soap - SOAP module for PHP
php7.0-sybase - Sybase module for PHP
php7.0-xsl - XSL module for PHP (dummy)
php7.0-zip - Zip module for PHP
php7.0-dba - DBA module for PHP

After installation, edit / etc/php/7.0/fpm/php.ini to replace; cgi.fix_pathinfo=1 is cgi.fix_pathinfo=0

vim /etc/php/7.0/fpm/php.ini

Direct input

:%s/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g

Then press ESC and press: wq and press Enter to save and exit.

Restart PHP 7.0-fpm

systemctl restart php7.0-fpm

Configuration of Nginx website file

Let's start by assuming that your domain name is example.com and your server IP is 192.0.2.2.( RFC 5737 And you have parsed A of example.com and recorded it to your server IP 192.0.2.2

We started editing the default configuration file for Nginx / etc/nginx/sites-available/default

vim /etc/nginx/sites-available/default

Enter or edit the following

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

    root /var/www/html;
    index index.php index.html index.htm;

#By default, the first domain name can be replaced by _example.com or not processed.
    server_name _;

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

#Open PHP 7.0-fpm mode
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

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

Then restart Nginx

nginx -s reload

As you can see, the default directory is / var/www/html, and then we create a phpinfo.php under this directory.

vim /var/www/html/phpinfo.php

input

<?php phpinfo(); ?>

After saving and exiting, enter http://example.com/ or http://192.0.2.2/ in the browser and see the classic phpinfo page to show that the installation was successful. If not, please carefully compare the steps to find out what went wrong.

Installation of MySQL 5.7

Execute the following commands

apt-get install mysql-server mysql-client -y

After successful installation, the system will let you enter the root password of MySQL twice. Please remember to use random and non-guessable passwords.

I've witnessed many novices installing weak passwords for the first time, and then the server was overtaken by people. So it's highly recommended here to execute a security setup after installing MySQL, a very simple command.

mysql_secure_installation

After execution, you will choose password strength, usually 1 or 2.

root@demo:~# mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root: 

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No:y Please input y Make initial security settings

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2 The most powerful password, of course, needs to enter 2

Using existing password for root.

Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : n If a strong password has been set before, there is no need to change it again root Password, otherwise press y Enter two resets after return

By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y Remove anonymous users. Remove them without any use.

Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y Close root Remote login. Shut it down if you don't need remote login.
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.

Remove test database and access to it? (Press y|Y for Yes, any other key for No) :y remove test data base

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y Reset database permissions

Again, passwords must be random and unpredictable. It's not clear how many examples of server days are caused by weak passwords.

With the initial security settings in place, we can create the database. First, we log in to MySQL using root.

mysql -u root -p

You will be prompted to enter your password and create a database called example after entering your password to login.

CREATE DATABASE example DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

Because of the popularity of mobile phones, we no longer use utf-8 coding, but use utf8mb4 so that we can MySQL The emoji expressions are stored in the database, such as

Then we create a user called example_user, using a powerful password and giving example database permissions.

GRANT ALL ON example.* TO 'example_user'@'localhost' IDENTIFIED BY 'Here's the powerful random password you're going to set that nobody can guess.';

The terminal will prompt Query OK, 0 rows affected, 1 warning (0.00 sec) not to take care of this warning.

Then we refresh permissions

FLUSH PRIVILEGES;

Use exit if you have no problem; command exit

Then we test the database and create a mysql-test.php in the / var/www/html directory.( via File

vim /var/www/html/mysql-test.php

input

<?php
# Fill our vars and run on cli
# $ php -f mysql-test.php
$dbname = 'example';    //MySQL database name
$dbuser = 'example_user';   //MySQL username
$dbpass = 'Your powerful password no one can guess';
$dbhost = 'localhost';  //?? hostname ?????? localhost
$link = mysqli_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to '$dbhost'");
mysqli_select_db($link, $dbname) or die("Could not open the db '$dbname'");
$test_query = "SHOW TABLES FROM $dbname";
$result = mysqli_query($link, $test_query);
$tblCnt = 0;
while($tbl = mysqli_fetch_array($result)) {
  $tblCnt++;
  #echo $tbl[0]."<br />\n";
}
if (!$tblCnt) {
  echo "MySQL is working fine. There are no tables.";
} else {
  echo "MySQL is working fine. There are $tblCnt tables.";
}
?>

Visit http://example.com/mysql-test.php after creation. If MySQL is working fine. There are no tables, then MySQL is working normally.

Well, that's the basics. Ubuntu Server 16.04 Installation of LEMP tutorial, if you have any questions, you can always comment and discuss.

Write in the end: FOR Freedom to see the outside world, as well as the IT industry, it is necessary to go to Google to look up information, and finally, Amway a V - PN agent. VPN of a red apricot Going to Google to check data is absolutely preferred, fast connection speed, easy to use. I bought 99 yuan a year, through this link( http://my.yizhihongxing.com/aff.php?aff=2509 ) After registration, the membership center will receive a discount code, which will be spread out evenly. Only 7 yuan per month is a special benefit.

Label of this article: ____________ Install LEMP/LNMP tutorial Ubuntu LEMP MySQL Nginx UbuntuServer Internet Random Talk

Turn from SUN'S BLOG - Focus on Internet knowledge and share the spirit of the Internet!

Original Address:____________< Ubuntu Server 16.04 Installation of LEMP/LNMP Detailed Tutorial>

Relevant reading:< Aaron Swartz - The Life of Internet Genius: Ask yourself every moment what's the most important thing I can do in the world right now?>
Relevant reading:< Web environment apache + php + mysql XAMPP, how to achieve a server to configure multiple websites?>

Relevant reading:< What is engineer culture? Why do engineers live? Why engineer culture as an IT or Internet company?>

Relevant reading: Useful for programmers: the latest Google hosts file download in 2017 and summary of various hosts problems encountered by netizens and configuration details

Relevant reading:< win10 Permanent Activation tutorial and how to see if windows system is permanently activated?>

Relevant BLOG: SUN'S BLOG - Focus on Internet knowledge and share Internet spirit! Go and see: www.whosmall.com

Original address: http://whosmall.com/?post=209

Posted by ozPATT on Tue, 26 Mar 2019 08:03:29 -0700