二、源碼安裝最新nginx1.16.0
2.1安裝編譯工具及庫文件
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
2.2安裝 PCRE
注意安裝前先查看有沒有PCRE,有則跳過2.2這一步!
pcre-config --version
2.2.1下載PCRE
PCRE 作用是讓 Nginx 支持 Rewrite 重寫功能,例如隱藏URL的某路徑,TP5中的index.php就需要用到Rewrite,當然這是後話。
PCER下載地址:http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
下載好使用Xftp 6複製到服務器中(爲什麼不直接下載呢,因爲這樣比較快)
或者可能超慢的
wget -c http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
2.2.2解壓
cd /Downloads
(你剛纔把壓縮包複製到的位置)
tar -zxvf pcre-8.35.tar.gz
2.2.3配置
注意:指定了安裝目錄,我是/etc/pcre-8.35。不指定目錄的安裝到時卸載有可能需要手動一個個刪除(微笑)
cd pcre-8.35
./configure --prefix=/etc/pcre-8.35
2.2.4編譯&安裝
make && make instsall
2.2.5查看版本
pcre-config --version
2.3安裝nginx
2.3.1下載nginx
nginx下載地址:http://nginx.org/download/nginx-1.16.0.tar.gz (建議)
流程參考2.3.1
或者wget -c http://nginx.org/download/nginx-1.16.0.tar.gz
2.3.2解壓
cd /Downloads
tar -zxvf nginx-1.16.0.tar.gz
2.3.3配置指定路徑
cd nginx-1.16.0
(這裏沒指定上面安裝好的pcre是因爲我的系統已經下載好了,避免安裝出現問題,使用時在conf指定位置即可)
./configure --prefix=/etc/nginx --with-http_ssl_module
2.3.4編譯並安裝
make && make install
清理編譯文件
make clean
2.3.5查看安裝版本
/etc/nginx/sbin/nginx -v
2.3.6設置服務啓動+開機啓動(編碼安裝需要自己編寫)
cd /etc/init.d/nginx
在init.d中新建一個nginx啓動文件,可能你多我一層目錄,在init.d中
vi nginx
將以下代碼粘貼進去,目錄不是/etc/nginx的記得更改
#! /bin/bash
# chkconfig: - 85 15
# description: nginx is a World Wide Web server. It is used to serve
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ “KaTeX parse error: Expected 'EOF', got '&' at position 22: …KING" = "no" ] &̲& exit 0
nginx…(basename $nginx)
NGINX_CONF_FILE=”/etc/nginx/conf/nginx.conf"
[ -f /etc/nginx/sbin/nginx ] && ./etc/nginx/sbin/nginx
lockfile=/etc/nginx/logs/nginx.lock
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c ?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc ?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc ?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case “$1” in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $“Usage: $0{start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}”
exit 2
esac
– VISUAL –
在init.d目錄下
chmod +x nginx
chkconfig
chkconfig --add nginx
chkconfig nginx on
添加成功
systemctl list-dependencies nginx
systemctl daemon-reload
2.3.7nginx.conf配置文件
vi /etc/nginx/conf/nginx.conf
以下是我的nginx.conf
user www;
worker_processes auto;
error_log /etc/nginx/logs/error.log;
pid /run/nginx.pid;
\# Load dynamic modules. See /usr/share/nginx/README.dynamic.
\# 模塊目錄待更改
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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 /etc/nginx/logs/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/conf/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
# include /etc/nginx/conf/nginx.conf.default;
#服務器配置
server {
listen 80;
server_name localhost;
root /var/www;
index index.html index.htm index.php;
error_page 404 /404.html;
location = /404.html {
return 404 'Sorry, File not Found!';
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
#還沒做這個頁面
root /usr/share/nginx/html; # windows用戶替換這個目錄
}
location /myProject/ {
try_files $uri @rewrite;
}
location @rewrite {
set $static 0;
if ($uri ~ \.(css|js|jpg|jpeg|png|gif|ico|woff|eot|svg|css\.map|min\.map)$) {
set $static 1;
}
if ($static = 0) {
rewrite ^/myProject/JM-yingfu/(.*)$ /myProject/JM-yingfu/index.php?s=/$1 last;
}
}
#拒絕所有.php格式的文件上傳
location ~ /Uploads/.*\.php$ {
deny all;
}
location ~ \.php/ {
if ($request_uri ~ ^(.+\.php)(/.+?)($|\?)) { }
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_NAME $1;
fastcgi_param PATH_INFO $2;
fastcgi_param SCRIPT_FILENAME $document_root$1;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
需要注意以下3點:
1、軟件目錄和網站目錄,網站目錄我是選擇放在/var/www
2、我進行了二級目錄也就是myProject,並且使用rewirite重寫隱藏了index.php,所以直接指定了項目,具體查找myProject那部分代碼。假如你不需要二級目錄和隱藏項目中的index.php,只需要使用查找替換myProject/和JM-yingfu/爲空,前者是二級目錄,後者是我的項目名。
3、複製粘貼完後注意vi進去查看粘貼的代碼是否完整
4、下面是我遇到的一些問題,因爲我已經在nginx.conf做了修改,所以你不會遇到!!可跳過
遇到問題了一個個解決吧0.0
systemctl status nginx.service
1、mime.types文件缺失
find / -name mime.types
目錄不對,複製一份
cp /etc/nginx/conf/mime.types /etc/nginx/mime.types
service nginx restart
2、重啓後,顯示user不允許在這
google一番後,猜測是這裏的問題,結果確實是
看了下只有這個配置文件,所以只引入nginx.conf.default即可
service nginx restart
3、重啓後,和2類似的問題
google一番後,依然是上面include的問題,因爲nginx.conf和nginx.conf.default均含有worker_processes的定義。所以
刪掉nginx.conf.default中的wroker_process,只保留一個
service nginx restart
4、重啓後,又是類似的問題(手動微笑)
刪掉nginx.conf.default中的events方法,只保留nginx.conf一個定義
service nginx restart
5、重啓後,又是上面的問題。(從yum安裝的conf複製的源碼安裝conf就是事多)
到了這裏我發現nginx.conf.default都刪沒了。。所以我直接nginx.conf中的include直接註釋掉(微笑)
service nginx restart
6、重啓後,成功!不過出現了警告
按照提示
systemctl daemon-reload
完成!
可以使用service和systemctl命令操作,一樣的
狀態|重啓|開啓|停止|重載
service nginx states|restart|start|stop|reload
systemctl states|restart|start|stop|reload nginx
2.3.8創建網站的目錄
在nginx中我們指定的目錄是/var/www,所以需要創建個目錄作爲網站根目錄
mkdir /var/www
創建個index.php測試
vi /var/www/index.php
在你的瀏覽器輸入你的雲服務器地址或者127.0.0.1
(這裏之所以不用輸入index.php就能訪問到index.php,是因爲我們nginx.conf中對url進行了重寫,自動填充了index.php)