search
HomeOperation and MaintenanceNginxHow to install LEMP environment for Nginx server in Ubuntu

Preparation

Install ubuntu 16.04 server version

Step 1: Install nginx server

1, nginx It is an advanced, resource-optimized web server program used to display web pages to visitors on the Internet. We start with the installation of the nginx server and use the apt command to obtain the nginx program from the official software repository of ubuntu.

$ sudo apt-get install nginx

How to install LEMP environment for Nginx server in Ubuntu

Install nginx on ubuntu 16.04
2. Then enter the netstat and systemctl commands to confirm that the nginx process has been started and bound to port 80.

$ netstat -tlpn

How to install LEMP environment for Nginx server in Ubuntu

Check nginx network port connection

$ sudo systemctl status nginx.service

How to install LEMP environment for Nginx server in Ubuntu

Check nginx service status

When you confirm that the service process has been started, you can open a browser, use the http protocol to access your server IP address or domain name, and browse the default web page of nginx.

http://ip-address

How to install LEMP environment for Nginx server in Ubuntu

Step 2: Enable nginx http/2.0 protocol

3. Support for http/2.0 protocol is included by default in the latest release of nginx on ubuntu 16.04 Binary is included, it only connects via ssl and promises a huge improvement in loading web pages.

To enable this protocol of nginx, first find the website configuration file provided by nginx and enter the following command to back up the configuration file.

$ cd /etc/nginx/sites-available/
$ sudo mv default default.backup

How to install LEMP environment for Nginx server in Ubuntu

Back up nginx website configuration file
4. Then, use a text editor to create a new default file and enter the following content:

server {
    listen 443 ssl http2 default_server;
    listen [::]:443 ssl http2 default_server;
    root /var/www/html;
    index index.html index.htm index.php;
    server_name 192.168.1.13;
    location / {
        try_files $uri $uri/ =404;
    }
    ssl_certificate /etc/nginx/ssl/nginx.crt;
    ssl_certificate_key /etc/nginx/ssl/nginx.key;
    ssl_protocols tlsv1 tlsv1.1 tlsv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers eecdh+chacha20:eecdh+aes128:rsa+aes128:eecdh+aes256:rsa+aes256:eecdh+3des:rsa+3des:!md5;
    ssl_dhparam /etc/nginx/ssl/dhparam.pem;
    ssl_session_cache shared:ssl:20m;
    ssl_session_timeout 180m;
    resolver 8.8.8.8 8.8.4.4;
    add_header strict-transport-security "max-age=31536000;
    #includesubdomains" always;
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }
    location ~ /\.ht {
        deny all;
    }
}
server {
    listen     80;
    listen  [::]:80;
    server_name  192.168.1.13;
    return     301 https://$server_name$request_uri;
}

How to install LEMP environment for Nginx server in Ubuntu

Enable nginx http 2 protocol
The above configuration snippet adds the http2 parameter to all ssl listening instructions to enable http/2.0.

The last section added to the server configuration above is used to redirect all non-ssl traffic to the ssl/tls default host. Then replace the server_name option with your host's IP address or DNS record (preferably the fqdn name).

5. After you follow the above steps to edit the default configuration file of nginx, use the following commands to generate and view the SSL certificate and key.

Use your custom settings to complete the certificate production. Note that the common name is set to match your dns fqdn record or server ip address.

$ sudo mkdir /etc/nginx/ssl
$ sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/nginx/ssl/nginx.key -out /etc/nginx/ssl/nginx.crt
$ ls /etc/nginx/ssl/

How to install LEMP environment for Nginx server in Ubuntu

Generate nginx ssl certificate and key
6. Use a strong dh encryption algorithm by entering the following command, which will modify the previous configuration file ssl_dhparam configuration document.

$ sudo openssl dhparam -out /etc/nginx/ssl/dhparam.pem 2048

How to install LEMP environment for Nginx server in Ubuntu

Create diffie-hellman key
7. After the diffie-hellman key is generated, verify whether the nginx configuration file is correct and whether it can be served by the nginx network program application. Then run the following command to restart the daemon and observe any changes.

$ sudo nginx -t
$ sudo systemctl restart nginx.service

How to install LEMP environment for Nginx server in Ubuntu

