Ubuntu安裝搭建Nginx伺服器
Ubuntu下安裝Nginx
一、系統基本資訊檢視
1、檢視Ubuntu版本資訊;
使用指令:cat /proc/version 檢視
~$ cat /proc/version
Linux version 4.15.0-106-generic ([email protected]) (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
這裡直接使用如下指令即可:
[email protected]:~$ 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**下
安裝成功之後:
[email protected]:/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**中
[email protected]:/var/log/nginx$ ls
access.log error.log
并已經在/etc/init.d/下建立了啟動腳本nginx
[email protected]:/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
[email protected]:~$ 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位址:
[email protected]:~$ 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
PS: 還有一種安裝Nginx的方式為——源代碼安裝,具體操作流程可以參照博文: 原文
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
參照博文:
https://www.jianshu.com/p/7cb1a824333e
https://juejin.im/post/5db823545188256edc46e938