CentOS 6.8下编译安装NGINX、PHP 7、MariaDB及配置

在Linux系统下的web应用的安装方式多种多样,核心主要是nginx/Apache、php、mysql/MariaDB三大主要应用,针对CentOS 6.8、CentOS 7.2环境下的安装方式也很多,本文主要从源代码编译安装进行说明和简单的配置。

1、安装nginx

nginx官网网站是:http://nginx.org/ ,当前的稳定版本为:nginx 1.10.2,下载地址:http://nginx.org/download/nginx-1.10.2.tar.gz ,nginx的美化目录插件Fancy Index可以下载并一起编译安装。

首先,安装编译安装的必要依赖包

yum -y install gcc gcc-c++ pcre* openssl* gd-devel* zlib zlib-devel pcre-devel libxslt* git autoconf automake libtool make cmake

新增nginx用户及用户组

/usr/sbin/groupadd -f nginx
/usr/sbin/useradd nginx -M -s /sbin/nologin -g nginx 

下载Fancy Index

git clone https://github.com/aperezdc/ngx-fancyindex.git ngx-fancyindex

如果git命令不支持,需要先安装该命令:

yum -y install git

接下来下载nginx,解压缩,编译安装:

#下载nginx最新源文件
wget http://nginx.org/download/nginx-1.10.2.tar.gz
#解压缩
tar -zxvf nginx-1.10.2.tar.gz
#进入源文件目录
cd nginx-1.10.2
#编译
./configure --user=nginx \
--group=nginx \
--prefix=/usr/local/nginx \
--with-pcre \
--with-http_ssl_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_sub_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-http_image_filter_module \
--with-threads \
--with-stream \
--with-stream_ssl_module \
--with-http_slice_module \
--with-http_v2_module \
--with-ipv6 \
--with-mail \
--with-mail_ssl_module \
--with-file-aio \
--with-http_xslt_module \
--add-module=../ngx-fancyindex
#安装
make && make install

nginx服务的启动、重启、通知操作:

#启动服务
/usr/local/nginx/sbin/nginx
#停止服务
/usr/local/nginx/sbin/nginx -s stop
#重启服务
/usr/local/nginx/sbin/nginx -s reload
#检测是否启动成功
netstat -tunlp | grep nginx
#查询nginx主进程号 
ps -ef | grep nginx
#停止进程 
kill -QUIT 主进程号 
#快速停止 
kill -TERM 主进程号 
#强制停止 
pkill -9 nginx

添加nginx服务到开启自动启动

echo '/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf' >> /etc/rc.local

您现在可以打开浏览器访问您的站点了(您可以访问服务器的IP地址或者在服务器上使用http://localhost),页面显示如下:
%e5%be%ae%e4%bf%a1%e6%88%aa%e5%9b%be_20161114122309

修改配置文件nginx.conf支持PHP,安装完成PHP后,需要重启下nginx。

vi /usr/local/nginx/conf/nginx.conf

修改后的nginx.conf配置文件如下:

#user nobody;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
 worker_connections 1024;
}


http {
 include mime.types;
 default_type application/octet-stream;

 #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
 # '$status $body_bytes_sent "$http_referer" '
 # '"$http_user_agent" "$http_x_forwarded_for"';

 #access_log logs/access.log main;

 sendfile on;
 #tcp_nopush on;

 #keepalive_timeout 0;
 keepalive_timeout 65;

 #gzip on;

 server {
 listen 80;
 server_name localhost;

 #charset koi8-r;

 #access_log logs/host.access.log main;

 location / {
 root /usr/local/nginx/html;
 index index.php index.shtml index.html index.htm;
 ssi on;
 fancyindex on;
 fancyindex_exact_size off;
 }
 
 location ~ \.php$ {
 root /usr/local/nginx/html;
 fastcgi_pass 127.0.0.1:9000;
 fastcgi_index index.php;
 fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
 include fastcgi_params;
 }
 
 #error_page 404 /404.html;

 # redirect server error pages to the static page /50x.html
 #
 error_page 500 502 503 504 /50x.html;
 location = /50x.html {
 root html;
 }

 # proxy the PHP scripts to Apache listening on 127.0.0.1:80
 #
 #location ~ \.php$ {
 # proxy_pass http://127.0.0.1;
 #}

 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 #
 #location ~ \.php$ {
 # root html;
 # fastcgi_pass 127.0.0.1:9000;
 # fastcgi_index index.php;
 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
 # include fastcgi_params;
 #}

 # deny access to .htaccess files, if Apache's document root
 # concurs with nginx's one
 #
 #location ~ /\.ht {
 # deny all;
 #}
 }


 # another virtual host using mix of IP-, name-, and port-based configuration
 #
 #server {
 # listen 8000;
 # listen somename:8080;
 # server_name somename alias another.alias;

 # location / {
 # root html;
 # index index.html index.htm;
 # }
 #}


 # HTTPS server
 #
 #server {
 # listen 443 ssl;
 # server_name localhost;

 # ssl_certificate cert.pem;
 # ssl_certificate_key cert.key;

 # ssl_session_cache shared:SSL:1m;
 # ssl_session_timeout 5m;

 # ssl_ciphers HIGH:!aNULL:!MD5;
 # ssl_prefer_server_ciphers on;

 # location / {
 # root html;
 # index index.html index.htm;
 # }
 #}

}

