LNMP环境搭建
1. nginx安装
- 安装
yum install nginx
- 启动
systemctl start nginx
systemctl restart nginx
如果不能安装,
2.安装PHP及相关模块:
- 安装php-fpm:
yum install php-fpm
- 安装完成后配置文件在/etc/php-fpm.conf,配置引用了/etc/php-fpm.d/*.conf,默认有一个www.conf,修改www.conf,找到用户、用户组设置:
user = nginx 39行
group = nginx 41行
- 修改/etc/php.ini文件:提高安全性能
;cgi.fix_pathinfo=1 修改为 cgi.fix_pathinfo=0
- 启动服务:
systemctl start php-fpm.service
- 配置nginx支持php网页:vim /etc/nginx/nginx.conf.default找到以下内容:
location ~\.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php,phpinfo.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
将内容复制到nginx.conf中,效果如下:
第一个location是原先就有的,
location / {
root /usr/share/nginx/html;
index index.html index.htm index.php;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
3.安装mysql数据库
- 安装mariadb:
yum install mariadb mariadb-server mariadb-libs mariadb-devel
- 检查是否安装成功:
rpm -qa |grep maria
- 开启mariadb服务,并设置开启启动,检查mysql状态
systemctl start mariadb
systemctl enable mariadb
systemctl status mariadb
- 数据库安全设置
mysql_secure_installation
Enter current password for root (enter for none): 敲回车键
Set root password? [Y/n] 按Y键
New password: 123456(真实环境中设置复杂密码)
Re-enter new password: 123456
Remove anonymous users? [Y/n] n
Disallow root login remotely?[Y/n] n
Remove test database and access to it? [Y/n] n
Reload privilege tables now? [Y/n] n
-
数据库基本操作
- 登录数据库:mysql -u root -p
MariaDB [(none)]> mariadb的命令操作提示符 MariaDB [(none)]> show databases; 查看有哪些数据库 [(none)]> use mysql; 选择数据库 MariaDB [mysql]> show tables; 查看数据库中的表 MariaDB [mysql]> select * from 表名 查询表中所有数据 MariaDB [mysql]> select user,password from 表名 只查询表中usr与password字段的内容。
- 数据库备份:
MariaDB [mysql]> exit 退出 [root@teacher mnt]# mysqldump couman --user=root --password=123456>db-01.mysql; 将couman数据库备份到当前目录下,叫db-01.mysql
- 数据库的恢复:
首先创建一个空数据库存 MariaDB [(none)]> create database couman; Query OK, 1 row affected (0.00 sec)
- 备份文件中恢复数据库(导入数据库):
MariaDB [mysql]> exit 退出 [root@teacher mnt]# mysql -u root -p couman<db-01.mysql Enter password:
4.域名发布网站
- 在/etc/hosts下添加域名,也可以配置DNS服务器
vim /etc/hosts
添加:
192.168.161.131 www.book.com
192.168.161.131 www.anxing.com
- 将网站上传到服务器/data(目录可以自己制定)目录下:
mkdir /data
- 配置nginx.conf访问多个网站:
vim /etc/nginx/nginx.conf
末尾"}"之前添加:
include vhost/*.conf
- 添加配置文件:
mkdir /etc/nginx/vhost
cd /etc/nginx/vhost
- www.book.com网站:
vim book.conf
server{
listen 192.168.161.131:80;
server_name www.book.com;
location / {
root /data/book;
Index index.html index.php;
}
location ~ \.php$ {
root /data/book;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
- www.anxing.com网站:
vim anxing.com
server{
listen 192.168.161.131:80;
server_name www.anxing.com;
location / {
root /data/anxing;
Index index.html index.php;
}
location ~ \.php$ {
root /data/anxing;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
- 重启服务:
systemctl restart nginx
systemctl restart php-fpm
systemctl restart mariadb
- 这里关闭防火墙,selinux安全机制:
systemctl stop firewalld
setenforce 0
- 域名访问:www.book.com www.anxing.com
配置多个网站,不同域名访问