Loading... > 本文适用于熟悉`Linux`操作系统,又不想使用第三方面板的用户,最近项目上买了阿里的服务器,由于安全考虑,又不能使用一些面板来部署,只能手动部署整个环境了,毕竟服务器的管理权限交给第三方面板始终还是有点不安全的,之前宝塔就爆出 `phpmyadmin` 漏洞,无需任何验证就可以直接进数据库。部署过程留个记录,中间还是折腾了一番,分享给有需求的同学。 ## LNMP环境 **LNMP代表的就是:Linux系统下Nginx+MySQL+PHP这种架构** **1**、[Linux](https://baike.baidu.com/item/Linux)是一类[Unix](https://baike.baidu.com/item/Unix)计算机操作系统的统称,是目前最流行的免费操作系统。代表版本有:[debian](https://baike.baidu.com/item/debian)、[centos](https://baike.baidu.com/item/centos)、[ubuntu](https://baike.baidu.com/item/ubuntu)、[fedora](https://baike.baidu.com/item/fedora)、[gentoo](https://baike.baidu.com/item/gentoo)等。 **2**、[Nginx](https://baike.baidu.com/item/Nginx)是一个高性能的HTTP和[反向代理](https://baike.baidu.com/item/反向代理)服务器,也是一个[IMAP](https://baike.baidu.com/item/IMAP/350154)/[POP3](https://baike.baidu.com/item/POP3/175122)/[SMTP](https://baike.baidu.com/item/SMTP/175887)代理服务器。 **3**、Mysql是一个小型[关系型数据库管理系统](https://baike.baidu.com/item/关系型数据库管理系统)。 **4**、[PHP](https://baike.baidu.com/item/PHP)是一种在服务器端执行的嵌入HTML文档的[脚本语言](https://baike.baidu.com/item/脚本语言)。 ## 步骤一:准备编译环境 <div class="tip inlineBlock warning"> 关闭防火墙 </div> **1**.运行**systemctl status firewalld**命令查看当前防火墙的状态。  - 如果防火墙的状态参数是`inactive`,则防火墙为关闭状态。 - 如果防火墙的状态参数是`active`,则防火墙为开启状态。本示例中防火墙为开启状态,因此需要关闭防火墙。 **2**.关闭防火墙。如果防火墙为关闭状态可以忽略此步骤。 - 临时关闭防火墙,运行命令 ``` $ systemctl stop firewalld ``` **说明** : 暂时关闭防火墙,下次重启Linux后,防火墙还会开启 - 永久关闭防火墙,运行命令 ``` $ systemctl disable firewalld ``` <div class="tip inlineBlock warning"> 关闭SELinux </div> **1**.运行**getenforce**命令查看SELinux的当前状态。  - 如果`SELinux`状态参数是`Disabled`,则`SELinux`为关闭状态。 - 如果`SELinux`状态参数是`Enforcing`,则`SELinux`为开启状态。本示例中`SELinux`为开启状态,因此需要关闭 **2**.关闭`SELinux`。如果`SELinux`为关闭状态可以忽略此步骤。 - 如果想临时关闭SELinux,运行命令 ``` $ setenforce 0 ``` **说明** :暂时关闭`SELinux`,下次重启`Linux`后,`SELinux`还会开启。 ## 步骤二:安装Nginx **1**.安装`Nginx`。 ``` $ yum -y install nginx ``` **2**.查看`Nginx`版本 ``` $ nginx -v ``` 返回结果如下所示,表示`Nginx`安装成功。 ``` nginx version: nginx/1.16.1 ``` ## 步骤三:安装`MySQL` **1**.更新`YUM`源 ``` $ rpm -Uvh http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm ``` **2**.安装`MySQL` **说明** : 如果使用的操作系统内核版本为`el8`,可能会提示报错信息`No match for argument`。需要先运行命令**yum module disable mysql**禁用默认的`mysql`模块,再安装`MySQL` ``` $ yum -y install mysql-community-server ``` **3**.查看`MySQL`版本号 ``` $ mysql -V ``` **4**.返回结果如下所示,表示`MySQL`安装成功 ``` $ mysql Ver 14.14 Distrib 5.7.28, for Linux (x86_64) using EditLine wrapper ``` **5**.启动`MySQL` ``` $ systemctl start mysqld ``` **6**.设置开机启动`MySQL` ``` $ systemctl enable mysqld $ systemctl daemon-reload ``` ## 步骤四:安装PHP **1**.更新`YUM`源,添加`epel`源。 ``` $ yum install \ $ https://repo.ius.io/ius-release-el7.rpm \ $ https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm ``` **2**.添加`Webtatic`源。 ``` $ rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm ``` **3**.安装`PHP`。 ``` $ yum -y install php70w-devel php70w.x86_64 php70w-cli.x86_64 php70w-common.x86_64 php70w-gd.x86_64 php70w-ldap.x86_64 php70w-mbstring.x86_64 php70w-mcrypt.x86_64 php70w-pdo.x86_64 php70w-mysqlnd php70w-fpm php70w-opcache php70w-pecl-redis php70w-pecl-mongodb ``` **4**.查看`PHP`版本。 ``` $ php -v ``` **5**.返回结果如下所示,表示安装成功。 ``` PHP 7.0.33 (cli) (built: Dec 6 2018 22:30:44) ( NTS ) Copyright (c) 1997-2017 The PHP Group Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies with Zend OPcache v7.0.33, Copyright (c) 1999-2017, by Zend Technologies ``` ## 步骤五:配置Nginx **1**.先备份`Nginx`配置文件 ``` $ cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak ``` **2**.修改`Nginx`配置文件,添加`Nginx`对`PHP`的支持。 **说明** : 若不添加此配置信息,后续使用浏览器访问PHP页面时,页面将无法显示 **3**.打开`Nginx`配置文件 ``` $ vim /etc/nginx/nginx.conf ``` **4**.在`server`大括号内,修改或添加下列配置信息 ``` #除下面提及的需要添加的配置信息外,其他配置保持默认值即可。 #将location / 大括号内的信息修改为以下所示,配置网站被访问时的默认首页。 location / { index index.php index.html index.htm; } ``` ``` #添加下列信息,配置Nginx通过fastcgi方式处理的PHP请求。 location ~ .php$ { root /usr/share/nginx/html; #将/usr/share/nginx/html替换为的网站根目录,本教程使用/usr/share/nginx/html作为网站根目录。 fastcgi_pass 127.0.0.1:9000; #Nginx通过本机的9000端口将PHP请求转发给PHP-FPM进行处理。 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; #Nginx调用fastcgi接口处理PHP请求。 } ``` 添加配置信息后,如下图所示  **5**.启动`Nginx`服务 ``` $ systemctl start nginx ``` **6**.设置`Nginx`服务开机自启动 ``` $ systemctl enable nginx ``` ## 步骤六:配置MySQL **1**.查看`/var/log/mysqld.log`文件,获取并记录`root`用户的初始密码。 ``` grep 'temporary password' /var/log/mysqld.log ``` 返回结果如下: ``` 2016-12-13T14:57:47.535748Z 1 [Note] A temporary password is generated for root@localhost: p0/G28g>lsHD ``` **说明** : 下一步重置`root`用户密码时,会使用该初始密码。 **2**.配置`MySQL`的安全性。 ``` mysql_secure_installation ``` <div class="tip inlineBlock error"> 安全性的配置包含以下五个方面 </div> **1**. 重置root账号密码。 ``` Enter password for user root: #输入上一步获取的root用户初始密码 The 'validate_password' plugin is installed on the server. The subsequent steps will run with the existing configuration of the plugin. Using existing password for root. Estimated strength of the password: 100 Change the password for root ? (Press y|Y for Yes, any other key for No) : Y #是否更改root用户密码,输入Y New password: #输入新密码,长度为8至30个字符,必须同时包含大小写英文字母、数字和特殊符号。特殊符号可以是()` ~!@#$%^&*-+=|{}[]:;‘<>,.?/ Re-enter new password: #再次输入新密码 Estimated strength of the password: 100 Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y ``` **2**. 输入Y删除匿名用户账号。 ``` By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y #是否删除匿名用户,输入Y Success. ``` **3**. 输入Y禁止root账号远程登录。 ``` Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y #禁止root远程登录,输入Y Success. ``` **4**. 输入Y删除test库以及对test库的访问权限。 ``` Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y #是否删除test库和对它的访问权限,输入Y - Dropping test database... Success. ``` **5**. 输入Y重新加载授权表。 ``` Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y #是否重新加载授权表,输入Y Success. All done! ``` ## 步骤七:配置PHP <div class="tip inlineBlock warning"> 新建phpinfo.php文件,用于展示PHP信息 </div> **1**.新建文件 ``` vim <网站根目录>/phpinfo.php #将<网站根目录>替换为配置的网站根目录。 ``` 网站根目录是在`nginx.conf`文件中`location ~ .php$`大括号内配置的`root`值,如下图所示  我的网站根目录为`/usr/share/nginx/html`,因此命令为: ``` vim /usr/share/nginx/html/phpinfo.php ``` **2**.输入下列内容,函数`phpinfo()`会展示PHP的所有配置信息 ``` <?php echo phpinfo(); ?> ``` **3**.启动`PHP-FPM` ``` systemctl start php-fpm ``` **4**.设置`PHP-FPM`开机自启动 ``` systemctl enable php-fpm ``` ## 步骤八:测试访问 浏览器访问:`http://<公网IP地址>/phpinfo.php` **1**.返回结果如下图所示,表示`LNMP`环境部署成功。 **2**.测试访问`LNMP`平台成功后,建议将`phpinfo.php`文件删除,消除安全隐患 ``` rm -rf <网站根目录>/phpinfo.php #将<网站根目录>替换为在nginx.conf中配置的网站根目录 ``` **3**.本教程配置的网站根目录为`/usr/share/nginx/html`,因此命令为: ``` rm -rf /usr/share/nginx/html/phpinfo.php ``` ## 常见问题 <div class="panel panel-default collapse-panel box-shadow-wrap-lg"><div class="panel-heading panel-collapse" data-toggle="collapse" data-target="#collapse-7f54bfea741ad6869a34d477f42274e839" aria-expanded="true"><div class="accordion-toggle"><span>如何使用其它版本的Nginx服务器?</span> <i class="pull-right fontello icon-fw fontello-angle-right"></i> </div> </div> <div id="collapse-7f54bfea741ad6869a34d477f42274e839" class="panel-body collapse"> 答:访问Nginx开源社区http://nginx.org/en/download.html,获取对应版本的下载链接 **1**.请根据的个人需求,选择对应的`Nginx`版本,以`Nginx 1.8.1`为例 ``` wget http://nginx.org/download/nginx-1.8.1.tar.gz ``` **2**.安装`Nginx`相关依赖。 ``` yum install -y gcc-c++ yum install -y pcre pcre-devel yum install -y zlib zlib-devel yum install -y openssl openssl-devel ``` **3**.解压Nginx 1.8.1安装包,然后进入Nginx所在的文件夹。 ``` tar zxvf nginx-1.8.1.tar.gz cd nginx-1.8.1 ``` **4**.依次,编译源码。 ``` ./configure \ --user=nobody \ --group=nobody \ --prefix=/usr/local/nginx \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --with-http_realip_module \ --with-http_sub_module \ --with-http_ssl_module make && make install ``` **5**.进入`Nginx`的`sbin`目录,然后启动`Nginx` ``` cd /usr/local/nginx/sbin/ ./nginx ``` </div></div> <div class="panel panel-default collapse-panel box-shadow-wrap-lg"><div class="panel-heading panel-collapse" data-toggle="collapse" data-target="#collapse-dd550fc0899d9d4d9f625313dd102d3137" aria-expanded="true"><div class="accordion-toggle"><span>如何使用其它版本的mysql或PHP?</span> <i class="pull-right fontello icon-fw fontello-angle-right"></i> </div> </div> <div id="collapse-dd550fc0899d9d4d9f625313dd102d3137" class="panel-body collapse"> 答:如上所示,都差不多的,去官网下你需要的版本安装包,然后手动安装再配置就可以了,篇幅较长,可自行百度下。 </div></div> Last modification:February 19th, 2021 at 10:45 am © 来自互联网 Support 如果你想请我喝奶茶的话 ×Close Appreciate the author Sweeping payments Pay by AliPay Pay by WeChat