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