Centos如何安裝nginx?本教程以centos7系統(tǒng)為例
本配置適用于centos7版本
1.安裝vim編輯工具
yum install vim -y
2.安裝lrzsz上傳工具
yum install lrzsz -y
安裝完后可以使用rz命令上傳所需要的軟件工具(最好進(jìn)入家目錄/home在上傳方便以后查找)
3.指定環(huán)境版本和上傳環(huán)境安裝包
nginx-1.12.2.tar(具體版本不一定,這邊只是隨機(jī)安裝1個(gè)版本,具體看需要什么版本)
4.安裝關(guān)聯(lián)包
yum -y install pcre-devel
yum -y install openssl openssl-devel
yum -y install gcc-c
5.安裝nginx-1.12.2.tar
6.進(jìn)入上傳文件目錄解壓nginx-1.12.2.tar文件
[root@vt178m5c home]# tar -zxvf nginx-1.12.2.tar.gz
7.進(jìn)入nginx-1.12.2解壓文件開始編譯安裝nginx
[root@vt178m5c home]# cd nginx-1.12.2
[root@vt178m5c nginx-1.12.2]# ./configure --prefix=/usr/local/nginx \
> --with-http_stub_status_module \
> --with-http_gzip_static_module \
> --with-http_flv_module \
> --with-http_ssl_module \
> --http-client-body-temp-path=/usr/local/nginx/client_body_temp \
> --http-fastcgi-temp-path=/usr/local/nginx/fastcgi_temp \
> --http-proxy-temp-path=/usr/local/nginx/proxy_temp \
> --http-uwsgi-temp-path=/usr/local/nginx/uwsgi_temp \
> --http-scgi-temp-path=/usr/local//nginx/scgi_temp
8.編譯完確認(rèn)沒有報(bào)錯(cuò)后開始安裝nginx
[root@vt178m5c nginx-1.12.2]# make && make install
9.修改nginx配置文件
[root@vt178m5c nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf
找到
#error_log logs/error.log;
#access_log logs/access.log main;
取消注釋與修改
error_log logs/error.log;
access_log logs/access.log ;
找到
index index.html index.htm;
修改為
index index.php index.html index.htm;
找到
取消注釋, /scripts$fastcgi_script_name;修改為$document_root$fastcgi_script_name;
這個(gè)配置的意思是 在瀏覽器中訪問的.php文件,實(shí)際讀取的是 $document_root(網(wǎng)站根目錄)下的.php文件 -- 也就是說當(dāng)訪問127.0.0.1/index.php的時(shí)候,需要讀取網(wǎng)站根目錄下面的index.php文件,如果沒有配置這一配置項(xiàng)時(shí),nginx不會(huì)去網(wǎng)站根目錄下訪問.php文件,所以返回空白
配置項(xiàng)目中:include fastcgi_params; fastcgi_params 文件中含有各個(gè)nginx常量的定義,默認(rèn)情況 SCRIPT_FILENAME = $fastcgi_script_name
10.編寫nginx啟動(dòng)腳本
[root@vt178m5c nginx-1.12.2]# vim /etc/init.d/nginx
腳本代碼:
#! /bin/bash
#chkconfig: 2345 80 90
#description:nginx run
# nginx啟動(dòng)腳本
# @author liut
# @version 0.0.1
# @date 2018-2-9
PATH=/usr/local/nginx/conf
DESC="nginx daemon"
NAME=nginx
DAEMON=/usr/local/nginx/sbin/nginx
CONFIGFILE=$PATH/$NAME.conf
PIDFILE=$PATH/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
set -e
[ -x "$DAEMON" ] || exit 0
do_start()
{
$DAEMON -c $CONFIGFILE || echo -n "nginx already running"
}
do_stop()
{
$DAEMON -s stop || echo -n "nginx not running"
}
do_reload()
{
$DAEMON -s reload || echo -n "nginx can't reload"
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
do_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
do_stop
echo "."
;;
reload|graceful)
echo -n "Reloading $DESC configuration..."
do_reload
echo "."
;;
restart)
echo -n "Restarting $DESC: $NAME"
do_stop
do_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2
exit 3
;;
esac
exit 0
11.編寫完腳本記得修改下腳本權(quán)限
12.nginx啟動(dòng)|停止|重啟
[root@vt178m5c nginx-1.12.2]# /etc/init.d/nginx start 啟動(dòng)
[root@vt178m5c nginx-1.12.2]# /etc/init.d/nginx stop 停止
[root@vt178m5c nginx-1.12.2]# /etc/init.d/nginx restart 重啟