一,nginx的官網:
http://nginx.org/
說明:劉宏締的架構森林是一個專注架構的博客,地址:https://www.cnblogs.com/architectforest
對應的源碼可以訪問這里獲取: https://github.com/liuhongdi/
說明:作者:劉宏締 郵箱: 371125307@qq.com
二,下載與解壓nginx
1,下載
[root@centos8 source]# wget http://nginx.org/download/nginx-1.18.0.tar.gz
2, 解壓縮
[root@centos8 source]# tar -zxvf nginx-1.18.0.tar.gz
三,編譯與安裝nginx
1,configure
#--prefix 指定安裝路徑
#--with-http_stub_status_module 允許查看nginx狀態的模塊
# --with-http_ssl_module 支持https的模塊
[root@centos8 nginx-1.18.0]# ./configure --prefix=/usr/local/soft/nginx-1.18.0 --with-http_stub_status_module --with-http_ssl_module
2,編譯並安裝
[root@centos8 nginx-1.18.0]# make && make install
四, configure報錯的解決
說明:如果相應的軟件包已安裝不會報錯,寫在這里供參考
1,問題1,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.
解決:
[root@os3 nginx-1.18.0]# yum install pcre-devel
2,問題2,configure提示:
./configure: error: SSL modules require the OpenSSL library. You can either do not enable the modules, or install the OpenSSL library into the system, or build the OpenSSL library statically from the source with nginx by using --with-openssl=<path> option.
解決:
[root@os3 nginx-1.18.0]# yum install openssl openssl-devel
五,測試編譯安裝的效果:查看nginx的版本
[root@centos8 nginx-1.18.0]# /usr/local/soft/nginx-1.18.0/sbin/nginx -v nginx version: nginx/1.18.0
六,如何查看nginx的配置編譯參數?
[root@centos8 nginx-1.18.0]# /usr/local/soft/nginx-1.18.0/sbin/nginx -V nginx version: nginx/1.18.0 built by gcc 8.3.1 20190507 (Red Hat 8.3.1-4) (GCC) built with OpenSSL 1.1.1c FIPS 28 May 2019 TLS SNI support enabled configure arguments: --prefix=/usr/local/soft/nginx-1.18.0 --with-http_stub_status_module --with-http_ssl_module
注意區分和上一條查看版本命令的區別: -v參數分別是小寫和大寫
七,使systemctl能管理運行nginx服務
1,運行的准備工作:配置日志目錄
[root@centos8 conf]# mkdir /data/nginx [root@centos8 conf]# mkdir /data/nginx/logs
2,運行的准備工作:創建nginx用戶
[root@centos8 conf]# groupadd nginx
#-g:指定所屬的group
#-s:指定shell,因為它不需要登錄,所以用/sbin/nologin
#-M:不創建home目錄,因為它不需要登錄
[root@centos8 conf]# useradd -g nginx -s /sbin/nologin -M nginx
3,簡單配置nginx
[root@centos8 conf]# vi nginx.conf
內容:
指定運行nginx的用戶和組是:nginx
user nginx nginx;
發生錯誤時要寫入到錯誤日志(目錄用上面創建好的)
error_log /data/nginx/logs/error.log;
指定pid的路徑
pid logs/nginx.pid;
日志格式(取消注釋即可)
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';
指定訪問日志的路徑和格式(目錄用上面創建好的)
access_log /data/nginx/logs/access.log main;
4,生成service文件:
[root@centos8 ~]# vi /usr/lib/systemd/system/nginx.service
內容:
[Unit] Description=nginx-The High-performance HTTP Server After=network.target [Service] Type=forking PIDFile=/usr/local/soft/nginx-1.18.0/logs/nginx.pid ExecStartPre=/usr/local/soft/nginx-1.18.0/sbin/nginx -t -c /usr/local/soft/nginx-1.18.0/conf/nginx.conf ExecStart=/usr/local/soft/nginx-1.18.0/sbin/nginx -c /usr/local/soft/nginx-1.18.0/conf/nginx.conf ExecReload=/usr/local/soft/nginx-1.18.0/sbin/nginx -s reload ExecStop=/usr/local/soft/nginx-1.18.0/sbin/nginx -s stop PrivateTmp=true [Install] WantedBy=multi-user.target
5,啟動服務
重新加載服務文件
[root@centos8 ~]# systemctl daemon-reload
啟動:
[root@centos8 ~]# systemctl start nginx
6,查看效果:
從瀏覽器訪問安裝機器的ip的80端口即可:
看例子截圖:
7,查看日志目錄
[root@centos8 conf]# ll /data/nginx/logs/ 總用量 8 -rw-r--r-- 1 root root 1477 4月 22 18:49 access.log -rw-r--r-- 1 root root 1195 4月 22 18:32 error.log
日志已成功寫入
八,查看centos版本
[root@centos8 ~]# cat /etc/redhat-release CentOS Linux release 8.1.1911 (Core)