linux服務器中nginx服務的2種安裝方法

示例服務器操作系統是centos7,最小化安裝,只關閉selinux
一、yum安裝nginx服務
A、基礎條件設置
nginx服務的yum源設置

a.  安裝wget軟件
yum install wget -y 

b.  備份和修改基礎倉庫到163
cd /etc/yum.repos.d/  && mv CentOS-Base.repo CentOS-Base.repo.back
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo

c.  安裝epel倉庫和修改epel倉庫地址到阿里雲
yum install epel-release -y
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

d.  可選:不使用epel源安裝nginx,使用nginx.org的官方源,劣勢是速度慢,優勢是nginx版本新。手動建立/etc/yum.repos.d/nginx.repo
cat >> /etc/yum.repos.d/nginx.repo <<EOF
[nginx-stable] 
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0 
enabled=1 
gpgkey=https://nginx.org/keys/nginx_signing.key 
module_hotfixes=true
EOF

B、正式安裝nginx服務
nginx服務安裝
yum install nginx openssl openssl-devel zlib zlib-devel pcre pcre-devel -y

二、編譯安裝nginx服務
基礎條件設置和yum安裝相同
正式安裝nginx服務
1、下載nginx源碼包
wget -O /usr/local/src/nginx-1.17.10.tar.gz http://nginx.org/download/nginx-1.17.10.tar.gz

2、安裝依賴包
yum -y install openssl openssl-devel zlib zlib-devel pcre pcre-devel

3、添加運行nginx服務的用戶
useradd -s /sbin/nologin -M nginx

4、解壓nginx源代碼,並且切換目錄到解壓後的nginx目錄中
cd /usr/local/src
tar zxvf nginx-1.17.10.tar.gz
cd nginx-1.17.10

5、編譯安裝,在示例中,只with了部份模塊
./configure --prefix=/usr/local/nginx-1.17.10 --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre --with-http_flv_module
make -j 4
make install

6、nginx可執行命令做軟鏈接
ln -s /usr/local/nginx-1.17.10/sbin/nginx /usr/local/sbin/nginx

三、nginx服務啓動命令或啓動腳本
a. 檢查nginx配置
nginx -t

b. 啓動nginx
nginx 或者 systemctl start nginx

3、重啓nginx
nginx -s reload 或者 systemctl restart nginx

4、關閉nginx
nginx -s stop 或者 systemctl stop nginx

5、nginx 的systemctl 控制腳本位置 (如果希望nginx服務在操作系統重啓後隨機啓動。修改命令是:systemctl enable nginx)
/usr/lib/systemd/system/nginx.service

注: systemctl的啓動腳本在yum安裝後會自動生成,編譯安裝想使用systemctl命令啓動nginx服務需要手動寫啓動腳本,可以參考yum安裝nginx的啓動腳本

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章