CentOS7.3 安装配置 Nginx、MariaDB、PHP

CentOS7.3 安装配置 Nginx、MariaDB、PHP

配置 Nginx 安装参数并安装

配置

以下参数也可以在后期通过配置 conf 文件进行配置
基本配置如下

./configure --prefix=/usr/local/Nginx --sbin-path=/usr/sbin/Nginx --conf-path=/etc/Nginx/Nginx.conf --error-log-path=/var/log/Nginx/error.log --user=Nginx --group=Nginx

更多模块一起安装配置如下,有些模块需要系统安装相应组件,根据提示进行安装,比如 gd 组件,需要通过 yum install gd-devel 安装对应系统组件。

./configure --prefix=/usr/local/Nginx --sbin-path=/usr/sbin/Nginx --conf-path=/etc/Nginx/Nginx.conf --error-log-path=/var/log/Nginx/error.log --user=Nginx --group=Nginx --with-select_module --with-poll_module --with-file-aio --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_xslt_module=dynamic --with-http_image_filter_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_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 --with-http_perl_module --http-log-path=/var/log/Nginx/access.log --with-mail --with-mail_ssl_module --with-cpu-opt=cpu --with-pcre --with-pcre-jit --with-zlib-asm=cpu --with-debug

出现如下内容说明配置成功,记录了你的 Nginx 相关配置信息。

Nginx path prefix: "/usr/local/Nginx"
  Nginx binary file: "/usr/sbin/Nginx"
  Nginx modules path: "/usr/local/Nginx/modules"
  Nginx configuration prefix: "/etc/Nginx"
  Nginx configuration file: "/etc/Nginx/Nginx.conf"
  Nginx pid file: "/usr/local/Nginx/logs/Nginx.pid"
  Nginx error log file: "
  "
  Nginx http access log file: "/var/log/Nginx/access.log"
  Nginx http client request body temporary files: "client_body_temp"
  Nginx http proxy temporary files: "proxy_temp"
  Nginx http fastcgi temporary files: "fastcgi_temp"
  Nginx http uwsgi temporary files: "uwsgi_temp"
  Nginx http scgi temporary files: "scgi_temp"

编译

make && make install

提示如下成功:

cp objs/ngx_http_xslt_filter_module.so '/usr/local/Nginx/modules/ngx_http_xslt_filter_module.so'
make[1]: 离开目录“/root/Nginx-1.11.10”

检查是否安装成功

执行如下命令启动,并用 ps 命令检查启动情况

/usr/sbin/Nginx -c /etc/Nginx/Nginx.conf
ps -ef | grep Nginx

添加 Nginx 到系统服务

vim /usr/lib/systemd/system/Nginx.service

增加如下内容结合安装时的配置文件进行修改),其中 特殊的环境变量 $MAINPID 可用于表示主进程的PID。有关 systemd.service 的配置,可以参加下面两篇文章
systemd 入门教程:命令篇
Systemd 入门教程:实战篇
systemd.service 中文手册

[Unit]
Description=The Nginx HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/Nginx/logs/Nginx.pid
ExecStartPre=/usr/sbin/Nginx -c /etc/Nginx/Nginx.conf -t
ExecStart=/usr/sbin/Nginx -c /etc/Nginx/Nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

:wq 保存退出

停止原先启动的 Nginx 进程,

/usr/sbin/Nginx -s stop
  ps -ef|grep Nginx
  systemctl daemon-reload
  systemctl start Nginx.service

服务启动后,打开浏览器,输入您网站的地址,查看欢迎页面是否正常显示

不知道 ip 的,可以通过如下命令找到本机 ip 地址

ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'

命令行测试网页

curl http://yourip

安装 MysqL (MariaDB)

sudo yum install mariadb-server mariadb
sudo systemctl start mariadb

增加 MysqL 安全性

sudo MysqL_secure_installation

设置为系统启动加载

sudo systemctl enable mariadb

安装 PHP7

建立 PHP7 的 yum 源

wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
wget http://rpms.remirepo.net/enterprise/remi-release-7.rpm
rpm -Uvh remi-release-7.rpm 
rpm -Uvh epel-release-latest-7.noarch.rpm

