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
apt-get update && apt-get-y upgrade
apt-get remove apache2
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)
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
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.
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!
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