本文适用于熟悉
Linux
操作系统,又不想使用第三方面板的用户,最近项目上买了阿里的服务器,由于安全考虑,又不能使用一些面板来部署,只能手动部署整个环境了,毕竟服务器的管理权限交给第三方面板始终还是有点不安全的,之前宝塔就爆出phpmyadmin
漏洞,无需任何验证就可以直接进数据库。部署过程留个记录,中间还是折腾了一番,分享给有需求的同学。
LNMP环境
LNMP代表的就是:Linux系统下Nginx+MySQL+PHP这种架构
1、Linux是一类Unix计算机操作系统的统称,是目前最流行的免费操作系统。代表版本有:debian、centos、ubuntu、fedora、gentoo等。
2、Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。
3、Mysql是一个小型关系型数据库管理系统。
4、PHP是一种在服务器端执行的嵌入HTML文档的脚本语言。
步骤一:准备编译环境
1.运行systemctl status firewalld命令查看当前防火墙的状态。
- 如果防火墙的状态参数是
inactive
,则防火墙为关闭状态。 - 如果防火墙的状态参数是
active
,则防火墙为开启状态。本示例中防火墙为开启状态,因此需要关闭防火墙。
2.关闭防火墙。如果防火墙为关闭状态可以忽略此步骤。
临时关闭防火墙,运行命令
$ systemctl stop firewalld
说明 : 暂时关闭防火墙,下次重启Linux后,防火墙还会开启
永久关闭防火墙,运行命令
$ systemctl disable firewalld
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
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
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
常见问题
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
[collapse status="false" title="如何使用其它版本的mysql或PHP?"] 答:如上所示,都差不多的,去官网下你需要的版本安装包,然后手动安装再配置就可以了,篇幅较长,可自行百度下。