12.6 Nginx安裝

Nginx安裝目錄概要

Nginx安裝

  1. 切換到/usr/local/src/目錄下
[root@hanfeng ~]# cd /usr/local/src/
[root@hanfeng src]#
  1. 下載Nginx安裝包
[root@hanfeng src]# wget http://nginx.org/download/nginx-1.12.1.tar.gz
  1. 解壓安裝包
[root@hanfeng src]# tar zxf nginx-1.12.1.tar.gz
[root@hanfeng src]#
  1. 切換到nginx-1.12.1目錄下
[root@hanfeng src]# cd nginx-1.12.1
[root@hanfeng nginx-1.12.1]#
  1. 初始化./configure --prefix=/usr/local/nginx
[root@hanfeng nginx-1.12.1]# ./configure --prefix=/usr/local/nginx
  1. 而後編譯make && make install
[root@hanfeng nginx-1.12.1]# make && make install

7查看nginx目錄javascript

  • conf目錄,就是配置文件目錄
  • html目錄,樣例文件
  • logs目錄,存放日誌的
  • sbin目錄,核心進程目錄
[root@hanfeng nginx-1.12.1]# ls /usr/local/nginx/
conf  html  logs  sbin
[root@hanfeng nginx-1.12.1]#
  1. 支持-t 檢查配置文件語法錯誤
[root@hanfeng nginx-1.12.1]# /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
[root@hanfeng nginx-1.12.1]#
  1. 給nginx建立啓動腳本,放在/etc/init.d/nginx,配置文件內容
[root@hanfeng nginx-1.12.1]# vim /etc/init.d/nginx

將如下文件拷貝進去
#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"
start() 
{
    echo -n $"Starting $prog: "
    mkdir -p /dev/shm/nginx_temp
    daemon $NGINX_SBIN -c $NGINX_CONF
    RETVAL=$?
    echo
    return $RETVAL
}
stop() 
{
    echo -n $"Stopping $prog: "
    killproc -p $NGINX_PID $NGINX_SBIN -TERM
    rm -rf /dev/shm/nginx_temp
    RETVAL=$?
    echo
    return $RETVAL
}
reload()
{
    echo -n $"Reloading $prog: "
    killproc -p $NGINX_PID $NGINX_SBIN -HUP
    RETVAL=$?
    echo
    return $RETVAL
}
restart()
{
    stop
    start
}
configtest()
{
    $NGINX_SBIN -c $NGINX_CONF -t
    return 0
}
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  reload)
        reload
        ;;
  restart)
        restart
        ;;
  configtest)
        configtest
        ;;
  *)
        echo $"Usage: $0 {start|stop|reload|restart|configtest}"
        RETVAL=1
esac
exit $RETVAL

而後保存退出
  1. 更改配置文件的權限
[root@hanfeng nginx-1.12.1]# chmod 755 /etc/init.d/nginx
[root@hanfeng nginx-1.12.1]#
  1. 將nginx加入到服務列表裏
[root@hanfeng nginx-1.12.1]# chkconfig --add nginx
[root@hanfeng nginx-1.12.1]#
  1. 配置開啓啓動nginx服務
[root@hanfeng nginx-1.12.1]# chkconfig nginx on 
[root@hanfeng nginx-1.12.1]#
  1. 定義配置文件,默認conf目錄下是有一個nginx.conf文件的,但咱們不使用它,使用本身配置的
[root@hanfeng nginx-1.12.1]# cd /usr/local/nginx/conf/
[root@hanfeng conf]# ls
fastcgi.conf            koi-utf             nginx.conf           uwsgi_params
fastcgi.conf.default    koi-win             nginx.conf.default   uwsgi_params.default
fastcgi_params          mime.types          scgi_params          win-utf
fastcgi_params.default  mime.types.default  scgi_params.default
[root@hanfeng conf]# mv nginx.conf nginx.conf.1
[root@hanfeng conf]#
  1. 建立一個配置文件,配置文件內容
