測試環境:
[root@centos-linux ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
[root@centos-linux ~]# php --version
PHP 7.2.32 (cli)
首先安裝必要的庫(nginx 中gzip模塊需要 zlib 庫,rewrite模塊需要 pcre 庫,ssl 功能需要openssl庫)
1、安裝相關的依賴包。
yum install -y gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel
2、下載Nginx
wget http://nginx.org/download/nginx-1.18.0.tar.gz
3、解壓安裝包
tar zxvf nginx-1.18.0.tar.gz
4、進入安裝包目錄
cd nginx-1.18.0/
5、編譯安裝
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make
make install
6、創建 Nginx 運行使用的用戶 www:
/usr/sbin/groupadd www
/usr/sbin/useradd -g www www
7、nginx.conf最小配置(/usr/local/nginx/conf/nginx.conf),紅色為新增內容
user www www; worker_processes 1; error_log /usr/local/nginx/logs/nginx_error.log crit; #日志位置和日志級別 pid /usr/local/nginx/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; index index.html index.htm index.php; root /usr/local/nginx/html;#站點目錄 location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
8、檢查配置文件正確性的命令
# /usr/local/nginx/sbin/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
9、啟動Nginx
/usr/local/nginx/sbin/nginx

10、站點訪問

11、Nginx常用命令
/usr/local/webserver/nginx/sbin/nginx -s reload # 重新載入配置文件
/usr/local/webserver/nginx/sbin/nginx -s reopen # 重啟Nginx
/usr/local/webserver/nginx/sbin/nginx -s stop # 停止 Nginx
12、Nginx調用PHP
先啟動php-fpm
systemctl start php-fpm // 需要加入systemctl服務后才可以
在配置文件中增加如下內容
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
# include fastcgi.conf; #或用此句替代以上兩句。不通版本的nginx不一定都包含該文件,如果沒有該配置文件,則使用上面兩句話。
}
13、使用yum安裝Nignx
// yum -y install epel-release // 如果需要添加CentOS EPEL倉庫 yum -y install nginx
啟動Nginx
systemctl start nginx
14、卸載Nginx
查找nginx相關文件
find / -name nginx*
從源頭刪除
rm -rf /usr/sbin/nginx rm -rf /etc/nginx rm -rf /etc/init.d/nginx
在使用yum清理
yum remove nginx
15、設置Nginx開機自啟動
在/lib/systemd/system目錄下,創建nginx.service文件
編輯nginx.service文件,增加如下內容
[Unit] Description=nginx service After=network.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s quit PrivateTmp=true [Install] WantedBy=multi-user.target
# systemctl start nginx.service // 啟動nginx服務 # systemctl stop nginx.service // 停止服務 # systemctl restart nginx.service // 重新啟動服務 # systemctl list-units --type=service // 查看所有已啟動的服務 # systemctl status nginx.service // 查看服務當前狀態 # systemctl enable nginx.service // 設置開機自啟動 # systemctl disable nginx.service //停止開機自啟動
可能遇到的錯誤提示:
