編譯安裝nginx並使用systemctl方式管理

2021-10-02 16:40:27 字數 2807 閱讀 3166

分為兩個大部分:

第一部分:編譯安裝nginx

01 安裝gcc編譯器以及編譯安裝的依賴

yum -y install gcc gcc-c++ wget  openssl openssl-devel
02 進入src目錄

cd /usr/local/src/
wget
04 解壓並進入目錄開始編譯三部曲

install05 檢查:返回值為0則是執行成功

echo

$?

06 啟動nginx並檢視

nginx

ps -ef |

grep nginx

第二部分:systemctl方式管理

01 建立配置檔案

原始碼安裝的nginx在/etc/systemd/system/multi-user.target.wants/目錄下是沒有nginx.service這個檔案的,所以要新建

vim /usr/lib/systemd/system/nginx.service
02 寫入內容:

[unit]

description=nginx - high performance web server

documentation=

after=network-online.target remote-fs.target nss-lookup.target

wants=network-online.target

[service]

type=forking

pidfile=/usr/local/nginx/logs/nginx.pid

execstart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

execreload=/bin/kill -s hup $mainpid

execstop=/bin/kill -s term $mainpid

[install]

wantedby=multi-user.target

03 設定開機啟動

systemctl enable nginx.service
04 關閉之前啟動的nginx(編譯完成後nginx啟動的nginx服務程式)

pkill    -9  nginx
05 過載nginx配置檔案

systemctl daemon-reload
06 重新啟動nginx服務

systemctl start nginx
檢視nginx服務執行狀態

[root@localhost conf]

# systemctl status nginx

● nginx.service - nginx - high performance web server

loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)

active: active (running) since sun 2020-02-09 09:32:55 est; 4min 11s ago

docs:

process: 7564 execstart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf (code=exited, status=0/success)

main pid: 7565 (nginx)

cgroup: /system.slice/nginx.service

├─7565 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

└─7566 nginx: worker process

feb 09 09:32:55 localhost.localdomain systemd[1]: starting nginx - high performance web server...

feb 09 09:32:55 localhost.localdomain systemd[1]: pid 7342 read from file /usr/local/nginx/logs/nginx.pid does not exist ...mbie.

feb 09 09:32:55 localhost.localdomain systemd[1]: started nginx - high performance web server.

hint: some lines were ellipsized, use -l to show in full.

完全沒毛病

Nginx安裝並使用

本文介紹了nginx安裝過程和使用方法 參考 1.2.1 安裝依賴 yum y install gcc zlib zlib devel pcre devel openssl openssl devel 1 先進入到目錄 3 解壓 tar xvf nginx 1.13 7.tar.gz 4.1 編譯n...

docker安裝nginx並使用nginx

1.搜尋安裝的 nginx 映象 docker search nginx2.在docker hub 中選擇合適的版本後進行 映象拉取 docker pull nginx3.拉取完成後執行 nginx 容器 docker run di name nginx p 80 80 v usr docker n...

nginx編譯安裝

nginx版本 1.12.2 1.安裝依賴包 yum install y gcc gcc c zlib zlib devel openssl openssl devel pcre pcre devel 2.編譯和安裝nginx 執行如下命令 3.安裝完成啟動nginx。如果用指定使用者啟動nginx...