[root@hanfeng conf]# vim nginx.conf

在配置文件中添加如下內容
user nobody nobody;    // user定義啓動nginx的用戶
worker_processes 2;    //定義子進程有幾個
error_log /usr/local/nginx/logs/nginx_error.log crit;   //錯誤日誌
pid /usr/local/nginx/logs/nginx.pid;  // PID所在
worker_rlimit_nofile 51200;   //nginx最多能夠打開文件的上限
events
{
use epoll;                                   //使用epoll模式
worker_connections 6000;      // 進程最大有多少個鏈接
}

http
{
    include mime.types;
    default_type application/octet-stream;
    server_names_hash_bucket_size 3526;
    server_names_hash_max_size 4096;
    log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
    ' $host "$request_uri" $status'
    ' "$http_referer" "$http_user_agent"';
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 30;
    client_header_timeout 3m;
    client_body_timeout 3m;
    send_timeout 3m;
    connection_pool_size 256;
    client_header_buffer_size 1k;
    large_client_header_buffers 8 4k;
    request_pool_size 4k;
    output_buffers 4 32k;
    postpone_output 1460;
    client_max_body_size 10m;
    client_body_buffer_size 256k;
    client_body_temp_path /usr/local/nginx/client_body_temp;
    proxy_temp_path /usr/local/nginx/proxy_temp;
    fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
    fastcgi_intercept_errors on;
    tcp_nodelay on;
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 8k;
    gzip_comp_level 5;
    gzip_http_version 1.1;
    gzip_types text/plain application/x-javascript text/css text/htm 
    application/xml;
    server        //一個server 對應一個虛擬主機,定義這個,才能正常訪問網站  下面一整段,就是一個默認的虛擬主機
    {
        listen 80;
        server_name localhost;            //網站域名
        index index.html index.htm index.php;
        root /usr/local/nginx/html;            //網站的根目錄
        location ~ \.php$                         //配置解析php的部分
        {
            include fastcgi_params;
            fastcgi_pass unix:/tmp/php-fcgi.sock;        //nginx經過這一行配置來調用php-fpm服務
           # fastcgi_pass 127.0.0.1:9000;            //若是php-fpm監聽的是9000端口,直接這樣寫配置便可
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
        }    
    }
}

而後保存退出
  • 做爲一個網站的服務,必須監聽一個端口,默認監聽的是80端口,假如沒有配置 server 這個幾行,那麼nginx將識別不到監聽端口,致使服務不可用
  1. 編譯好配置文件,檢查配置文件是否存在語法錯誤
[root@hanfeng conf]# /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
[root@hanfeng conf]#
  1. 啓動nginx服務
[root@hanfeng conf]# /etc/init.d/nginx  start
Starting nginx (via systemctl):                            [  肯定  ]
[root@hanfeng conf]#
  1. 查看nginx進程
[root@hanfeng conf]# ps aux |grep nginx
root     16411  0.0  0.0  20996   624 ?        Ss   21:53   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody   16412  0.0  0.3  23440  3212 ?        S    21:53   0:00 nginx: worker process
nobody   16413  0.0  0.3  23440  3212 ?        S    21:53   0:00 nginx: worker process
root     16415  0.0  0.0 112676   984 pts/0    R+   21:55   0:00 grep --color=auto nginx
[root@hanfeng conf]#
  1. 測試nginx,這裏能夠輸入curl localhost 或者輸入curl 127.0.0.1 獲得的結果相同
[root@hanfeng conf]# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@hanfeng conf]#
  1. 測試解析php,新建一個1.php文件
[root@hanfeng conf]# vim /usr/local/nginx/html/1.php
寫入
<?php
echo "this is nginx test page.";
[root@hanfeng conf]# curl localhost/1.php
this is nginx test page.[root@hanfeng conf]#
相關文章
相關標籤/搜索