最近腾讯云服务器在搞活动,我在上面买了1 核 1 GB 1 Mbps配置系统CentOS7.5,然后源码编译安装了原汁原味的LNMP黄金组合,这套组合也是目前市面上公认的黄金组合,下面就为大家详细描述一下安装过程,供大家参考。

一、安装PHP

1、访问php官方网站下载地址https://www.php.net/downloads.php,截止到本人发稿时php最新版本是7.3.8,如果后面有升级版本,也可按本教程安装即可,如图所示:

PHP官网下载页面

2、登陆到服务器终端,选择一个放置源码包的目录,我个人习惯把源码包放置在/data/src下面,在php官网下载页面选择php-7.3.8.tar.gz这个安装包,鼠标右击复制链接地址,执行命令下载源码包wget https://www.php.net/distributions/php-7.3.8.tar.gz

[root@VM_0_8_centos ~]# cd /data/src/
[root@VM_0_8_centos src]# wget https://www.php.net/distributions/php-7.3.8.tar.gz
--2019-08-07 23:52:46--  https://www.php.net/distributions/php-7.3.8.tar.gz
正在解析主机 www.php.net (www.php.net)... 185.85.0.29, 2a02:cb40:200::1ad
正在连接 www.php.net (www.php.net)|185.85.0.29|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:未指定 [application/x-gzip]
正在保存至: “php-7.3.8.tar.gz”

    [                  <=>                                                     ] 19,559,584  4.53MB/s 用时 4.1s

2019-08-07 23:52:51 (4.53 MB/s) - “php-7.3.8.tar.gz” 已保存 [19559584]

[root@VM_0_8_centos src]#

3、解压源码包,进入加压后的文件夹

[root@VM_0_8_centos src]# tar -xf php-7.3.8.tar.gz
[root@VM_0_8_centos src]# cd php-7.3.8

4、这一步就是开始执行configure配置命令了,配置过程会出现各种错误,一般都是缺少各种依赖造成的,提前安装好以下依赖包防止配置过程报缺少依赖的错误:

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

特别注意配置过程中,如果出现这个错误configure: error: Please reinstall the libzip distribution,则执行下面命令进行安装此依赖:

[root@VM_0_8_centos php-7.3.8]# yum remove libzip
[root@VM_0_8_centos php-7.3.8]# cd ../
[root@VM_0_8_centos src]# wget https://nih.at/libzip/libzip-1.2.0.tar.gz
[root@VM_0_8_centos src]# tar -xf libzip-1.2.0.tar.gz
[root@VM_0_8_centos src]# ll
总用量 20276
drwxrwxr-x  7 root root     4096 4月  11 2018 libzip-1.2.0
-rw-r--r--  1 root root  1189827 3月  12 09:18 libzip-1.2.0.tar.gz
drwxr-xr-x 16 1000 1000     4096 8月   8 10:42 php-7.3.8
-rw-r--r--  1 root root 19559584 7月  30 05:40 php-7.3.8.tar.gz
[root@VM_0_8_centos src]# cd libzip-1.2.0
[root@VM_0_8_centos libzip-1.2.0]# ./configure
此处是配置输出的内容省略....
[root@VM_0_8_centos libzip-1.2.0]# make && make install
此处是编译安装输出的内容省略....

如果出现错误:configure: error: off_t undefined; check your library configuration,则进行下面修改:

[root@VM_0_8_centos nginx-1.17.2]# vim /etc/ld.so.conf 
#添加如下几行
/usr/local/lib64
/usr/local/lib
/usr/lib
/usr/lib64 
#保存退出
:wq
[root@VM_0_8_centos nginx-1.17.2]# ldconfig -v # 使之生效

安装完所需要的依赖包以后,就可以执行配置命令了,按照下面的配置即可满足日常所需,其中–prefix是php的安装目录,可自定义目录位置,–with-config-file-path为php.ini的配置目录,服务器配置内存小于1G的必须加上–disable-fileinfo这个配置,否则会报错,执行下面配置:

