今天小编跟大家讲解下有关“CentOS7安装LNMP环境 如何使用yum工具分开安装LNMP?”的内容 ,相信小伙伴们对这个话题应该有所关注吧,小编也收集到了相关资料,希望小伙伴们看了有所帮助。
LNMP环境:linux(centos7)+ngix(1.12.2)+mariadb(5.5.56)+php(5.4.16)
一、安装MariaDB
[plain] view plain copy
#yum install mariadb mariadb-server #询问是否要安装,输入Y即可自动安装,直到安装完成
#systemctl start mariadb.service #启动MariaDB
#systemctl stop mariadb.service #停止MariaDB
#systemctl restart mariadb.service #重启MariaDB
#systemctl enable mariadb.service #设置开机启动
#cp /usr/share/mysql/my-huge.cnf /etc/my.cnf #拷贝配置文件(注意:如果/etc目录下面默认有一个my.cnf,直接覆盖即可)
mysql_secure_installation
回车,根据提示输入Y
输入2次密码,回车
根据提示一路输入Y
最后出现:Thanks for using MySQL!
MySql密码设置完成,重新启动 MySQL:
#systemctl restart mariadb.service
允许远程连接 进入mysql
#mysql -uroot -p
>use mysql;
>GRANT ALL ON *.* TO root@'%' IDENTIFIED BY 'MariaDBPassword' WITH GRANT OPTION;
>quit
重启mariadb
#systemctl restart mariadb.service
二、安装nginx
由于centos7没有nginx源,所以首先要配置nginx源:
由于yum源中没有我们想要的nginx,那么我们就需要创建一个“/etc/yum.repos.d/nginx.repo”的文件,其实就是新增一个yum源。
[root@niaoyun~]# vim /etc/yum.repos.d/nginx.repo
然后将下面的内容复制进去:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
然后保存“/etc/yum.repos.d/nginx.repo”文件后,我们就使用yum命令查询一下我们的nginx的yum源配置好了没有。
[root@niaoyun~]# yum list |grep nginx
nginx.x86_64 1:1.10.1-1.el7.ngx nginx
nginx-debug.x86_64 1:1.8.0-1.el7.ngx nginx
nginx-debuginfo.x86_64 1:1.10.1-1.el7.ngx nginx
nginx-module-geoip.x86_64 1:1.10.1-1.el7.ngx nginx
nginx-module-image-filter.x86_64 1:1.10.1-1.el7.ngx nginx
nginx-module-njs.x86_64 1:1.10.1.0.0.20160414.1c50334fbea6-1.el7.ngx
nginx
nginx-module-perl.x86_64 1:1.10.1-1.el7.ngx nginx
nginx-module-xslt.x86_64 1:1.10.1-1.el7.ngx nginx
nginx-nr-agent.noarch 2.0.0-9.el7.ngx nginx
pcp-pmda-nginx.x86_64 3.10.6-2.el7 base
执行安装:
[plain] view plain copy
#yum install nginx
启动
#systemctl start nginx.service
自动启动
#systemctl enable nginx.service
#mkdir /data
#mkdir /data/logs
#mkdir /data/logs/nginx
#chown -R nginx:nginx /data/logs/nginx
配置
#vi /etc/nginx/nginx.conf
error_log /data/logs/nginx/error.log;
events {
worker_connections 1024;
use epoll; //增加此行 如果你使用Linux 2.6+,你应该使用epoll。
}
http {
access_log /data/logs/nginx/access.log main;
三、安装php
[plain] view plain copy
#yum install php php-fpm php-mysql php-gd libjpeg* php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-bcmath php-mhash
启动php-fpm
#systemctl start php-fpm.service
自动启动php-fpm
#systemctl enable php-fpm.service
四、配置PHP、NginX
1、配置PHP
[plain] view plain copy
# vi /etc/php.ini
修改如下内容 (可根据情况修改)
memory_limit = 256M
upload_max_filesize = 256M
post_max_size = 256M
———————————–
保存,
然后
由于php-fpm中session保存目录为:php_value[session.save_path] = /var/lib/php/session
mkdir /var/lib/php/session
chmod -R 777 /var/lib/php/session
2、配置NginX
[php] view plain copy
# nginx虚拟主机配置文件一般都在/etc/nginx/conf.d目录下,每添加一个子域名,就创建一个.conf文件,配置如下
# vi /etc/nginx/conf.d/phplee.com.conf
# phplee.com
server {
listen 80;
server_name www.phplee.com phplee.com;
root /usr/www/phplee.com;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
#以上是基本配置,包含运行php脚本的fastcgi配置,下面是其他功能配置
location @rewrite {
set $static 0;
if ($uri ~ \.(css|js|jpg|jpeg|png|gif|ico|woff|eot|svg|css\.map|min\.map)$) {
set $static 1;
}
if ($static = 0) {
rewrite ^/(.*)$ /index.php?s=/$1;
}
}
location ~ /Uploads/.*\.php$ {
deny all;
}
location ~ /\.ht {
deny all;
}
error_page 404 /404.html;
location = /404.html {
return 404 'Sorry, File not Found!';
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html; # windows dir
}
}
注意: root 项应该配置在server下,这样 php配置项才能正常读取,如果root项配置在location下面,则php配置项$document_root应为真实路径 /usr/www/phplee.com
[php] view plain copy
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
在最新的 nginx 版本中,使用 fastcgi.conf 代替 fastcgi.params ,因为在 fastcgi.conf 中多了一个 fastcgi_param 配置:
[plain] view plain copy
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
所以在
location ~ \.php$ {
…
}
中不再需要配置该参数。
五、防火墙配置
CentOS 7 网络防火墙由 iptables 变为 firewalld,操作方法如下:
[plain] view plain copy
# 查看 firewalld 当前激活区块信息
[root@localhost test1]# firewall-cmd –get-active-zones
public
interfaces: enp0s9 enp0s10
# 查看 public 区块所有的规则,这里有2个services,0个ports规则
[root@localhost test1]# firewall-cmd –zone=public –list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: enp0s9 enp0s10
sources:
services: ssh dhcpv6-client
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
# 添加 80 和3306 端口的永久开启规则
[root@localhost test1]# firewall-cmd –zone=public –add-port=80/tcp –permanent
success
[root@localhost test1]# firewall-cmd –zone=public –add-port=3306/tcp –permanent
success
# 重新加载所有规则
[root@localhost test1]# firewall-cmd –reload
success
# 再次查看,发现刚才添加的规则已生效
[root@localhost test1]# firewall-cmd –zone=public –list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: enp0s9 enp0s10
sources:
services: ssh dhcpv6-client
ports: 80/tcp 3306/tcp
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
访问nginx站点,正常显示~!
以上就是关于“CentOS7安装LNMP环境 如何使用yum工具分开安装LNMP?”的相关知识,感谢各位的阅读,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会。