<返回更多

Linux安装调试Nginx

2021-09-16    牛鹭学院软件测试
加入收藏

 

Ubuntu下安装Nginx

一、系统基本信息查看

1、查看Ubuntu版本信息:

使用命令:cat /proc/version 查看

~$ cat /proc/version
linux version 4.15.0-106-generic (buildd@lcy01-amd64-016) (gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)) #107-Ubuntu SMP Thu Jun 4 11:27:52 UTC 2020

从查看的结果很明显发现当前的版本信息为: (Ubuntu 7.5.0-3ubuntu1~18.04),

2、查看系统中是不是已经安装了Nginx

这里直接使用如下命令即可:
ltwuyanlong@ubuntu:~$ nginx -v

Command 'nginx' not found, but can be installed with:

sudo apt install nginx-core  
sudo apt install nginx-extras
sudo apt install nginx-full  
sudo apt install nginx-light

二、开始安装Ngnix

1、查看80端口占用情况

$ lsof -i tcp:80

2、更新apt

$ sudo apt update  

3、安装nginx

$sudo apt-get install nginx

Ubuntu安装之后的文件结构大致为:所有的配置文件都在**/etc/nginx下,并且每个虚拟主机

已经安排在了
/etc/nginx/sites-available**下

安装成功之后:

xxx@ubuntu:/etc/nginx$ ls -l
total 64
drwxr-xr-x 2 root root 4096 Jan 10 11:18 conf.d
-rw-r--r-- 1 root root 1077 Apr  5  2018 fastcgi.conf
-rw-r--r-- 1 root root 1007 Apr  5  2018 fastcgi_params
-rw-r--r-- 1 root root 2837 Apr  5  2018 koi-utf
-rw-r--r-- 1 root root 2223 Apr  5  2018 koi-win
-rw-r--r-- 1 root root 3957 Apr  5  2018 mime.types
drwxr-xr-x 2 root root 4096 Jan 10 11:18 modules-available
drwxr-xr-x 2 root root 4096 Jun 22 04:56 modules-enabled
-rw-r--r-- 1 root root 1482 Apr  5  2018 nginx.conf
-rw-r--r-- 1 root root  180 Apr  5  2018 proxy_params
-rw-r--r-- 1 root root  636 Apr  5  2018 scgi_params
drwxr-xr-x 2 root root 4096 Jun 22 04:56 sites-available
drwxr-xr-x 2 root root 4096 Jun 22 04:56 sites-enabled
drwxr-xr-x 2 root root 4096 Jun 22 04:56 snippets
-rw-r--r-- 1 root root  664 Apr  5  2018 uwsgi_params
-rw-r--r-- 1 root root 3071 Apr  5  2018 win-utf

程序文件在**/usr/sbin/nginx**,日志放在了**/var/log/nginx**中

xxx@ubuntu:/var/log/nginx$ ls
access.log  error.log

并已经在/etc/init.d/下创建了启动脚本nginx

xxx@ubuntu:/etc/init.d$ ls
acpid       Apport          console-setup.sh  dbus       grub-common  keyboard-setup.sh  nginx          pppd-DNS  saned              ufw                  x11-common
alsa-utils  avahi-daemon    cron              dns-clean  hwclock.sh   kmod               open-vm-tools  procps    speech-dispatcher  unattended-upgrades
anacron     bluetooth       cups              Docker     irqbalance  .NETworking         plymouth       rsync     spice-vdagent      uuidd
apparmor    cgroupfs-mount  cups-browsed      gdm3       kerneloops   network-manager    plymouth-log   rsyslog   udev               whoopsie

在这下面确实创建了启动脚本。

默认的虚拟主机的目录设置在了**/var/www/nginx-default **(有的版本 默认的虚拟主机的目录设置在了/var/www, 请参考
/etc/nginx/sites-available里的配置)。

4、启动Nginx

xxx@ubuntu:~$ sudo /etc/init.d/nginx start 
[sudo] password for xxx: 
[ ok ] Starting nginx (via systemctl): nginx.service.

在启动之后在Ubuntu上面的浏览器中输入地址:

http://localhost/,假如输出如下的信息就证明启动成功了。

Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.

假如要在你客户端浏览器中访问的话需要使用安装Nginx服务器的IP地址进行访问。例如,首先查询IP地址:

	xxx@ubuntu:~$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:81:e9:1b brd ff:ff:ff:ff:ff:ff
    inet 192.168.6.128/24 brd 192.168.6.255 scope global dynamic noprefixroute ens33
       valid_lft 1205sec preferred_lft 1205sec
    inet6 fe80::5e9d:43aa:1a64:8fd/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
    link/ether 02:42:b7:2f:42:3b brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever

获取,如上图所示,查询到的IP为: 192.168.6.128.之后直接在浏览器中输入这个IP回车之后,反馈的信息为:

Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.

启动可能导致的问题:

如果不能访问,先不要继续,看看是什么原因,解决之后再继续。

启动时候若显示端口80被占用: Starting nginx: [emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use),修改文件:
/etc/nginx/sites-available/default,去掉 listen 前面的 # 号 , # 号在该文件里是注释的意思 , 并且把 listen 后面的 80 端口号改为自己的端口,访问是需要添加端口号。(安装完后如出现403错误,那可能是nginx配置文件里的网站路径不正确)

文件default里面的配置信息为:

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or WordPress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
        listen 80 default_server;
        listen [::]:80 default_server;

        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

        # pass PHP scripts to FastCGI server
"default" [readonly] 91 lines, 2416 characters

5、管理nginx

$ sudo systemctl status nginx  
$ sudo systemctl start nginx
$ sudo systemctl stop nginx
$ sudo systemctl restart nginx
$ sudo systemctl reload nginx
$ sudo systemctl enable nginx
$ sudo systemctl disable nginx

6、查看nginx配置文件

$ nginx -t

7、设置防火墙

$ sudo ufw allow 'Nginx Full'  
$ sudo ufw status  
声明:本站部分内容来自互联网,如有版权侵犯或其他问题请与我们联系,我们将立即删除或处理。
▍相关推荐
更多资讯 >>>