Configure Nginx + PHP 7.0 + MySQL environment under Ubuntu 16.04

Before I used the one-click installation package of lnmp.org to configure the web server, I was too lazy to move, and I didn't know much about nginx configuration. After buying a new vps, I need to reconfigure the server environment. Take this opportunity to let me manually install them one by one and become familiar with them.

I chose the Ubuntu 16.04 system when deploying vps, so the following operations are based on this system.

Since I logged in with the root account at the time, there is no sudo in front of the code. If you install on the Ubuntu desktop version, remember to add sudo before each command

Update the software list and upgrade various software##

apt-get update && apt-get-y upgrade

Uninstall Apache

apt-get remove apache2

Install nginx, PHP7.0 and php7.0-fpm

Install directly with apt one-click, generally do not need to download source code to compile

apt-get-y install nginx php7.0 php7.0-fpm

Check the operation of nginx and php7.0-fpm service

systemctl status nginx
systemctl status php7.0-fpm

If there is no abnormality, it should display a green active (running) prompt, enter q to exit the interface
Normally, both services should be active (running)

Install MySQL

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

A purple background interface will appear during installation. Set the password of the MySQL root account. It is recommended to set a complex password

After installing MySQL, it’s best to run the security configuration wizard once, through which you can check the password of the root account, prohibit remote access to MySQL, remove anonymous users, test data tables, etc.
Security Configuration Wizard Command

mysql_secure_installation

Just follow the prompts

Install phpMyAdmin

You can easily operate MySQL database through the web through phpMyAdmin, and you can also use apt-get to install it here

apt-get-y install phpmyadmin

During the installation process, it will have two options to ask whether your web server is Apache or libhttpd. Here we are neither of them, just choose one.

After installation, phpMyAdmin is located in the /usr/share/phpmyadmin directory. When we configure nginx, we can access phpMyAdmin by pointing a website root directory to here.

Upload your website##

First of all, we can specify a directory dedicated to the website for easy operation. Here I use /var/www. First use the mkdir command to create the folder

mkdir /var/www

Here I configure two sites, one is my blog (based on Typecho), one is phpMyAdmin program, and both are programs based on PHP language

For blogs, first create a folder in the /var/www directory to store the source code of the blog, here I use blog

mkdir /var/www/blog

Then put the files in the blog root directory into the blog folder, here I use Filezlia to upload directly

When Filezlia connects to vps, it uses sftp to log in. Generally speaking, it is the root user, so the owner of the files uploaded under the root user is root. By default, nginx runs as the www-data user, and has no permission to write files whose owner is root. So if you do not change the file owner, there may be situations where the blog program cannot modify the source code of the theme online, cannot upload files, etc. Therefore, we need to change the owner of all files on the website to www-data, and run directly with -R( Recursion) parameter chown command:

chown -R www-data:www-data /var/www

After uploading website files, you can run this command to ensure that php can write to website files and avoid all kinds of weird problems. This also reflects the advantage of putting the website directory under a folder-a simple command can change the owner of all files.

After the website files are placed, we can start to modify the nginx configuration (in fact, there is no order), and the website can be accessed after configuration!

Modify Nginx configuration##

When installing nginx php7.0 php7.0-fpm in the second step, a basic php server has been set up, but we have not configured it yet, so it cannot run as expected for the time being.

First, let's understand the process of processing requests by the lnmp architecture web server. Generally speaking, our expectation is that when the request received by nginx points to a static file, nginx will return the corresponding file to the client. When the request received by nginx points to a php script, nginx will redirect the request Process in php7.0-fpm and return the processing result to the client.

For nginx, all the configuration is done in the /etc/nginx/nginx.conf file, open nginx.conf we can find it at the bottom of the file

