溫馨提示×

LNMP架構下Nginx如何配置

小樊
48
2025-02-23 08:17:47
欄目: 編程語言

LNMP架構是指Linux系統下Nginx+MySQL+PHP這種網站服務器架構,以下是在LNMP架構下配置Nginx的步驟:

安裝Nginx

  1. 安裝編譯工具及庫文件
yum -y install make automake autoconf libtool zlib zlib-devel gcc gcc-c++ openssl openssl-devel
  1. 安裝PCRE(目的:讓Nginx支持rewrite功能):
cd /usr/local/src/
wget http://ftp.pcre.org/pub/pcre/pcre-8.35.tar.gz
tar zxvf pcre-8.35.tar.gz
cd pcre-8.35
./configure
make && make install
pcre-config --version
  1. 安裝Nginx
cd /usr/local/src/
wget http://nginx.org/download/nginx-1.14.0.tar.gz
tar zxvf nginx-1.14.0.tar.gz
cd nginx-1.14.0
./configure \
    --prefix=/usr/local/nginx \
    --sbin-path=/usr/sbin/nginx \
    --conf-path=/etc/nginx/nginx.conf \
    --error-log-path=/var/log/nginx/error.log \
    --http-log-path=/var/log/nginx/access.log \
    --pid-path=/var/run/nginx.pid \
    --lock-path=/var/run/nginx.lock \
    --http-client-body-temp-path=/var/tmp/nginx/client \
    --http-proxy-temp-path=/var/tmp/nginx/proxy \
    --http-fastcgi-temp-path=/var/tmp/nginx/fcgi \
    --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
    --http-scgi-temp-path=/var/tmp/nginx/scgi \
    --with-pcre \
    --with-http_v2_module \
    --with-http_ssl_module \
    --with-http_realip_module \
    --with-http_addition_module \
    --with-http_sub_module \
    --with-http_dav_module \
    --with-http_flv_module \
    --with-http_mp4_module \
    --with-http_gunzip_module \
    --with-http_gzip_static_module \
    --with-http_random_index_module \
    --with-http_secure_link_module \
    --with-http_stub_status_module \
    --with-http_auth_request_module \
    --with-mail \
    --with-mail_ssl_module \
    --with-file-aio \
    --with-http_v2_module \
    --with-threads \
    --with-stream \
    --with-stream_ssl_module \
    --with-ipv6
make && make install

配置Nginx

Nginx的主要配置文件是/etc/nginx/nginx.conf,以下是一個基本的配置示例:

user nginx;
worker_processes auto;
pid /run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    sendfile on;
    keepalive_timeout 65;

    server {
        listen 80;
        server_name example.com;

        location / {
            root /usr/share/nginx/html;
            index index.html index.htm;
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root /usr/share/nginx/html;
        }

        location ~ \.php$ {
            fastcgi_pass  unix:/var/run/php-fpm/php-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }

        location ~ /\.ht {
            deny all;
        }
    }
}

啟動Nginx

配置完成后,啟動Nginx服務:

/usr/local/nginx/sbin/nginx

設置Nginx開機自啟動

 chkconfig --add nginx
 chkconfig --level 2345 nginx on

以上步驟展示了如何在LNMP架構下配置Nginx。根據實際需求,您可能需要進一步調整配置文件以滿足特定的性能和資源管理要求。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女