ubuntu系統下編譯安裝nginx和PHP

編譯Nginx

  1. 下載源碼包
wget https://nginx.org/download/nginx-1.16.1.tar.gz
mkdir -p /data/exec
  1. 安裝依賴類庫(centos包名不一樣,看configure報錯,缺什麼裝什麼即可)
apt install -y libpcre3 libpcre3-dev openssl libssl-dev build-essential zlib1g-dev
  1. 創建用戶www
useradd www -s '/sbin/nologin'
  1. 開始編譯nginx
./configure --user=www --group=www \
--prefix=/data/exec/nginx \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_gzip_static_module \
--with-http_sub_module \
--with-stream \
--with-stream_ssl_module \
--with-openssl-opt='enable-weak-ssl-ciphers'


make && make install

編譯PHP

  1. 下載源碼包
wget https://www.php.net/distributions/php-7.2.28.tar.bz2
mkdir -p /data/exec
  1. 創建用戶www
useradd www -s '/sbin/nologin'
  1. 安裝編譯php7依賴
apt install -y libxml2 libxml2-dev libcurl4-openssl-dev libfreetype6-dev libjpeg-dev libicu-dev libxslt1-dev openssl
  1. 開始編譯PHP7.2
./configure --prefix=/data/exec/php \
--with-config-file-path=/data/exec/php/etc \
--with-config-file-scan-dir=/data/exec/php/conf.d \
--enable-fpm --with-fpm-user=www --with-fpm-group=www \
--enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd \
--with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir \
--with-zlib --with-libxml-dir --enable-xml --disable-rpath \
--enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization \
--with-curl --enable-mbregex --enable-mbstring --enable-intl --enable-ftp --with-gd \
--with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip \
--enable-soap --with-gettext --enable-opcache --with-xsl

如果提示 configure: error: Cannot find OpenSSL’s libraries
find / -name libssl.so 輸出: /usr/lib/x86_64-linux-gnu/libssl.so
重新連接一下
ln -s /usr/lib/x86_64-linux-gnu/libssl.so /usr/lib

make && make install

編譯工作到此結束,還需要修改nginx和php的配置文件才能正常工作
nginx配置示例,按需修改

server
    {
        listen 80;
        #listen [::]:80;
        server_name _;
        index index.html index.htm index.php default.html default.htm default.php;		# 這裏需要有index.php
        root  /data/project/wwwroot;		# 這裏是你的網站root目錄

        include rewrite/laravel.conf;
        #error_page   404   /404.html;

        # Deny access to PHP files in specific directory
        #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

        location ~ [^/]\.php(/|$)
        {   
            fastcgi_pass  unix:/data/exec/php/php-cgi.sock;				# 這裏指定php的socket文件, 可以在php-fpm的配置裏面找一下,確保nginx和php配置一致.
            fastcgi_index index.php;
            include fastcgi.conf;
            fastcgi_split_path_info ^(.+?\.php)(/.*)$;
			set $path_info $fastcgi_path_info;
			fastcgi_param PATH_INFO       $path_info;
			try_files $fastcgi_script_name =404;
        }

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }

        location ~ /.well-known {
            allow all;
        }

        location ~ /\.
        {
            deny all;
        }

        access_log  /data/logs/nginx/default-access.log;
        error_log   /data/logs/nginx/default-error.log;
    }


測試PHP工作

echo “<?php phpinfo(); ?>” >> /data/exec/nginx/html/index.php

打開opcache擴展 需要在php.ini 添加一行
zend_extension=opcache.so

常用命令

查看編譯參數
php -i |grep configure
nginx -V

查看php當前用的是什麼配置文件
php --ini

查看php安裝的擴展
php -m

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章