1. 程式人生 > 實用技巧 >部署Nginx服務

部署Nginx服務

部署Nginx服務

案例實施

1.基礎環境安裝

1)修改主機名

[root@localhost ~]# hostnamectl set-hostname nginx
[root@localhost ~]# bash
[root@nginx ~]# hostnamectl
   Static hostname: nginx
         Icon name: computer-vm
           Chassis: vm
        Machine ID: a2c6f463b1b24cc3a32a4f0049238e3e
           Boot ID: 55e972753e6d40b9a05652293b8700dc
    Virtualization: vmware
  Operating System: CentOS Linux 
7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 3.10.0-327.el7.x86_64 Architecture: x86-64 [root@nginx ~]#

2)關閉防火牆及SELinux服務

[root@nginx ~]# setenforce 0
[root@nginx ~]# systemctl stop firewalld

3)安裝配置基礎服務

使用CentOS-7-x86_64-DVD-1511.iso檔案自行配置本地YUM源,編譯安裝基礎環境,命令如下

:

[root@nginx ~]# yum install gcc gcc-c++ openssl-devel zlib-devel zlib pcre-devel -y

建立指定使用者,這個nginx使用者要和 PHP伺服器上建立的nginx兩者id一致,這裡先建立使用者,命令如下:

完畢!
[root@nginx ~]# groupadd -g 1001 nginx
[root@nginx ~]# useradd -u 900 nginx -g nginx -s /sbin/nologin 
[root@nginx ~]# tail -l /etc/passwd

(4)安裝配置Nginx服務

使用遠端傳輸工具

,將提供的nginx-1.18.0.tar.gz壓縮包上傳至nginx節點的/usr/local/srcl目錄下,並解壓到當前目錄,命令如下:

[root@nginx src]# tar xzvf nginx-1.18.0.tar.gz

進入nginx-1.18.0目錄,編譯並安裝,命令如下:

[root@nginx nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --with-http_dav_module 
--with-http_stub_status_module --with-http_addition_module --with-http_sub_module
--with-http_flv_module --with-http_mp4_module --with-http_ssl_module
--with-http_gzip_static_module --user=nginx --group=nginx

如果沒有報錯提示,請進行下一步安裝,命令如下:

[root@nginx nginx-1.18.0]# make && make install

編譯安裝完畢後,建立軟連線並啟動測試,命令如下: (netstat命令無法使用時,請自行使用YUM源安裝net-tools工具)

[root@nginx nginx-1.18.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@nginx nginx-1.18.0]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx nginx-1.18.0]# nginx 
[root@nginx nginx-1.18.0]# netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      25180/nginx: master 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1445/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1854/master         
tcp6       0      0 :::22                   :::*                    LISTEN      1445/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1854/master         

如果發現80埠啟動,則表示 Nginx服務啟動成功。可以在瀏覽器訪問地址192.168.117.99來檢視是否出現Nginx的歡迎頁面。