阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

CentOS 7下编译安装Nginx+MySQL+PHP

264次阅读
没有评论

共计 23464 个字符,预计需要花费 59 分钟才能阅读完成。

实现环境

CentOS 7 64 位
IP 地址:172.16.0.20
Nginx:1.12.0
MySQL:5.7.18
PHP:7.1.4
yum 源:aliyun 源

如果你的系统是新安装的 RedHat 7 或 Centos 7,可以使用我的一键优化脚本优化系统,有利于系统的使用和下面的安装。点击我查看

首先下载好我们的需要的包

创建一个目录存放下载的软件

[root@localhost ~]# mkdir /software
[root@localhost ~]# cd /software/

软件下载地址
这是我的网盘链接
或者去它们各自的官网下载最新版,下载命令参考

wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-boost-5.7.18.tar.gz
wget https://sourceforge.net/projects/pcre/files/pcre/8.40/pcre-8.40.tar.gz
wget http://nginx.org/download/nginx-1.12.0.tar.gz
wget http://hk1.php.net/distributions/php-7.1.4.tar.gz

这是需要的包

[root@localhost software]# ll
总用量 757804

-rw-r--r--   1 root root   61612105 4 月  18 14:55 mysql-boost-5.7.18.tar.gz
-rw-r--r--.  1 root root     980831 4 月  14 17:08 nginx-1.12.0.tar.gz
-rw-r--r--   1 root root  20319716 4 月  18 15:41 php-7.1.4.tar.gz

关闭系统限制
关闭系统防火墙

[root@localhost software]# systemctl stop firewalld.service 
[root@localhost software]# systemctl disable firewalld.service

关闭 SElinux

[root@localhost software]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
[root@localhost software]# setenforce 0

开始安装 nginx

创建 www 账户 用来启动 nginx

[root@localhost software]# useradd www -s /sbin/nologin

安装依赖的包

[root@localhost software]# yum -y install pcre pcre-devel zlib zlib-devel gcc-c++ gcc openssl*

解压 Nginx 源码包

root@localhost software]# tar zxvf nginx-1.12.0.tar.gz

进入解压后的目录,对 Nginx 进行配置

[root@localhost software]# cd nginx-1.12.0/
[root@localhost nginx-1.12.0]# ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_realip_module --with-http_sub_module --with-http_gzip_static_module --with-http_stub_status_module  --with-pcre

编译和安装

[root@localhost nginx-1.12.0]# make && make install

启动 Nginx

[root@localhost nginx-1.12.0]# /usr/local/nginx/sbin/nginx

浏览器访问测试是否 ok

 

关闭 Nginx 进程

[root@localhost nginx-1.12.0]# killall nginx
[root@localhost nginx-1.12.0]# ps -ef|grep nginx

nginx 命令做软连接方便使用