Check nginx configuration
8. Type the following command to test that nginx uses the http/2.0 protocol. If you see h2 in the protocol, it means that nginx has been successfully configured to use the http/2.0 protocol. All latest browsers support this protocol by default.

$ openssl s_client -connect localhost:443 -nextprotoneg ''

How to install LEMP environment for Nginx server in Ubuntu

Test the nginx http 2.0 protocol

Step 3: Install the php 7 interpreter

With the assistance of the fastcgi process management program, nginx can generate dynamic web content using the PHP dynamic language interpreter. fastcgi can be obtained by installing the php-fpm binary package from the ubuntu official repository.

9. Enter the following command in your server console to obtain php7.0 and the extension package, which allows php to communicate with the nginx network service process.

$ sudo apt install php7.0 php7.0-fpm

How to install LEMP environment for Nginx server in Ubuntu

Install php 7 and php-fpm
10. After the php7.0 interpreter is successfully installed, enter the following command to start or check the php7.0-fpm daemon Process:

$ sudo systemctl start php7.0-fpm
$ sudo systemctl status php7.0-fpm

How to install LEMP environment for Nginx server in Ubuntu

开启、验证 php-fpm 服务
11、 当前的 nginx 配置文件已经配置了使用 php fpm 来提供动态内容。

下面给出的这部分服务器配置让 nginx 能够使用 php 解释器,所以不需要对 nginx 配置文件作别的修改。

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

下面是的截图是 nginx 默认配置文件的内容。你可能需要对其中的代码进行修改或者取消注释。

How to install LEMP environment for Nginx server in Ubuntu

启用 php fastcgi
12、 要测试启用了 php-fpm 的 nginx 服务器,用下面的命令创建一个 php 测试配置文件 info.php。接着用 http://ip_or domain/info.php 这个网址来查看配置。

$ sudo su -c &#39;echo "<?php phpinfo(); ?>" |tee /var/www/html/info.php&#39;

How to install LEMP environment for Nginx server in Ubuntu

创建 php info 文件

How to install LEMP environment for Nginx server in Ubuntu

检查 php fastcgi 的信息
检查服务器是否宣告支持 http/2.0 协议,定位到 php 变量区域中的 $_server[‘server_protocol'] 就像下面这张截图一样。

How to install LEMP environment for Nginx server in Ubuntu

检查 http2.0 协议信息
13、 为了安装其它的 php7.0 模块,使用 apt search php7.0 命令查找 php 的模块然后安装。

如果你想要 安装 wordpress 或者别的 cms,需要安装以下的 php 模块,这些模块迟早有用。

$ sudo apt install php7.0-mcrypt php7.0-mbstring

How to install LEMP environment for Nginx server in Ubuntu

安装 php 7 模块
14、 要注册这些额外的 php 模块,输入下面的命令重启 php-fpm 守护进程。

$ sudo systemctl restart php7.0-fpm.service

第 4 步:安装 mariadb 数据库

15、 最后,我们需要 mariadb 数据库来存储、管理网站数据,才算完成 lemp 的搭建。

运行下面的命令安装 mariadb 数据库管理系统,重启 php-fpm 服务以便使用 mysql 模块与数据库通信。

$ sudo apt install mariadb-server mariadb-client php7.0-mysql
$ sudo systemctl restart php7.0-fpm.service

How to install LEMP environment for Nginx server in Ubuntu

安装 mariadb
16、 为了安全加固 mariadb,运行来自 ubuntu 软件仓库中的二进制包提供的安全脚本,这会询问你设置一个 root 密码,移除匿名用户,禁用 root 用户远程登录,移除测试数据库。

输入下面的命令运行脚本,并且确认所有的选择。参照下面的截图。

$ sudo mysql_secure_installation

How to install LEMP environment for Nginx server in Ubuntu

mariadb 的安全安装
17、 配置 mariadb 以便普通用户能够不使用系统的 sudo 权限来访问数据库。用 root 用户权限打开 mysql 命令行界面,运行下面的命令:

$ sudo mysql 
mariadb> use mysql;
mariadb> update user set plugin=&#39;‘ where user=&#39;root&#39;;
mariadb> flush privileges;
mariadb> exit

How to install LEMP environment for Nginx server in Ubuntu

mariadb 的用户权限
最后通过执行以下命令登录到 mariadb 数据库,就可以不需要 root 权限而执行任意数据库内的命令:

$ mysql -u root -p -e &#39;show databases&#39;

How to install LEMP environment for Nginx server in Ubuntu

查看 mariadb 数据库

The above is the detailed content of How to install LEMP environment for Nginx server in Ubuntu. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
NGINX's Key Features: Performance, Scalability, and SecurityNGINX's Key Features: Performance, Scalability, and SecurityApr 13, 2025 am 12:09 AM

NGINX improves performance through its event-driven architecture and asynchronous processing capabilities, enhances scalability through modular design and flexible configuration, and improves security through SSL/TLS encryption and request rate limiting.

NGINX vs. Apache: Web Hosting and Traffic ManagementNGINX vs. Apache: Web Hosting and Traffic ManagementApr 12, 2025 am 12:04 AM

NGINX is suitable for high concurrency and low resource consumption scenarios, while Apache is suitable for scenarios that require complex configurations and functional extensions. 1.NGINX is known for handling large numbers of concurrent connections with high performance. 2. Apache is known for its stability and rich module support. When choosing, it must be decided based on specific needs.

NGINX: The Versatile Tool for Modern Web ApplicationsNGINX: The Versatile Tool for Modern Web ApplicationsApr 11, 2025 am 12:03 AM

NGINXisessentialformodernwebapplicationsduetoitsrolesasareverseproxy,loadbalancer,andwebserver,offeringhighperformanceandscalability.1)Itactsasareverseproxy,enhancingsecurityandperformancebycachingandloadbalancing.2)NGINXsupportsvariousloadbalancingm

