1. 程式人生 > >Linux下搭建nginx+php7

Linux下搭建nginx+php7

//系統
# cat /etc/redhat-release 
CentOS release 6.9 (Final)
# uname -r
2.6.32-696.el6.x86_64
//關閉SELinux,機器需要重啟
# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
//驗證
# grep SELINUX=disabled /etc/selinux/config
SELINUX=disabled
//關閉SELinux,不需要重啟
# setenforce 0
//iptables關閉
# /etc/init.d/iptables stop
//應用版本 //nginx-1.14.0 //php-7.1.7 mysql> status; -------------- mysql Ver 14.14 Distrib 5.5.25a, for Linux (x86_64) using readline 5.1
//安裝依賴
# yum -y install wget gcc gcc-c++ cmake pcre* curl*
# yum -y install libtool* zlib* openssl* libxml2* libcurl* libjpeg* libpng libpng-devel freetype freetype-devel libxslt*
//安裝Nginx
# tar zxvf nginx-1.14.0.tar.gz
# cd nginx-1.14.0
# ./configure --prefix=/usr/local/nginx-1.14
# make && make install
# ln -s /usr/local/nginx-1.14 /usr/local/nginx
# cat /usr/local/nginx/conf/nginx.conf //在最後一行新增(最後一個 } 之前)
include vhost/*.conf;
# mkdir /usr/local/nginx/conf/vhost && cd /usr/local/nginx/conf/vhost
# cat /usr/local/nginx/conf/vhost/nginx.conf //建立nginx.conf檔案,內容如下 server { listen 80; server_name localhost; root html; index index.html index.php; error_log logs/tianhuabms_error.log; location / { location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO /usr/local/nginx/conf/$fastcgi_path_info; fastcgi_pass 127.0.0.1:9000; include fastcgi_params; } } location = /robots.txt { allow all; log_not_found off; access_log off; } } # /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 # /usr/local/nginx/sbin/nginx //啟動nginx # ps aux|grep nginx root 5148 0.0 0.0 19988 692 ? Ss 19:41 0:00 nginx: master process /usr/local/nginx/ sbin/nginxnobody 5149 0.0 0.1 20460 1292 ? S 19:41 0:00 nginx: worker process root 5151 0.0 0.0 103336 900 pts/1 S+ 19:41 0:00 grep nginx # /usr/local/nginx/sbin/nginx -s stop //停止nginx # /usr/locat/nginx/sbin/nginx -s reload //重新載入配置檔案
//編譯php
# useradd www -s /sbin/nologin -M
# tar zxvf php-7.1.7.tar.gz
# cd php-7.1.7
//在編譯前增加 /usr/local/lib 以避免一些錯誤
# echo "/usr/local/lib" >> /etc/ld.so.conf && ldconfig -v
//如果./configure時遇到錯誤,解決錯誤後clean
# make clean //解決錯誤後clean,重新./configure

# ./configure --prefix=/usr/local/php7 \
--with-config-file-path=/usr/local/php/etc \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--with-mysqli \
--with-pdo-mysql \
--with-iconv-dir \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--with-mcrypt \
--enable-ftp \
--with-gd \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--without-pear \
--with-gettext \
--disable-fileinfo \
--enable-maintainer-zts
# make
# make test
# make install
//編譯php ./configure時遇到錯誤
configure: error: mcrypt.h not found. Please reinstall libmcrypt.
//解決
# wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/attic/libmcrypt/libmcrypt-2.5.7.tar.gz 
# tar zxvf libmcrypt-2.5.7.tar.gz && cd libmcrypt-2.5.7
# ./configure && make && make install

//編譯php ./configure時遇到錯誤
configure: error: Don't know how to define struct flock on this system, set --enable-opcache=no
//解決
--enable-opcache=no

//編譯php ./configure時遇到錯誤
configure: error: off_t undefined; check your library configuration
//解決
echo "/usr/local/lib" >> /etc/ld.so.conf && ldconfig -v

//啟動php-fpm時出現錯誤
Starting php-fpm [02-Aug-2018 14:53:24] ERROR: [pool www] cannot get uid for user 'www'
[02-Aug-2018 14:53:24] ERROR: FPM initialization failed
 failed
//解決,造成的原因是編譯php時指定的使用者和屬組
# useradd www -s /sbin/nologin -M   
//配置php
# cp php.ini-production /usr/local/php7/etc/php.ini
# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
# cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf
# cp ./sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
# chmod +x /etc/init.d/php-fpm
# /etc/init.d/php-fpm start
//測試
# cat /usr/local/nginx/html/phpinfo.php
<?php
phpinfo();
?>
//最後在瀏覽器中輸入 localhost/phpinfo.php 出現一個關於php相關資訊的頁面即成功
//最後附上 優化或修改CentOS最大連線數限制
[root@local ~]# vim /etc/security/limits.conf    //在檔案最後新增以下內容
*       soft    nofile  65535
*       hard    nofile  65535
*       soft    nproc  65535
*       hard    nproc  65535
[root@local ~]# vim /etc/pam.d/login    //在檔案最後新增以下內容
session    required     pam_limits.so
[root@local ~]# ulimit -n   //重新登入後驗證
65535