include /etc/nginx/sites-enabled/*;

In other words, in the default configuration, nginx will automatically import the configuration in all files in the /etc/nginx/sites-enabled/ directory, and the configuration in these files is included in the http{} of nginx.conf

Generally, we will put the server configuration of each site in the sites-enabled directory, here I directly add a server configuration for each domain name in the /etc/nginx/sites-enabled/default file

vim /etc/nginx/sites-enabled/default

If you don’t know how to use the vim editor, you can also use sftp to log in to the server to download the file and upload it after editing with other text editors, but it is a lot more troublesome than editing directly on vps with vim

Recommended reading: Do you really know how to configure Nginx as a web server

"#" All comments after that can be deleted at will

# Default server configuration
#
server {
  # Listen on port 80. If the requested domain name is not defined or is accessed directly by ip, it will be handed over to this server for processing
 listen 80 default_server; 
 listen [::]:80 default_server;

 server_name _;return400;#By default, I ban direct ip access. If the domain name is not set, it will directly return 400}

# Configure phpMyAdmin
server {
 listen 80;
 server_name phpmyadmin.izgq.net;
 root /usr/share/phpmyadmin; 
 index index.php; 

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

# The configuration of the blog is basically the same
server {
  listen 80;
  server_name blog.izgq.net;
  root /var/www/blog;
  index index.php;

  location /{
    try_files $uri $uri//index.php$is_args$args;}

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

Don't forget to reload the nginx service to make the configuration effective after modification

service nginx reload

So the website started working happily

If some PHP extensions such as gd library are missing during use, then PHP reports an error, like this

apt-get install php7.0-gd

Add a suffix to install

Recommended Posts

Configure Nginx + PHP 7.0 + MySQL environment under Ubuntu 16.04
Ubuntu18.10 configure Java environment
Install MySQL under Ubuntu
Django&MySQL environment deployment under Ubuntu 14.04
How to compile and install PHP and Nginx in Ubuntu environment
Method of setting up PHP operating environment under Ubuntu server
CentOS 7 install Nginx, PHP, MySQL packages
Install and configure MySQL on Ubuntu
Ubuntu16.04 build php5.6 Web server environment
CentOS 7 configure php language development environment
Install MySQL under Ubuntu 18.04 (graphic tutorial)
JDK environment variable configuration method under ubuntu
Build the C++ compilation environment under ubuntu
Configure Java development environment in Ubuntu20.04 LTS
Linux environment construction: CentOs + Apache + MySQL + PHP
Configure Nginx Git server on Ubuntu system
Ubuntu install PHP and PHP Nginx configuration method
[PHP] Build a PHP operating environment under CentOS
Configure android debugging tool adb under ubuntu16.04
Build a python development environment under Ubuntu
How to change MySQL password under Ubuntu
Configure node js and npm under Ubuntu 14.04
Install JDK and configure environment variables on Ubuntu 16.04
Production environment deployment of ubuntu Django + Uwsgi + Nginx
How to configure TensorFlow use environment in Ubuntu
Ubuntu configure Tomcat
Remotely connect to MySQL database in Ubuntu environment
Ubuntu configure Apache
Ubuntu14 upgrade MySQL
How to enable Mysql remote access under Ubuntu 14.04
Embedded Linux development environment to build and configure Ubuntu
Install PHP in yum under CentOS, configure php-fpm service
Installation and simple practice of MySQL in ubuntu environment (1)
Environment configuration of JDK, mysql and tomcat under Centos7
Install and configure Mono production environment on Ubuntu Server
How to open https on nginx server under Ubuntu
Resolve the problems encountered in the linux environment under ubuntu
Install apache+PHP under Ubuntu
Install node.js under Ubuntu
ubuntu16.04 deploy GPU environment
Install python3.6 under Ubuntu 16.04
ubuntu redis php installation
Install mysql5.7 under CentOS7
Install Thrift under ubuntu 14.10
Install OpenJDK10 under Ubuntu
Install Caffe under Ubuntu 14.04
Python MySQLd under Ubuntu
Ubuntu configuration development environment
Install mysql on Ubuntu 14.04
Use iptables under ubuntu
Ubuntu development environment configuration
ubuntu 14.04 configure 3-wire 3IP
2018-09-11 Install arduino under Ubuntu
Ubuntu configure network commands
LNMP installation under Ubuntu
Network configuration under Ubuntu
Python virtual environment: Ubuntu16.04
Ubuntu completely remove MySQL
Ubuntu Touch environment setup
Install mysql under Centos 7
Install ROS under ROS Ubuntu 18.04[2]