在Ubuntu Linux上安装Nginx

本教程将向您展示在Ubuntu Linux上安装Nginx所需的所有步骤。

本教程在Ubuntu 18.04上进行了测试。

1.在Ubuntu Linux上安装Nginx

使用Ubuntu APT命令安装Nginx服务器。

# apt-get update
# apt-get install nginx

手动重新启动Nginx Web服务器。

# service nginx restart
# service nginx status

验证Nginx服务状态。

● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2018-12-29 04:29:22 UTC; 1h 17min ago
Docs: man:nginx(8)
Process: 2233 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status
Process: 2221 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exite
Main PID: 2238 (nginx)
Tasks: 2 (limit: 1152)
CGroup: /system.slice/nginx.service
├─2238 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
└─2239 nginx: worker process

打开浏览器并输入Web服务器的IP地址。

在我们的示例中,在浏览器中输入了以下URL:

•http://200.200.200.200

将显示Nginx欢迎页面。

Nginx welcome page

Nginx Web服务器已成功安装。

2.在Ubuntu Linux上安装MySQL

几乎每个网站都需要一个数据库系统来存储其所有配置。

使用Ubuntu APT命令安装MySQL服务器。

# apt-get update
# apt-get install mysql-server mysql-client

使用以下命令访问MySQL服务控制台。

# mysql -u root -p

3.向Nginx添加PHP支持

Nginx需要一个外部程序来添加PHP支持。

使用Ubuntu APT命令安装PHP所需的包。

# apt-get update
# apt-get install php7.2-fpm

可选的。 使用以下命令安装Apache最常用的PHP模块。

# apt-get install php7.2-xml php7.2-curl php7.2-gd php7.2-mbstring php7.2-mysql
# apt-get install php7.2-bz2 php7.2-zip php7.2-json php7.2-readline

在系统上查找PHP配置文件的位置。

编辑php.ini配置文件。

# updatedb
# locate php.ini
# vi /etc/php/7.2/fpm/php.ini

您的PHP版本可能与我们的版本不同。

您的PHP配置文件位置可能与我们的不同。

这是我们配置的文件。

file_uploads = On
max_execution_time = 300
memory_limit = 256M
post_max_size = 32M
max_input_time = 60
max_input_vars = 4440

编辑Nginx默认网站配置文件。

# vi /etc/nginx/sites-available/default

在配置之前,这是原始文件。

server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
}

这是我们配置的新文件。

server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
}

验证您的Nginx配置文件是否没有错误。

# nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

重启PHP服务。
重启Nginx服务。

# service php7.2-fpm restart
# service nginx restart

要测试我们的配置,让我们创建一个基本的PHP测试页面。

# vi /var/www/html/test.php

这是test.php文件的内容。

<?php phpinfo(); ?>

打开浏览器并输入您的Web服务器和/test.php的IP地址。

在我们的示例中,在浏览器中输入了以下URL:

•http://200.200.200.200/test.php

应该显示PHP信息页面。

PHPInfo Nginx