Nginx SSL/TLS Configuration: Securing Your Website with HTTPSNginx SSL/TLS Configuration: Securing Your Website with HTTPSApr 10, 2025 am 09:38 AM

To ensure website security through Nginx, the following steps are required: 1. Create a basic configuration, specify the SSL certificate and private key; 2. Optimize the configuration, enable HTTP/2 and OCSPStapling; 3. Debug common errors, such as certificate path and encryption suite issues; 4. Application performance optimization suggestions, such as using Let'sEncrypt and session multiplexing.

Nginx Interview Questions: Ace Your DevOps/System Admin InterviewNginx Interview Questions: Ace Your DevOps/System Admin InterviewApr 09, 2025 am 12:14 AM

Nginx is a high-performance HTTP and reverse proxy server that is good at handling high concurrent connections. 1) Basic configuration: listen to the port and provide static file services. 2) Advanced configuration: implement reverse proxy and load balancing. 3) Debugging skills: Check the error log and test the configuration file. 4) Performance optimization: Enable Gzip compression and adjust cache policies.

Nginx Caching Techniques: Improving Website PerformanceNginx Caching Techniques: Improving Website PerformanceApr 08, 2025 am 12:18 AM

Nginx cache can significantly improve website performance through the following steps: 1) Define the cache area and set the cache path; 2) Configure the cache validity period; 3) Set different cache policies according to different content; 4) Optimize cache storage and load balancing; 5) Monitor and debug cache effects. Through these methods, Nginx cache can reduce back-end server pressure, improve response speed and user experience.

Nginx with Docker: Deploying and Scaling Containerized ApplicationsNginx with Docker: Deploying and Scaling Containerized ApplicationsApr 07, 2025 am 12:08 AM

Using DockerCompose can simplify the deployment and management of Nginx, and scaling through DockerSwarm or Kubernetes is a common practice. 1) Use DockerCompose to define and run Nginx containers, 2) implement cluster management and automatic scaling through DockerSwarm or Kubernetes.

Advanced Nginx Configuration: Mastering Server Blocks & Reverse ProxyAdvanced Nginx Configuration: Mastering Server Blocks & Reverse ProxyApr 06, 2025 am 12:05 AM

The advanced configuration of Nginx can be implemented through server blocks and reverse proxy: 1. Server blocks allow multiple websites to be run in one instance, each block is configured independently. 2. The reverse proxy forwards the request to the backend server to realize load balancing and cache acceleration.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.