centos7.3安装nginx+php+mariadb

1.创建临时目录

cd /  

mkdir package

chmod -R 0777 package

cd package


2.获取nginx安装包

wget http://nginx.org/download/nginx-1.13.9.tar.gz


3.解压安装包

tar -xf nginx-1.13.9.tar.gz

切换目录

cd nginx-1.13.9

4.配置 nginx

./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"

可能出现错误

缺少编译器,错误信息如下:

./configure: error: C compiler cc is not found
  • 1

解决方法:安装gcc-c++

yum -y install gcc-c++ autoconf automake


在配置信息./configure --prefix=/usr/local/nginx 的时,出现错误:

/configure: error: the HTTP rewrite module requires the PCRE library.
  • 1

解决方法:安装pcre

yum -y install pcre pcre-devel
  • 1

-y 是跳过所有需要手动确认的环节

缺少ssl错误,错误信息如下:

./configure: error: the HTTP cache module requires md5 functions
from OpenSSL library.   You can either disable the module by using
--without-http-cache option, or install the OpenSSL library into the system,
or build the OpenSSL library statically from the source with nginx by using
--with-http_ssl_module --with-openssl=<path> options.
  • 1
  • 2
  • 3
  • 4
  • 5

解决方法:安装openssl

yum -y install openssl openssl-devel
  • 1


  • 1

autoconf是自动配置,automake是自动编译

缺少zlib包,错误信息如下:

./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by usingwithout-http_gzip_module
option, or install the zlib library into the system, or build the zlib
library
statically from the source with nginx by usingwith-zlib=<path> option.
  • 1
  • 2
  • 3
  • 4
  • 5

解决方法:安装zlib

yum install -y zlib-devel
  • 1

确实libxml2,错误信息如下:

./configure: error: the HTTP XSLT module requires the libxml2/libxslt
libraries. You can either do not enable the module or install the libraries.
  • 1
  • 2

解决方法:

yum -y install libxml2 libxml2-dev
yum -y install libxslt-devel
  • 1
  • 2

http_image_filter_module是nginx提供的集成图片处理模块,需要gd-devel的支持,错误信息如下:

./configure: error: the HTTP image filter module requires the GD library.
You can either do not enable the module or install the libraries.
  • 1
  • 2

解决方法:

yum -y install gd-devel
  • 1

缺少ExtUtils,错误信息如下:

./configure: error: perl module ExtUtils::Embed is required
  • 1

解决方法:

yum -y install perl-devel perl-ExtUtils-Embed
  • 1

缺少GeoIP,错误信息如下:

./configure: error: the GeoIP module requires the GeoIP library.
You can either do not enable the module or install the library.
  • 1
  • 2

解决方法:

yum -y install GeoIP GeoIP-devel GeoIP-data
  • 1

5.编译安装

make
make install
  • 1
  • 2

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

添加用户

useradd -s /sbin/nologin -M nginx
id nginx


检查是否安装成功

执行如下命令启动,并用 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

报错 nginx: [emerg] getpwnam("nginx") failed

解决办法:

/usr/sbin/groupadd -f nginx

/usr/sbin/useradd -g nginx nginx


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

不知道 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

//编译安装

wget http://cn2.php.net/get/php-7.2.5.tar.gz/from/this/mirror

 tar zxvf php-7.2.5.tar.gz

 cd php-7.2.5

yum install curl-devel

./configure --prefix=/usr/local/php7.2.5 --with-curl --with-freetype-dir --with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-libxml-dir --with-mysqli --with-openssl --with-pcre-regex --with-pdo-mysql --with-pdo-sqlite --with-pear --with-png-dir --with-jpeg-dir --with-xmlrpc --with-xsl --with-zlib --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-mbregex --enable-mbstring --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-xml --enable-zip --enable-debug

make && make install

unrecognized options: –with-mcrypt, –enable-gd-native-ttf
表示php7.2不支持这两个选项,
把上面两个编译选项删除就可以了。

建立 php7 的 yum 源

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

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

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

开启remi 、remi-php72 源

yum-config-manager --enable remi
yum-config-manager --enable remi-php72

如果安装的是 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 = /dev/shm/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:/dev/shm/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

安装fastCGI

cd /package

wget http://download.lighttpd.net/spawn-fcgi/releases-1.6.x/spawn-fcgi-1.6.4.tar.gz  下载fcgi

tar -xf spawn-fcgi-1.6.4.tar.gz   解压

cd spawn-fcgi-1.6.4   切换目录

./configure   编译

make && make install 安装


猜你喜欢

转载自blog.csdn.net/daily886/article/details/79504404
今日推荐