[root@VM_0_8_centos php-7.3.8]#./configure --prefix=/usr/local/php7 --with-config-file-path=/usr/local/php7 --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-opcache --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-gettext --enable-mbstring --with-iconv --with-mcrypt --with-mhash --with-openssl --enable-bcmath --enable-soap --with-libxml-dir --enable-pcntl --enable-shmop --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-sockets --with-curl --with-zlib --enable-zip --with-bz2 --with-readline --without-sqlite3 --without-pdo-sqlite --with-pear --disable-fileinfo 

配置成功以后会出现下面的提示:

Generating files
configure: creating ./config.status
creating main/internal_functions.c
creating main/internal_functions_cli.c
+--------------------------------------------------------------------+
| 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.

config.status: creating php7.spec
config.status: creating main/build-defs.h
config.status: creating scripts/phpize
config.status: creating scripts/man1/phpize.1
config.status: creating scripts/php-config
config.status: creating scripts/man1/php-config.1
config.status: creating sapi/cli/php.1
config.status: creating sapi/fpm/php-fpm.conf
config.status: creating sapi/fpm/www.conf
config.status: creating sapi/fpm/init.d.php-fpm
config.status: creating sapi/fpm/php-fpm.service
config.status: creating sapi/fpm/php-fpm.8
config.status: creating sapi/fpm/status.html
config.status: creating sapi/phpdbg/phpdbg.1
config.status: creating sapi/cgi/php-cgi.1
config.status: creating ext/phar/phar.1
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands

5、执行make && make install 开始安装php

[root@VM_0_8_centos php-7.3.8]# make && make install

6、最后就是开始配置php环境了。

复制配置文件:

[root@VM_0_8_centos php-7.3.8]# cp /data/src/php-7.3.8/php.ini-development /usr/local/php7/php.ini
[root@VM_0_8_centos ~]# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
[root@VM_0_8_centos ~]# cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf

将php和php-fpm都加入环境变量:

[root@VM_0_8_centos ~]# vim /etc/profile

PATH=$PATH:$HOME/bin:/usr/local/php7/bin:/usr/local/php7/sbin
export PATH

[root@VM_0_8_centos ~]# source /etc/profile

启动php-fpm:

[root@VM_0_8_centos ~]# php-fpm
[root@VM_0_8_centos ~]# ps -ef |grep php-fpm
root     10394     1  0 8月01 ?       00:00:12 php-fpm: master process (/usr/local/php7/etc/php-fpm.conf)
nginx    14018 10394  0 8月07 ?       00:00:43 php-fpm: pool www
nginx    15631 10394  0 00:05 ?        00:00:41 php-fpm: pool www
nginx    16710 10394  0 07:17 ?        00:00:21 php-fpm: pool www
root     27340 23694  0 12:00 pts/0    00:00:00 grep --color=auto php-fpm

停止php-fpm:

[root@VM_0_8_centos ~]# pkill php-fpm

查看php版本:

[root@VM_0_8_centos ~]# php -v
PHP 7.3.8 (cli) (built: Aug  1 2019 18:14:40) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.7, Copyright (c) 1998-2018 Zend Technologies

二、安装nginx

1、去nginx 官网https://nginx.org/en/download.html下载后缀为tar.gz的安装包,我选择的是1.17.2版本

[root@VM_0_8_centos src]# wget https://nginx.org/download/nginx-1.17.2.tar.gz

2、解压配置安装

[root@VM_0_8_centos src]# tar -xf nginx-1.17.2.tar.gz
[root@VM_0_8_centos src]# cd nginx-1.17.2
[root@VM_0_8_centos nginx-1.17.2]# ./configure
此处省略配置输出信息...
[root@VM_0_8_centos nginx-1.17.2]# make && make install

3、配置nginx环境变量

[root@VM_0_8_centos nginx-1.17.2]# whereis nginx
/usr/local/nginx/sbin/nginx
[root@VM_0_8_centos nginx-1.17.2]# vim /etc/profile
PATH=$PATH:$HOME/bin:/usr/local/php7/bin:/usr/local/php7/sbin:/usr/local/nginx/sbin
export PATH
[root@VM_0_8_centos nginx-1.17.2]# source /etc/profile

4、启动、停止和重启nginx

启动:nginx
强制停止:nginx -s stop #原理是先查出nginx进程id再使用kill命令强制杀掉进程
平滑停止:nginx -s quit #原理是待nginx进程处理任务完毕进行停止,推荐此方式平滑退出
重启:nginx -s reload #修改nginx的*.conf配置文件后,执行此命令可直接使配置生效而不必执行停止启动命令