後面會用到 yum-config-manager,若無此指令,可先用 yum search 查詢在那個套件再安裝,我们通过 search 找到为 yum-utils

yum search yum-config-manager
yum install yum-utils

开启remi 、remi-PHP71 源

yum-config-manager --enable remi
yum-config-manager --enable remi-PHP71

如果安装的是 PHP5.6 则修改上句如下:

yum-config-manager --enable remi-PHP56

yum repolist all 查看所有 repo,检查是否配置成功。

安装 PHP PHP-fpm PHP-MysqL 及其他 PHP 模块

如果原先已安装 PHP,可以用 yum update PHP* 更新,或者用 yum remove PHP 删除后再重新安装。

yum install PHP PHP-fpm PHP-MysqL PHP-bcmath PHP-gd PHP-mbstring -y

以下组件也可以挑需要的安装。

yum install PHP-fpm PHP-MysqL PHP-bcmath PHP-cli PHP-common PHP-gd PHP-mbstring PHP-pdo PHP-pear PHP-pecl-msgpack PHP-process PHP-xml gd-last PHP-json  PHP-pecl-memcached  PHP-pecl-zip -y

可以用 yum list installed | grep PHP 查看 PHP 套件安装情况。
PHP -v 查看当前版本。

配置 PHP-fpm

找到你的 PHP-fpm.sock 文件,将该路径配置到 PHP-fpm 的 conf 文件

find / -name www.conf
vi /etc/PHP-fpm.d/www.conf

修改 www.conf 如下

listen = /run/PHP-fpm/PHP-fpm.sock

PHP-fpm 的 user/group 要和 webserver 的所有权一致,如我在 CentOS7 下,用 Nginx 用户 运行 Nginx 服务,则修改 PHP-fpm 的 www.conf 配置如下:

listen.owner = Nginx
listen.group = Nginx
listen.mode = 0660

user = Nginx
group = Nginx

重启 PHP-fpm 服务,并加入到系统自启动中:

sudo systemctl start PHP-fpm
sudo systemctl enable PHP-fpm

如果重启后,PHP 服务还是不成功,可能还需执行 chown Nginx:Nginx /run/PHP-fpm/PHP-fpm.sock 后再重启。

配置 Nginx 使其能执行 PHP 页面

vi /etc/Nginx/Nginx.conf

主要有如下步骤:

  • 设置 root 根目录

  • 添加 index.PHP认目录首页请求

  • 修改 server_name ,添加域名或 IP

  • 定义 404 等错误页面的规则

  • 配置 PHP 处理模块,主要调整 location ~ \.PHP$ { 部分,尤其注意 fastcgi_pass unix: 指向的 PHP-fpm.sock 路径是否正确;

  • 保存后重启 Nginx 服务

我的配置文件大致如下:

server {
    listen       80;
    server_name  server_domain_name_or_IP;

    # note that these lines are originally from the "location /" block
    root   /www;
    index index.PHP index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /www;
    }

    location ~ \.PHP$ {
        try_files $uri =404;
        fastcgi_pass unix:/run/PHP-fpm/PHP-fpm.sock;
        fastcgi_index index.PHP;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

在根目录添加一个 PHPinfo 的 PHP 文件,打开浏览器进行测试是否正常显示
成功后删除测试页面

安装 PHPmyadmin

去官网下载解压即可:

wget https://files.PHPmyadmin.net/PHPMyAdmin/4.6.6/PHPMyAdmin-4.6.6-all-languages.zip
unzip PHPMyAdmin-4.6.6-all-languages.zip -d /www

cp config.sample.inc.PHP config.inc.PHP

如果浏览器打开出现如下错误

session_start(): open(SESSION_FILE,O_RDWR) Failed: Permission de

修改报错信息文件的所有权,如下

chown Nginx:Nginx /var/lib/PHP/session

相关文章

Centos下搭建性能监控Spotlight
CentOS 6.3下Strongswan搭建IPSec VPN
在CentOS6.5上安装Skype与QQ
阿里云基于centos6.5主机VPN配置
CentOS 6.3下配置multipah
CentOS安装、配置APR和tomcat-native