Ubuntu編譯安裝nginx以及配置自動啟動

  • 2019 年 11 月 3 日
  • 筆記

本文主要介紹ubuntu如何編譯安裝nginx以及遇到的問題 和 配置系統自動啟動服務

 

查看操作系統版本
cat /etc/issue 
Ubuntu 18.04.3 LTS n l 
 
更改鏡像源步驟:
1. 首先做一個備份,這是個好習慣,修改配置文件前先備份,以便於事後可以恢復
sudo su  進入root用戶安裝
cd /etc/apt
mv sources.list sources.list.bak
touch sources.list
chmod 777 sources.list
vim sources.list
2. 中科大最新鏡像源配置內容複製寫入

deb https://mirrors.ustc.edu.cn/ubuntu/ focal main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ focal-security main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-security main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
## Not recommended
# deb https://mirrors.ustc.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse

 

3.更新鏡像源
apt update
4.更新軟件
apt list –upgradable
 
安裝nginx大概需要如下依賴庫 gcc  pcre  zlib openssl libssl-dev libperl-dev zlib等依賴,安裝過程中或許會出現報錯,需要什麼依賴安裝什麼依賴。
apt install tar
apt install gcc
apt install libpcre3 libpcre3-dev
apt install openssl libssl-dev libperl-dev
apt install zlib1g zlib1g-dev

下載安裝nginx步驟

1.下載nginx穩定版本,並安裝。如下:

wget http://nginx.org/download/nginx-1.16.1.tar.gz
tar -zxvf nginx-1.16.1.tar.gz
cd nginx-1.16.1

執行:
./configure

執行:

make

最後執行:

make install

就安裝好了!

此處整理安裝nginx過程中nginx所需要的類庫沒有安裝出現的報錯:

./configure
報錯:
checking for OS
+ Linux 5.0.0-32-generic x86_64
checking for C compiler … not found
./configure: error: C compiler cc is not found
執行:apt install gcc
./configure
報錯:
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using –without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using –with-pcre=<path> option.
執行
apt install libpcre3 libpcre3-dev
出錯:鏡像源中沒有找到 pcre-devel 庫
root@ubuntu:~/nginx-1.16.1# apt install pcre-devel
Reading package lists… Done
Building dependency tree 
Reading state information… Done
E: Unable to locate package pcre-devel
./configure
出錯
ubuntu error: the HTTP rewrite module requires the PCRE library.
apt install libpcre3 libpcre3-dev
./configure
出錯:
./configure: error: the HTTP gzip module requires the zlib library.
apt install libssl-dev libperl-dev
./configure: error: the HTTP gzip module requires the zlib library.
apt install zlib1g zlib1g-dev
./configure
Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ using system zlib library
nginx path prefix: “/usr/local/nginx”
nginx binary file: “/usr/local/nginx/sbin/nginx”
nginx modules path: “/usr/local/nginx/modules”
nginx configuration prefix: “/usr/local/nginx/conf”
nginx configuration file: “/usr/local/nginx/conf/nginx.conf”
nginx pid file: “/usr/local/nginx/logs/nginx.pid”
nginx error log file: “/usr/local/nginx/logs/error.log”
nginx http access log file: “/usr/local/nginx/logs/access.log”
nginx http client request body temporary files: “client_body_temp”
nginx http proxy temporary files: “proxy_temp”
nginx http fastcgi temporary files: “fastcgi_temp”
nginx http uwsgi temporary files: “uwsgi_temp”
nginx http scgi temporary files: “scgi_temp”
make
make install

接下來配置nginx的自動啟動管理服務

配置之前先啰嗦幾句對ubuntu系統的應有的了解:

Ubuntu 16.10開始不再使用initd管理系統,改用systemd,包括用systemctl命令來替換了service和chkconfig的功能。

比如以前啟動mysql服務用sudo service mysql start,現在用sudo systemctl start mysqld.service。

systemd 默認讀取 /etc/systemd/system 下的配置文件,該目錄下的文件會鏈接/lib/systemd/system/下的文件。

1.建立服務文件
vim lib/systemd/system/nginx.service

複製如下內容寫入到nginx.service中
[Unit]
Description=nginx – high performance web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
[Install]
WantedBy=multi-user.target

[Unit]:服務的說明
Description:描述服務
After:依賴,當依賴的服務啟動之後再啟動自定義的服務

[Service]服務運行參數的設置
Type=forking是後台運行的形式
ExecStart為服務的具體運行命令(需要根據路徑適配)
ExecReload為重啟命令(需要根據路徑適配)
ExecStop為停止命令(需要根據路徑適配)
PrivateTmp=True表示給服務分配獨立的臨時空間
注意:啟動、重啟、停止命令全部要求使用絕對路徑

[Install]服務安裝的相關設置,可設置為多用戶

Type
Type=simple(默認值):systemd認為該服務將立即啟動。服務進程不會fork。如果該服務要啟動其他服務,不要使用此類型啟動,除非該服務是socket激活型。
Type=forking:systemd認為當該服務進程fork,且父進程退出後服務啟動成功。對於常規的守護進程(daemon),除非你確定此啟動方式無法滿足需求,使用此類型啟動即可。使用此啟動類型應同時指定 PIDFile=,以便systemd能夠跟蹤服務的主進程。
Type=oneshot:這一選項適用於只執行一項任務、隨後立即退出的服務。可能需要同時設置 RemainAfterExit=yes使得systemd在服務進程退出之後仍然認為服務處於激活狀態
Type=notify:與 Type=simple相同,但約定服務會在就緒後向systemd發送一個信號。這一通知的實現由 libsystemd-daemon.so提供。
Type=dbus:若以此方式啟動,當指定的 BusName 出現在DBus系統總線上時,systemd認為服務就緒。
PIDFile : pid文件路徑
ExecStartPre :啟動前要做什麼,上文中是測試配置文件 -t
2.保存目錄
/usr/lib/systemd/system
3.設置開機自啟動
systemctl enable nginx.service //任意目錄下執行
4.使用命令

systemctl start nginx.service      //啟動nginx服務
systemctl enable nginx.service   //設置開機自動啟動
systemctl disable nginx.service  //停止開機自動啟動
systemctl status nginx.service   //查看狀態
systemctl restart nginx.service  //重啟服務
systemctl list-units –type=service  //查看所有服務