Nginx服務的搭建與配置
Nginx服務的搭建與配置
一、關閉防火牆並安裝epel源
1、關閉selinux
①修改selinux的配置檔案
[root@localhost ~]# vim /etc/selinux/config
SELINUX=disabled
②關閉selinux
[root@localhost ~]# setenforce 0
2、關閉防火牆
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl enable firewalld
3、安裝epel.repo源
[root@localhost ~]# yum -y install epel-release.noarch
[root@localhost ~]# cd /etc/yum.repos.d/
二、Nginx原始碼包安裝
1、網站:https://nginx.org/download/
2、下載:wget https://nginx.org/download/nginx-1.19.9.tar.gz
三、設定nginx安裝源[如同安裝阿里雲源配置一樣]
1、安裝官方提供的Centos安裝nginx源
[root@www www]# rpm -Uvh https://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
2、或者手動新增nginx安裝源
[root@www www]# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgche=0
enabled=1
3、安裝Nginx
通過yum search nginx看看是否已經新增源成功。如果成功則執行下列命令安裝Nginx。
[root@www yum.repos.d]# yum -y install nginx
4、Nginx的配置
①Nginx的主配置檔案
[root@www ~]# vim /etc/nginx/nginx.conf
②Nginx配置支援PHP
在/etc/nginx/conf.d目錄下存放著多個配置檔案,這些配置項會在Nginx執行時載入到主配置專案中(類似虛擬機器)。Nginx是通過php-fpm來通訊的,所以需要監聽9000埠。
③在這個目錄下生成一個自己的配置檔案例如admin.conf,並進行新增php配置屬性資訊
[root@www nginx]# vim /etc/nginx/conf.d/admin.conf
server {
listen 80;#埠
server_name www.test.com admin.test.com;#域名
root /var/www/card/public;#網站根目錄
index index.php index.html index.htm;
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;#主要配置隱藏url中index.php
break;
}
}
location ~ \.php$ {
#root /var/www/card/public;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
④安裝php-fpm並修改使用者組和使用者名稱
[root@www nginx]#yum -y install php-fpm #安裝php-fpm
[root@www nginx]#vim /etc/php-fpm.d/www.conf #修改php-fpm配置檔案,使用者和使用者組預設是apache,改成nginx
; RPM: apache Choosed to be able to access some dir as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx
⑤建立php網站根目錄和PHP訪問頁面
[root@www www]# mkdir -p card/public
[root@www www]# vim /var/www/card/public/php_info.php
<!DOCTYPE html>
<html>
<body>
<?php echo "hello world";?>
</body>
</html>
⑥重啟php-fpm
[root@www var]# systemctl restart php-fpm #啟動php-fpm
[root@www var]# lsof -i:9000 #php-fpm埠是否正常啟動
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
php-fpm 55916 root 6u IPv4 2089492 0t0 TCP localhost:cslistener (LISTEN)
php-fpm 55921 nginx 0u IPv4 2089492 0t0 TCP localhost:cslistener (LISTEN)
php-fpm 55922 nginx 0u IPv4 2089492 0t0 TCP localhost:cslistener (LISTEN)
php-fpm 55923 nginx 0u IPv4 2089492 0t0 TCP localhost:cslistener (LISTEN)
php-fpm 55924 nginx 0u IPv4 2089492 0t0 TCP localhost:cslistener (LISTEN)
php-fpm 55925 nginx 0u IPv4 2089492 0t0 TCP localhost:cslistener (LISTEN)
⑦瀏覽器訪問PHP頁面
四、Nginx配置反向代理(使用者給使用者訪問的地址,紅色為反向代理的主要配置),後面補充完
[root@localhost conf.d]# vim /etc/nginx/conf.d/default.conf
upstream test{
server 192.168.81.130:8080 weight=1; #這裡的IP是後端伺服器IP地址,權重1
server 192.168.81.131:8080 weight=1;
}
server {
listen 80;
server_name www.test.com;
#access_log /var/log/nginx/host.access.log main;
location / {
proxy_pass http://test; #這裡可直接寫IP地址進行配置,如果需要配置負載均衡,可以只有http://test 和 upstream名稱一樣
#以下是一些反向代理的配置,可刪除
#root /usr/share/nginx/html;
#index index.html index.htm;
proxy_redirect off;
proxy_set_header Host $host; #指定請求伺服器域名和埠號
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}