1、首先關閉防禦牆或者設置規則經過且關閉selinuxphp
中止firewallhtml
systemctl stop firewalld
禁止firewall開機啓動linux
systemctl disable firewalld
或設置firewall規則nginx
firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --reload
修改SELINUX=enforce行爲SELINUX=disabledc++
sed -i 's/SELINUX=setenforce 0/SELINUX=disabled/' /etc/sysconfig/selinux
2、nginx-1.14.2版本(編譯安裝)-自定義安裝路徑web
安裝路徑:/usr/local/nginxvim
1.前期準備centos
安裝編譯須要的gcc和gcc-c++緩存
yum install -y gcc gcc-c++
安裝nginx依賴pcre-devel、openssl-devel、zlib-devel服務器
yum install -y pcre pcre-devel openssl openssl-devel zlib zlib-devel
下載nginx源碼包並解壓到當前目錄
wget http://nginx.org/download/nginx-1.14.2.tar.gz tar zxvf nginx-1.14.2.tar.gz
2.nginx編譯安裝
生成Makefile文件
cd nginx-1.14.2 ./configure --user=nginx \ --group=nginx \ --prefix=/usr/local/nginx/ \ --with-http_v2_module \ --with-http_ssl_module \ --with-http_sub_module \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --with-pcre
編譯源代碼並安裝
make && make install
3.後期結尾
建立用戶
useradd nginx
添加環境變量,建立nginx命令軟連接到環境變量
ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
4.配置nginx開啓php支持(僅參考)
在server段中開啓php支持
找到以下內容,刪除註釋字符,並將倒數第二行的 /scripts 替換爲 $document_root
修改前
#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;
#}
修改後
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
該段代碼在server中的位置:
server {
listen 80;
server_name localhost;
location / {
root html;
index index.php index.html index.htm;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
注意:location ~ \.php$ {}塊中root的值和location / {}塊中root的值須要一致
3、開啓nginx目錄瀏覽
vim /usr/local/nginx/conf/nginx.conf
添加以下內容:
location / { root /usr/local/nginx/html/pack/ //指定實際目錄絕對路徑; autoindex on; //開啓目錄瀏覽功能; autoindex_exact_size off; //關閉詳細文件大小統計,讓文件大小顯示MB,GB單位,默認爲b; autoindex_localtime on; //開啓以服務器本地時區顯示文件修改日期! }
還有一個問題是這裏開啓的是全局的目錄瀏覽功能,那麼如何實現具體目錄瀏覽功能呢?(僅參考)
2. 只打開網站部分目錄瀏覽功能
只打開http://www.******.com/soft 目錄瀏覽
vi /usr/local/nginx/conf/nginx.conf #編輯配置文件,在server {下面添加如下內容:
location /soft {
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}
:wq! #保存,退出
4、建立目錄
在web根目錄下建立centosplus、extras、updates、os四個目錄
mkdir centosplus extras updates os
#這四個目錄用來區分類型(僅參考)
for DIR in $(ls); do cd $DIR; mkdir Packages; cd ..; done
#分別在四個目錄下建立存儲rpm包的目錄
5、利用rsync同步至本地
rsync -avz --delete rsync://mirrors.ustc.edu.cn/centos/6/os/x86_64/Packages/ /usr/local/nginx/html/pack/centos/6/os/x86_64/Packages/ rsync -avz --delete rsync://mirrors.ustc.edu.cn/centos/6/extras/x86_64/Packages/ /usr/local/nginx/html/pack/centos/6/extras/x86_64/Packages/ rsync -avz --delete rsync://mirrors.ustc.edu.cn/centos/6/updates/x86_64/Packages/ /usr/local/nginx/html/pack/centos/6/updates/x86_64/Packages/ rsync -avz --delete rsync://mirrors.ustc.edu.cn/centos/6/centosplus/x86_64/Packages/ /usr/local/nginx/html/pack/centos/6/centosplus/x86_64/Packages/
rsync -avz --delete rsync://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/ /usr/local/nginx/html/pack/centos/7/os/x86_64/Packages/ rsync -avz --delete rsync://mirrors.ustc.edu.cn/centos/7/extras/x86_64/Packages/ /usr/local/nginx/html/pack/centos/7/extras/x86_64/Packages/ rsync -avz --delete rsync://mirrors.ustc.edu.cn/centos/7/updates/x86_64/Packages/ /usr/local/nginx/html/pack/centos/7/updates/x86_64/Packages/ rsync -avz --delete rsync://mirrors.ustc.edu.cn/centos/7/centosplus/x86_64/Packages/ /usr/local/nginx/html/pack/centos/7/centosplus/x86_64/Packages/
或者同步所有數據(數據量較大不推薦,大小約136G)
rsync -avz --delete rsync://mirrors.ustc.edu.cn/centos/ /usr/local/nginx/html/pack/centos/
提供幾個支持rsync同步的網站
mirrors.tuna.tsinghua.edu.cn
mirrors.ustc.edu.cn
mirrors.kernel.org
mirrors.neusoft.edu.cn
6、建立倉庫
對三個目錄使用createrepo建立倉庫(生成repodata目錄),供client端檢索使用
yum install -y createrepo
createrepo /usr/local/nginx/html/pack/centos/6/os/x86_64/ createrepo /usr/local/nginx/html/pack/centos/6/extras/x86_64/ createrepo /usr/local/nginx/html/pack/centos/6/updates/x86_64/ createrepo /usr/local/nginx/html/pack/centos/6/centosplus/x86_64/
createrepo /usr/local/nginx/html/pack/centos/7/os/x86_64/ createrepo /usr/local/nginx/html/pack/centos/7/extras/x86_64/ createrepo /usr/local/nginx/html/pack/centos/7/updates/x86_64/ createrepo /usr/local/nginx/html/pack/centos/7/centosplus/x86_64/
#-o 指定repodata生成的目錄
此時yum服務器已經搭建完成
7、建立計劃任務
vim /etc/crontab
添加如下內容:
0 5 * * 1 root rsync -avz --delete rsync://mirrors.ustc.edu.cn/centos/ /usr/local/nginx/html/pack/centos/ >/dev/null 2>&1 #每週一5點執行同步命令
同步完成後須要更新倉庫
createrepo --update /usr/local/nginx/html/pack/centos/6/os/x86_64/ createrepo --update /usr/local/nginx/html/pack/centos/6/extras/x86_64/ createrepo --update /usr/local/nginx/html/pack/centos/6/updates/x86_64/ createrepo --update /usr/local/nginx/html/pack/centos/6/centosplus/x86_64/ createrepo --update /usr/local/nginx/html/pack/centos/7/os/x86_64/ createrepo --update /usr/local/nginx/html/pack/centos/7/extras/x86_64/ createrepo --update /usr/local/nginx/html/pack/centos/7/updates/x86_64/ createrepo --update /usr/local/nginx/html/pack/centos/7/centosplus/x86_64/
8、客戶端配置
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
cat >> /etc/yum.repos.d/CentOS-Base.repo << eof [base] name=CentOS-$releasever - Base baseurl=http://mirrors.yryun.com/centos/$releasever/os/$basearch/ enabled=1 gpgcheck=0 #released updates [updates] name=CentOS-$releasever - Updates baseurl=http://mirrors.yryun.com/centos/$releasever/updates/$basearch/ enabled=1 gpgcheck=0 #additional packages that may be useful [extras] name=CentOS-$releasever - Extras baseurl=http://mirrors.yryun.com/centos/$releasever/extras/$basearch/ enabled=1 gpgcheck=0 #additional packages that extend functionality of existing packages [centosplus] name=CentOS-$releasever - Plus baseurl=http://mirrors.yryun.com/centos/$releasever/centosplus/$basearch/ enabled=1 gpgcheck=0 eof
#清除全部緩存
yum clean all
#創建緩存
yum makecache
#查看yum源列表
yum repolist
#當yum服務器內容修改了以後或者修改了yum源文件,客戶機須要從新創建緩存
#baseurl指向倉庫(repodata)所在的目錄