2、安装PHP 7

PHP官网网站是:http://php.net/ ,当前PHP 7的稳定版本为:PHP 7.0.13(PHP官网当前的稳定发行版有PHP 5.6和PHP 7.0,本文选用最新的PHP 7.0,具体优缺点可以百度一下),下载地址:http://php.net/downloads.php ,具体安装步骤如下:

首先,安装编译安装的必要依赖包

yum -y install libxml2 libxml2-devel openssl pcre-devel openssl-devel curl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libmcrypt-devel libxslt-devel

 

接下来下载PHP,解压缩,编译安装:

#下载php最新源文件
wget http://php.net/distributions/php-7.0.13.tar.gz
#解压缩
tar -zxvf php-7.0.13.tar.gz
#进入源文件目录
cd php-7.0.13
#编译
./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-config-file-scan-dir=/usr/local/php/etc/php.d \
--with-mcrypt=/usr/include \
--with-mysqli \
--with-pdo-mysql \
--with-pdo-sqlite \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--with-gd \
--with-kerberos \
--with-pcre-regex \
--with-iconv \
--with-zlib \
--with-mhash \
--without-pear \
--with-gettext \
--with-curl \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--with-xmlrpc \
--with-libxml-dir \
--with-xsl \
--enable-xml \
--enable-libxml \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--enable-mbregex \
--enable-mbstring \
--enable-ftp \
--enable-gd-native-ttf \
--enable-fpm \
--enable-pcntl \
--enable-sockets \
--enable-session \
--enable-zip \
--enable-soap \
--enable-session \
--enable-bcmath \
--enable-maintainer-zts \
--enable-opcache \
--disable-fileinfo
#安装
make && make install

开始配置,首先复制PHP.INI,并配置PHP.INI的一些参数

#复制php.ini
cp php.ini-production /usr/local/php/etc/php.ini
#编辑php.ini
vi /usr/local/php/etc/php.ini
#搜索一下参数,根据你的需要修改成您想要的内容,下面为我的配置 
; Maximum size of POST data that PHP will accept. 
; Its value may be 0 to disable the limit. It is ignored if POST data reading 
; is disabled through enable_post_data_reading. 
; http://php.net/post-max-size 
post_max_size = 128M 

; Maximum allowed size for uploaded files. 
; http://php.net/upload-max-filesize 
upload_max_filesize = 128M 

[Date] 
; Defines the default timezone used by the date functions 
; http://php.net/date.timezone 
;date.timezone = 
date.timezone=Asia/Shanghai

启用php-fpm服务,并复制相关配置文件

#复制配置文件
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf

#启动服务
/usr/local/php/sbin/php-fpm -c /usr/local/php/etc/php.ini -y /usr/local/php/etc/php-fpm.conf

#添加服务到自动启动
echo '/usr/local/php/sbin/php-fpm -c /usr/local/php/etc/php.ini -y /usr/local/php/etc/php-fpm.conf' >> /etc/rc.local

3、MariaDB数据库安装
MariaDB官网网站是:http://mariadb.org/ ,当前最新版下载地址:https://mariadb.org/download/ ,数据库安装采用yum安装,操作步骤如下:
添加 yum 数据源(建议命名为 MariaDB.repo 类似的名字):

#编辑源
vi /etc/yum.repos.d/MariaDB.repo

#插入内容
# MariaDB 10.1 CentOS repository list - created 2016-11-08 05:55 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos6-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

插入的内容可以从官网的Setting up MariaDB Repositories功能来生产数据源文件,地址是https://downloads.mariadb.org/mariadb/repositories/

#安装数据库
yum -y install MariaDB-server MariaDB-client
#如果要删除旧的数据库可以使用remove, 参数 -y 是确认,不用提示。
yum remove MariaDB-server MariaDB-client

数据库启动等操作命令

# 查看mysql状态;关闭数据库 
service mysql status
service mysql stop
# 启动数据库 
service mysql start

#修改root密码 
mysqladmin -u root password 'XXXXXX'
#进入mysql命令行状态下,可以查看运行状态
> 
#查看MySQL的状态 
status; 
#显示支持的引擎 
show engines; 

部分安装后,PHP无法连接到数据提示连接失败,需要修改PHP.INI的参数,首先通过mysql status查看下UNIX socket与php.ini里面的mysql.default_socket、mysqli.default_socket、pdo_mysql.default_socket值是否一致,如果不一致则修改之。

[root@web ~]# mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 29
Server version: 10.1.19-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> status;
--------------
mysql Ver 15.1 Distrib 10.1.19-MariaDB, for Linux (x86_64) using readline 5.1

Connection id: 29
Current database: 
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server: MariaDB
Server version: 10.1.19-MariaDB MariaDB Server
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: latin1
Db characterset: latin1
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /var/lib/mysql/mysql.sock
Uptime: 2 hours 59 min 14 sec

Threads: 1 Questions: 184 Slow queries: 0 Opens: 18 Flush tables: 1 Open tables: 12 Queries per second avg: 0.017
--------------

MariaDB [(none)]> 

数据库的UNIX socket为/var/lib/mysql/mysql.sock,修改PHP.INI里面的参数mysqli.default_socket、pdo_mysql.default_socket为/var/lib/mysql/mysql.sock,重启即可。