5、启动nginx后查看nginx进程:

[root@VM_0_8_centos nginx-1.17.2]# ps -ef |grep nginx |grep -v grep
root     15030     1  0 8月07 ?       00:00:00 nginx: master process nginx
nginx    15032 15030  0 8月07 ?       00:00:01 nginx: worker process

6、设置nginx开机自启动:

[root@VM_0_8_centos nginx-1.17.2]# vim /etc/rc.local
追加一行 /usr/local/nginx/sbin/nginx
[root@VM_0_8_centos nginx-1.17.2]# chmod 755 rc.local

7、配置nginx和php-fpm通讯

[root@VM_0_8_centos src]# vim /usr/local/nginx/conf/nginx.conf
添加一行 include vhosts/*.conf;
[root@VM_0_8_centos src]# mkdir /usr/local/nginx/conf/vhosts
[root@VM_0_8_centos src]#vim /usr/local/nginx/conf/vhosts/yourdomain.conf

server {
	listen 80;
	server_name www.yourdomain.com; #你的域名
	root /data/web/www.yourdomain.com; #网站主页路径。此路径仅供参考,具体请您按照实际目录操作。
	index index.php index.html index.htm;
	location / {
	   index index.php index.html index.htm;
	}
	location ~ \.php$ {
        root /data/web/www.yourdomain.com;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

8、在上面配置的网站根目录下新建一个info.php,测试nginx和php是否正常安装

<?php
phpinfo();

成功之后会出现下面页面:

三、安装mysql

1、去mysql官网https://dev.mysql.com/downloads/repo/yum/,找到mysql80-community-release-el7-3.noarch.rpm下载yum源

2、添加yum源并执行安装命令:

[root@VM_0_8_centos src]# yum localinstall mysql80-community-release-el7-3.noarch.rpm
[root@VM_0_8_centos src]# yum install mysql-community-server

3、安装完成之后就可以启动mysql服务了:

[root@VM_0_8_centos src]# systemctl start mysqld.service

查看mysql状态:

[root@VM_0_8_centos src]# systemctl status mysqld.service
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since 三 2019-07-31 23:41:11 CST; 1 weeks 0 days ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 19381 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 19404 (mysqld)
   Status: "Server is operational"
   CGroup: /system.slice/mysqld.service
           └─19404 /usr/sbin/mysqld

7月 31 23:41:09 VM_0_8_centos systemd[1]: mysqld.service holdoff time over, scheduling restart.
7月 31 23:41:09 VM_0_8_centos systemd[1]: Starting MySQL Server...
7月 31 23:41:11 VM_0_8_centos systemd[1]: Started MySQL Server.

关闭和重启mysql命令:

[root@VM_0_8_centos src]# systemctl stop mysqld.service
[root@VM_0_8_centos src]# systemctl restart mysqld.service

4、启动mysql服务以后,需要修改root密码,这个密码存放在文件/var/log/mysqld.log中,可通过下面方式找到临时密码:

[root@VM_0_8_centos src]# grep 'temporary password' /var/log/mysqld.log
2019-07-31T08:20:19.590478Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: aEqXa?@&TB9v-
[root@VM_0_8_centos src]#

连接数据库输入上面的临时密码登录后进行密码修改:

[root@VM_0_8_centos src]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13684
Server version: 8.0.17 MySQL Community Server - GPL

Copyright (c) 2000, 2019, 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> ALTER USER 'root'@'localhost' IDENTIFIED BY 'yourNewPassword';

如果修改密码出现强度不够的提示,尽量给一个强密码,修改完密码,重启mysql服务使新密码生效:

[root@VM_0_8_centos src]# systemctl restart mysqld.service

5、开放mysql远程连接的权限

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set host = '%' where user = 'root';
mysql> flush privileges;

腾讯云和阿里云控制台都有一个安全组规则,记得找到安全组权限开放3306端口权限否则还是无法远程连接mysql服务,为了省事我自己开放了所有端口权限,根据个人情况来设定

至此LNMP黄金组合成功搭建完毕!

发表评论

电子邮件地址不会被公开。 必填项已用*标注