[root@localhost nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /sbin/nginx

编写 nginx 启动脚本

cat >> /usr/lib/systemd/system/nginx.service << EOF
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=//usr/sbin/nginx -s reload
ExecStop=/usr/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

修改完 systemctl 服务,需要重新加载下 daemon

[root@localhost nginx-1.12.0]# systemctl daemon-reload

用 systemctl 启动 Nginx 服务,并查看状态

[root@localhost nginx-1.12.0]# systemctl start nginx
[root@localhost nginx-1.12.0]# systemctl status nginx
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since 二 2017-04-18 14:06:58 CST; 8s ago
     Docs: http://nginx.org/en/docs/
  Process: 11816 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 11813 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
 Main PID: 11818 (nginx)
   CGroup: /system.slice/nginx.service
           ├─11818 nginx: master process /usr/sbin/nginx
           └─11820 nginx: worker process

4 月 18 14:06:58 localhost.localdomain systemd[1]: Starting nginx - high performance web server...
4 月 18 14:06:58 localhost.localdomain nginx[11813]: nginx: the configuration file /usr/local/nginx/conf/nginx...s ok
4 月 18 14:06:58 localhost.localdomain nginx[11813]: nginx: configuration file /usr/local/nginx/conf/nginx.con...sful
4 月 18 14:06:58 localhost.localdomain systemd[1]: Failed to read PID from file /usr/local/nginx/logs/nginx.pi...ment
4 月 18 14:06:58 localhost.localdomain systemd[1]: Started nginx - high performance web server.

设置 nginx 开机启动

[root@localhost nginx-1.12.0]# systemctl enable nginx

nginx 安装完成,下面安装 mysql


 

安装 MySQL

安装依赖包

[root@localhost nginx-1.12.0]# cd /software/
[root@localhost software]# yum -y install ncurses ncurses-devel bison cmake gcc gcc-c++

创建用户和组

[root@localhost software]# groupadd mysql
[root@localhost software]# useradd -s /sbin/nologin -g mysql mysql -M
[root@localhost software]# id mysql

解压 mysql 源码包

[root@localhost software]# tar zxvf mysql-boost-5.7.18.tar.gz

进入解压后的目录,对 mysql 进行配置(5.5 以上都是 cmake)

[root@localhost software]# cd mysql-5.7.18/
[root@localhost mysql-5.7.18]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock -DSYSCONFDIR=/usr/local/mysql/etc -DSYSTEMD_PID_DIR=/usr/local/mysql -DDEFAULT_CHARSET=utf8  -DDEFAULT_COLLATION=utf8_general_ci -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 -DMYSQL_DATADIR=/usr/local/mysql/data -DWITH_BOOST=boost -DWITH_SYSTEMD=1

记住这个 /usr/local/mysql/mysql.sock,php 连接 mysql 会用到。

编译和安装

[root@localhost mysql-5.7.18]# make && make install

初始化数据库及启动

[root@localhost mysql-5.7.18]# chown -R mysql.mysql /usr/local/mysql/
[root@localhost mysql-5.7.18]# cd /usr/local/mysql/
cat >> my.cnf << EOF
[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
default-character-set=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1

# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M 

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
EOF
[root@localhost mysql]# chown mysql.mysql my.cnf
[root@localhost mysql]# echo 'PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
[root@localhost mysql]# echo 'export PATH' >> /etc/profile
[root@localhost mysql]# source /etc/profile
[root@localhost mysql]# bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
[root@localhost mysql]# cp usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
[root@localhost mysql]# systemctl daemon-reload 
[root@localhost mysql]# systemctl start mysqld
[root@localhost data]# ps -ef|grep mysql

设置 mysql 开机启动

[root@localhost mysql]# systemctl enable mysqld

查看 Mysql 启动状态

[root@localhost mysql]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)
   Active: active (running) since 三 2017-04-19 10:48:20 CST; 25min ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 12734 ExecStart=/usr/local/mysql/bin/mysqld --daemonize --pid-file=/usr/local/mysql/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 12714 ExecStartPre=/usr/local/mysql/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 12737 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─12737 /usr/local/mysql/bin/mysqld --daemonize --pid-file=/usr/local/mysql/mysqld.pid

4 月 19 10:48:20 localhost.localdomain mysqld[12734]: 2017-04-19T02:48:20.943096Z 0 [Note] Server hostname (bi...3306
4 月 19 10:48:20 localhost.localdomain mysqld[12734]: 2017-04-19T02:48:20.943247Z 0 [Note] IPv6 is available.
4 月 19 10:48:20 localhost.localdomain mysqld[12734]: 2017-04-19T02:48:20.943354Z 0 [Note]   - '::' resolves to '::';
4 月 19 10:48:20 localhost.localdomain mysqld[12734]: 2017-04-19T02:48:20.943397Z 0 [Note] Server socket creat...::'.
4 月 19 10:48:20 localhost.localdomain mysqld[12734]: 2017-04-19T02:48:20.966965Z 0 [Note] Event Scheduler: Lo...ents
4 月 19 10:48:20 localhost.localdomain mysqld[12734]: 2017-04-19T02:48:20.967379Z 0 [Note] /usr/local/mysql/bi...ons.
4 月 19 10:48:20 localhost.localdomain mysqld[12734]: Version: '5.7.18'  socket: '/usr/local/mysql/mysql.sock'...tion
4 月 19 10:48:20 localhost.localdomain mysqld[12734]: 2017-04-19T02:48:20.967402Z 0 [Note] Executing 'SELECT *...eck.
4 月 19 10:48:20 localhost.localdomain mysqld[12734]: 2017-04-19T02:48:20.967409Z 0 [Note] Beginning of list o...bles
4 月 19 10:48:20 localhost.localdomain systemd[1]: Started MySQL Server.

进入数据库,创建一个测试数据库以及授权远程用户可访问这个数据库

[root@localhost mysql]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.18 Source distribution

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> create database ceshi CHARACTER SET utf8  COLLATE utf8_general_ci;
Query OK, 1 row affected (0.00 sec)

mysql> grant all on ceshi.* to ceshi@'%' identified by 'ceshi2017';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

查看授权的用户表

[root@localhost mysql]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Server version: 5.7.18 Source distribution

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> SELECT DISTINCT CONCAT('User:''',user,'''@''',host,''';') AS query FROM mysql.user;
+--------------------------------+
| query                          |
+--------------------------------+
| User: 'ceshi'@'%';             |
| User: 'mysql.sys'@'localhost'; |
| User: 'root'@'localhost';      |
+--------------------------------+
3 rows in set (0.00 sec)

在别的机器连接 172.16.0.20 的 ceshi 数据库

[root@localhost ~]# mysql -h172.16.0.20 -uceshi -p'ceshi2017'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.18 Source distribution

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| ceshi              |
+--------------------+
2 rows in set (0.00 sec)

 


PHP 7 安装

PHP 7 在 15 年年底推出,PHP 官方说的比 PHP 5 快 2 倍,就为这个,这个鲜必须尝。不过有个很值得注意的地方是,虽然 PHP 7 增加了不少新特性,但也很多地方是向后不兼容的,例如 mysql 扩展,在 PHP 7 中已经被删除。现在最新版本是 7.1.4。

进入 software 目录

[root@localhost mysql]# cd /software/

接着解压 php 源码包

[root@localhost software]# tar zxvf php-7.1.4.tar.gz

再进入解压后的文件夹

[root@localhost software]# cd php-7.1.4/

这里将只安装一些常用的扩展,大家可以根据自己的实际需要进行增减,可以通过以下命令查看 PHP 安装是具体有有些扩展和选项:

[root@localhost php-7.1.4]# ./configure --help

有接近 300 个选项。
安装之前要先安装那些准备装的扩展要用到的软件模块

[root@localhost php-7.1.4]# yum -y install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel curl curl-devel openssl openssl-devel

接下来 configure PHP 7

[root@localhost php-7.1.4]# ./configure --prefix=/usr/local/php --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-mysqli --with-zlib --with-curl --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-openssl --enable-mbstring --enable-xml --enable-session --enable-ftp --enable-pdo -enable-tokenizer --enable-zip

上面已经提到,PHP 7 已经删除了 MySQL 扩展,所以 -with-mysql 不再是一个有效的选项。这里用 MySQLi 或 PDO 代替。
其中 –prefix 是安装目录,上面提到在同一个服务器安装多个 PHP 版本,这个 –prefix 设定是很有必要的。至于其他扩展大家按实际增减。
如果 configure 成功的话,将会看到以下类似字样:

+--------------------------------------------------------------------+
| License:                                                           |
| This software is subject to the PHP License, available in this     |
| distribution in the file LICENSE.  By continuing this installation |
| process, you are bound by the terms of this license agreement.     |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.                            |
+--------------------------------------------------------------------+

Thank you for using PHP.

编译和安装

[root@localhost php-7.1.4]# make && make install

好,PHP 7 已经安装完成,下面进行配置
先是 PHP 的配置文档

[root@localhost php-7.1.4]# cp php.ini-development /usr/local/php/lib/php.ini

php.ini 路径应该放在 PREFIX/lib 文件夹,除非在安装的时候通过这个选项修改
–with-config-file-path=PATH
如果安装 PHP 时没有指明 –prefix,那么就 php.ini 路径就是 /usr/local/lib/php.ini。刚才安装时有指明 –prefix,所以是 /usr/local/php/lib/php.ini
然后根据实际自己需要修改 php.ini。
查找 mysqli.default_socket,修改成 mysqli.default_socket = /usr/local/mysql/mysql.sock:

[root@localhost php-7.1.4]# grep mysqli.default_socket  /usr/local/php/lib/php.ini
mysqli.default_socket = 
[root@localhost php-7.1.4]# sed -i 's#mysqli.default_socket =#mysqli.default_socket = /usr/local/mysql/mysql.sock#'  /usr/local/php/lib/php.ini
[root@localhost php-7.1.4]# grep mysqli.default_socket  /usr/local/php/lib/php.ini
mysqli.default_socket = /usr/local/mysql/mysql.sock

其中 /usr/local/mysql/mysql.sock 就是上面安装 MySQL 时提到的。这个值必须填,否则会出现如下错误:
Warning: mysqli_connect(): (HY000/2002): No such file or directory

修改时区,查找 date.timezone,改成(主要将前面的 ; 去掉,这个是注释用的):

root@localhost php-7.1.4]# grep date.timezone /usr/local/php/lib/php.ini 
; http://php.net/date.timezone
;date.timezone =
[root@localhost php-7.1.4]# sed -i 's#;date.timezone =#date.timezone = Asia/Shanghai#' /usr/local/php/lib/php.ini 
[root@localhost php-7.1.4]# grep date.timezone /usr/local/php/lib/php.ini 
; http://php.net/date.timezone
date.timezone = Asia/Shanghai

好了,PHP 7 已经安装好,下面验证一下

[root@localhost php-7.1.4]# /usr/local/php/bin/php -v
PHP 7.1.4 (cli) (built: Apr 17 2017 14:58:11) (NTS)
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies

再查看下已经安装的模块

[root@localhost php-7.1.4]# /usr/local/php/bin/php -m
[PHP Modules]
Core
ctype
curl
date
dom
fileinfo
filter
ftp
gd
hash
iconv
json
libxml
mbstring
mysqli
mysqlnd
openssl
pcre
PDO
pdo_sqlite
Phar
posix
Reflection
session
SimpleXML
SPL
sqlite3
standard
tokenizer
xml
xmlreader
xmlwriter
zip
zlib

[Zend Modules]

接下来配置 php-fpm,复制 php-fpm 的配置文档

[root@localhost php-7.1.4]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@localhost php-7.1.4]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf

修改 /usr/local/php/etc/php-fpm.d/www.conf,把启动用户改为和 nginx 服务同一个启动用户(前面 Nginx 使用的是 www 账户,这里改成和 Nginx 使用一样的账户;一般都是使用 www 用户)

[root@localhost php-7.1.4]# grep -E 'user =|group =' /usr/local/php/etc/php-fpm.d/www.conf
user = nginx
group = nginx
[root@localhost php-7.1.4]# sed -i 's#user = nginx#user = www#' /usr/local/php/etc/php-fpm.d/www.conf
[root@localhost php-7.1.4]# sed -i 's#group = nginx#group = www#' /usr/local/php/etc/php-fpm.d/www.conf 
[root@localhost php-7.1.4]# grep -E 'user =|group =' /usr/local/php/etc/php-fpm.d/www.conf
user = www
group = www
;listen.group = www

其中 www.conf 中要留意这个值 listen = 127.0.0.1:9000

[root@localhost php-7.1.4]# grep 'listen = 127.0.0.1' /usr/local/php/etc/php-fpm.d/www.conf

这里使用 9000 端口,这个选项在配置 Nginx 网站时要用到的。
配置 php-fpm 启动服务脚本

[root@localhost php-7.1.4]# cp sapi/fpm/php-fpm.service /usr/lib/systemd/system/

查看启动脚本中指定的程序目录和 pid 文件(默认已经修改过了,如果没有修改过执行下面操作)

[root@localhost php-7.1.4]# grep -E 'PIDFile|ExecStart' /usr/lib/systemd/system/php-fpm.service
PIDFile=/usr/local/php/var/run/php-fpm.pid
ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php/etc/php-fpm.conf

修改启动脚本,把里边 prefix 相关的内容用实际路径代替

[root@localhost php-7.1.4]# vim /usr/lib/systemd/system/php-fpm.service
将
PIDFile=${prefix}/var/run/php-fpm.pid
ExecStart=${exec_prefix}/sbin/php-fpm --nodaemonize --fpm-config ${prefix}/etc/php-fpm.conf
修改成
PIDFile=/usr/local/php/var/run/php-fpm.pid
ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php/etc/php-fpm.conf

重新载入 systemd

[root@localhost php-7.1.4]# systemctl daemon-reload

让 php-fpm 随机启动

[root@localhost php-7.1.4]# systemctl enable php-fpm
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.

立即启动 php-fpm

[root@localhost php-7.1.4]# systemctl start php-fpm

查看状态

[root@localhost php-7.1.4]# systemctl status php-fpm
● php-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor preset: disabled)
   Active: active (running) since 一 2017-04-17 15:37:06 CST; 1min 9s ago
 Main PID: 55770 (php-fpm)
   CGroup: /system.slice/php-fpm.service
           ├─55770 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
           ├─55771 php-fpm: pool www
           └─55772 php-fpm: pool www

4 月 17 15:37:06 localhost.localdomain systemd[1]: Started The PHP FastCGI Process Manager.
4 月 17 15:37:06 localhost.localdomain systemd[1]: Starting The PHP FastCGI Process Manager...

好,php-fpm 已经成功启动,那就立即建个网站看看

配置 Nginx 站点
先建立一个 lnmp 站点,路径是 /var/www/html

[root@localhost php-7.1.4]# mkdir -p /var/www/html
[root@localhost php-7.1.4]# chown -R www.www /var/www

并准备好 phpinfo 测试文件

cat >> /var/www/html/test.php << EOF
<?php
phpinfo();
EOF

创建一个 Nginx 配置文件放到 /usr/local/nginx/conf/conf.d 中

[root@localhost php-7.1.4]# cd /usr/local/nginx/conf
[root@localhost conf]# sed -i '$i\include /usr/local/nginx/conf/conf.d/*;' nginx.conf
[root@localhost conf]# cat nginx.conf
[root@localhost conf]# mkdir conf.d
[root@localhost conf]# cd conf.d/

创建 test.com.conf 文件并写入以下内容

cat >> test.com.conf <<EOF
server {
    listen       81;
    server_name  localhost;
    root         /var/www/html;
    location / {index  index.php index.html index.htm;}
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME    \$document_root\$fastcgi_script_name;
        fastcgi_param  PHP_VALUE          open_basedir=\$document_root:/tmp/:/proc/;
        include        fastcgi_params;
    }
}
EOF

其中 server_name localhost; 中的 localhost 改成你自己的域名(例如:www.baidu.com,这里我直接使用 localhost 和 81 端口来测试。网站域名解析默认都是使用 80 端口的)
其中 root /var/www/html; 就是刚才创建的站点目录
其中 fastcgi_pass 127.0.0.1:9000; 就是上面配置 php-fpm 提到要留意的值
修改配置后一定要记得 reload nginx 才能生效

[root@localhost conf.d]# systemctl reload nginx
[root@localhost conf.d]# systemctl reload php-fpm

最后的配置(nginx 服务器的 IP 必须和域名做解析,才可以使用域名访问服务,域名购买一般在阿里云上购买)
这里我们直接使用 IP:81 访问(因为我们使用的端口是 81;域名是 localhost,也就是 nginx 所在主机 IP 的意思)

 

 


创建一键安装脚本 one-click-install-lnmp.sh

#!/bin/sh 
# __author__ = 'junxi'

# This script is used by fast installed lnmp ......
# write by 2017/04/19

mkdir /software
cd /software/
ll
systemctl stop firewalld.service 
systemctl disable firewalld.service 
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
setenforce 0

######start install nginx######
echo '######start install nginx######'
useradd www -s /sbin/nologin
yum -y install pcre pcre-devel zlib zlib-devel gcc-c++ gcc openssl*
tar zxvf nginx-1.12.0.tar.gz 
cd nginx-1.12.0/
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_realip_module --with-http_sub_module --with-http_gzip_static_module --with-http_stub_status_module  --with-pcre
make && make install
sleep 2
ln -s /usr/local/nginx/sbin/nginx /sbin/nginx
cat >> /usr/lib/systemd/system/nginx.service << EOF
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=//usr/sbin/nginx -s reload
ExecStop=/usr/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload 
systemctl start nginx
systemctl enable nginx
systemctl status nginx
sleep 2
echo '######nginx is install completed done.######'


###### start install mysql ######
cd /software/
yum -y install ncurses ncurses-devel bison cmake gcc gcc-c++
groupadd mysql
useradd -s /sbin/nologin -g mysql mysql -M
id mysql
chown -R mysql.mysql /usr/local/mysql
tar zxvf mysql-boost-5.7.18.tar.gz 
cd mysql-5.7.18/
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock -DSYSCONFDIR=/usr/local/mysql/etc -DSYSTEMD_PID_DIR=/usr/local/mysql -DDEFAULT_CHARSET=utf8  -DDEFAULT_COLLATION=utf8_general_ci -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 -DMYSQL_DATADIR=/usr/local/mysql/data -DWITH_BOOST=boost -DWITH_SYSTEMD=1
sleep 1
make && make install
sleep 2
chown -R mysql.mysql /usr/local/mysql/
cd /usr/local/mysql/
echo '######create my.cnf######'
cat >> my.cnf << EOF
[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
default-character-set=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1

# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M 

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
EOF

chown mysql.mysql my.cnf
echo 'PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
echo 'export PATH' >> /etc/profile
source /etc/profile
bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
cp usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
systemctl daemon-reload 
systemctl start mysqld
systemctl enable mysqld
ps -ef|grep mysql
systemctl status mysqld
echo '######mysql is install completed done.######'


###### start install php ######
cd /software
tar zxvf php-7.1.4.tar.gz
cd php-7.1.4/
./configure --help
yum -y install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel curl curl-devel openssl openssl-devel
./configure --prefix=/usr/local/php --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-mysqli --with-zlib --with-curl --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-openssl --enable-mbstring --enable-xml --enable-session --enable-ftp --enable-pdo -enable-tokenizer --enable-zip
sleep 1
make && make install
sleep 2
cp php.ini-development /usr/local/php/lib/php.ini
grep mysqli.default_socket  /usr/local/php/lib/php.ini
sed -i 's#mysqli.default_socket =#mysqli.default_socket = /usr/local/mysql/mysql.sock#'  /usr/local/php/lib/php.ini
grep mysqli.default_socket  /usr/local/php/lib/php.ini
grep date.timezone /usr/local/php/lib/php.ini
sed -i 's#;date.timezone =#date.timezone = Asia/Shanghai#' /usr/local/php/lib/php.ini
grep date.timezone /usr/local/php/lib/php.ini
/usr/local/php/bin/php -v
/usr/local/php/bin/php -m
cp /usr/local/php/etc/php-fpm.conf.default
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
grep -E 'user =|group =' /usr/local/php/etc/php-fpm.d/www.conf
sed -i 's#user = nginx#user = www#' /usr/local/php/etc/php-fpm.d/www.conf
sed -i 's#group = nginx#group = www#' /usr/local/php/etc/php-fpm.d/www.conf 
grep -E 'user =|group =' /usr/local/php/etc/php-fpm.d/www.conf
cp sapi/fpm/php-fpm.service /usr/lib/systemd/system/
grep -E 'PIDFile|ExecStart' /usr/lib/systemd/system/php-fpm.service
systemctl daemon-reload
systemctl enable php-fpm
systemctl start php-fpm
systemctl status php-fpm
echo '######php is install completed done.######'

####### create test.com file used by test lnmp config is correct or incorrect ######
mkdir -p /var/www/html
chown -R www.www /var/www
cat >> /var/www/html/test.php << EOF
<?php
phpinfo();
EOF
cd /usr/local/nginx/conf
sed -i '$i\include /usr/local/nginx/conf/conf.d/*;' nginx.conf
mkdir conf.d
cd conf.d/
echo '######create test.com.conf site file######'
cat >> test.com.conf <<EOF
server {
    listen       81;
    server_name  localhost;
    root         /var/www/html;
    location / {index  index.php index.html index.htm;}
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME    \$document_root\$fastcgi_script_name;
        fastcgi_param  PHP_VALUE          open_basedir=\$document_root:/tmp/:/proc/;
        include        fastcgi_params;
    }
}
EOF
systemctl reload nginx
systemctl reload php-fpm
sleep 2
echo '######LNMP is install completed done.######'
echo '######please Open the similar"172.16.0.20:81/test.php"to Visit the test.######'

把 nginx-1.12.0.tar.gz、mysql-boost-5.7.18.tar.gz、php-7.1.4.tar.gz 这三个文件和 one-click-install-lnmp.sh 脚本下载下来,放到 /software 目录下,放在同一个目录下,运行下面命令进行安装

[root@localhost ~]# mkdir /software
[root@localhost ~]# cd /software/
[root@localhost software]# ll
总用量 80984
-rw-r--r--. 1 root root 61612105 4 月  19 14:22 mysql-boost-5.7.18.tar.gz
-rw-r--r--. 1 root root   980831 4 月  19 14:23 nginx-1.12.0.tar.gz
-rw-r--r--. 1 root root     6282 4 月  19 14:24 one-click-install-lnmp.sh
-rw-r--r--. 1 root root 20319716 4 月  19 14:23 php-7.1.4.tar.gz
[root@localhost software]# sh one-click-install-lnmp.sh

等待安装完成即可。

LNMP 环境搭建(Discuz 论坛)  http://www.linuxidc.com/Linux/2016-03/129334.htm

Ubuntu 14.04 下 apt-get 方法安装 LNMP 环境  http://www.linuxidc.com/Linux/2016-07/133683.htm

CentOS 7 源码编译安装 PHP5.6 和 Nginx1.7.9 及 MySQL(搭建 LNMP 环境) http://www.linuxidc.com/Linux/2015-12/126200.htm

Ubuntu 14.04 LTS 安装 LNMP Nginx\PHP5 (PHP-FPM)\MySQL  http://www.linuxidc.com/Linux/2014-05/102351.htm

CentOS 6.8 编译安装 LNMP 简述  http://www.linuxidc.com/Linux/2017-05/143667.htm

Ubuntu 16.04 下源码配置 LNMP 开发环境 http://www.linuxidc.com/Linux/2016-09/135381.htm

CentOS 7 源码编译安装 PHP5.6 和 Nginx1.7.9 及 MySQL(搭建 LNMP 环境) http://www.linuxidc.com/Linux/2015-12/126200.htm

CentOS 7 源码安装最新版 LNMP 环境 http://www.linuxidc.com/Linux/2015-04/116058.htm

CentOS 6.8 安装 LNMP 环境(Linux+Nginx+MySQL+PHP)http://www.linuxidc.com/Linux/2017-04/142880.htm

Ubuntu 系统下 LNMP 环境的搭建  http://www.linuxidc.com/Linux/2017-04/142610.htm

Ubuntu 16.04 LTS 下 LNMP 环境配置简述  http://www.linuxidc.com/Linux/2017-05/144252.htm

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-10/147581.htm

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计23464字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中