1. 程式人生 > >在CENTOS上原始碼搭建LNMP環境

在CENTOS上原始碼搭建LNMP環境

前言

1.操作前提:

  • CentOS Linux release 7.5.1804;
  • sudo使用者(需要root許可權);

2.需要安裝的元件:

  • nginx穩定版:nginx-1.14.0;
  • MariaDB 10.3.10 Stable;
  • PHP 7.2.11 Stable;

3.操作步驟:

  • 新增環境依賴包;
  • 安裝libiconv,libmcrypt等;
  • 安裝nginx;
  • 安裝php;
  • 安裝mariadb;

下載安裝包

wget http://nginx.org/download/nginx-1.14.0.tar.gz &&  tar xzvf nginx-1.14.0.tar.gz 

wget http://am1.php.net/distributions/php-7.2.11.tar.gz && tar xzvf php-7.2.11.tar.gz

wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz &&  tar xzvf libiconv-1.14.tar.gz

wget http://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz && tar xzvf ./libmcrypt-2.5.8.tar.gz

刪除不需要的原始碼包:

rm -rf *.tar.gz

新增環境依賴包

以下依賴包是整個Lnmp搭建過程中所需的基本依賴包,如果在安裝過程中出現錯誤,提示缺少某依賴包,可以自己安裝:

#執行下面這條命令,各引數之間以空格分開

sudo yum -y install zlib-devel pcre-devel openssl-devel gcc gcc-c++  ncurses-devel perl perl-devel perl-ExtUtils-Embedlibjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel  libxml2 libxml2-devel curl-devel libxslt libxslt-devel gd gd-devel GeoIP GeoIP-devel

編譯nginx

為了使Nginx能夠啟用http/2,需要通過原始碼的方式安裝nginx,並指定--with-openssl的版本在1.0.2版本以上,這裡我將需要版本的依賴安裝在/usr/local/src目錄:

$ cd /usr/local/src/

# PCRE version 8.40
$ sudo wget https://ftp.pcre.org/pub/pcre/pcre-8.40.tar.gz && sudo tar xzvf pcre-8.40.tar.gz

# zlib version 1.2.11
$ sudo wget https://www.zlib.net/zlib-1.2.11.tar.gz && sudo tar xzvf zlib-1.2.11.tar.gz

# OpenSSL version openssl-1.1.0h
$ sudo wget https://www.openssl.org/source/openssl-1.1.0h.tar.gz && sudo tar xzvf openssl-1.1.0h.tar.gz

刪除不需要的包

sudo rm -rf *.tar.gz

編譯nginx

cd ~/src/nginx-1.14.0
sudo ./configure --prefix=/etc/nginx \
            --sbin-path=/usr/sbin/nginx \
            --modules-path=/usr/lib64/nginx/modules \
            --conf-path=/etc/nginx/nginx.conf \
            --error-log-path=/var/log/nginx/error.log \
            --pid-path=/var/run/nginx.pid \
            --lock-path=/var/run/nginx.lock \
            --user=nginx \
            --group=nginx \
            --build=CentOS \
            --builddir=nginx-1.14.0 \
            --with-select_module \
            --with-poll_module \
            --with-threads \
            --with-file-aio \
            --with-http_ssl_module \
            --with-http_v2_module \
            --with-http_realip_module \
            --with-http_addition_module \
            --with-http_xslt_module=dynamic \
            --with-http_image_filter_module=dynamic \
            --with-http_geoip_module=dynamic \
            --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_auth_request_module \
            --with-http_random_index_module \
            --with-http_secure_link_module \
            --with-http_degradation_module \
            --with-http_slice_module \
            --with-http_stub_status_module \
            --http-log-path=/var/log/nginx/access.log \
            --http-client-body-temp-path=/var/cache/nginx/client_temp \
            --http-proxy-temp-path=/var/cache/nginx/proxy_temp \
            --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
            --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
            --http-scgi-temp-path=/var/cache/nginx/scgi_temp \
            --with-mail=dynamic \
            --with-mail_ssl_module \
            --with-stream=dynamic \
            --with-stream_ssl_module \
            --with-stream_realip_module \
            --with-stream_geoip_module=dynamic \
            --with-stream_ssl_preread_module \
            --with-compat \
            --with-pcre=/usr/local/src/pcre-8.40 \
            --with-pcre-jit \
            --with-zlib=/usr/local/src/zlib-1.2.11 \
            --with-openssl=/usr/local/src/openssl-1.1.0h \
            --with-openssl-opt=no-nextprotoneg \
            --with-debug

上面--prefix=/etc/nginx為安裝目錄。

sudo make && sudo make install

建立軟鏈

sudo ln -s /usr/lib64/nginx/modules /etc/nginx/modules

這樣就可以在nginx的配置檔案中像這樣載入動態模組:load_module modules/ngx_foo_module.so;。

檢視編譯資訊

sudo nginx -V

修改/etc/nginx/nginx.conf

去掉 user nobody;一行的註釋。

檢查配置是否存在潛在錯誤

## 執行檢查可能會報錯
sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: [emerg] mkdir() "/var/cache/nginx/client_temp" failed (2: No such file or directory)
nginx: configuration file /etc/nginx/nginx.conf test failed

## 如果報上面的錯誤,則執行下面的程式碼
sudo mkdir -p /var/cache/nginx && sudo nginx -t

建立nginx系統服務單元

編輯檔案:

sudo vi /usr/lib/systemd/system/nginx.service

將下面的內容複製進去

[Unit]
Description=nginx - high performance web server
Documentation=https://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target

啟動nginx並設定開機啟動

sudo systemctl start nginx.service && sudo systemctl enable nginx.service

至此,nginx的安裝到此結束,nginx的原始碼安裝參考出處:centos7通過編譯原始碼的方式安裝nginx

接下來進行原始碼編譯安裝Php的操作。

編譯安裝php

php需要libiconv、libmcrypt等依賴,下面進行編譯:【閱讀原文