CentOS7安装LNMP环境

LNMP环境:linux(centos7)+ngix(1.12.2)+mariadb(5.5.56)+php(5.4.16)

一、安装MariaDB

[plain]  view plain  copy
  1. #yum install mariadb mariadb-server #询问是否要安装,输入Y即可自动安装,直到安装完成  
  2.    
  3. #systemctl start mariadb.service #启动MariaDB  
  4.    
  5. #systemctl stop mariadb.service #停止MariaDB  
  6.    
  7. #systemctl restart mariadb.service #重启MariaDB  
  8.    
  9. #systemctl enable mariadb.service #设置开机启动  
  10.    
  11. #cp /usr/share/mysql/my-huge.cnf /etc/my.cnf #拷贝配置文件(注意:如果/etc目录下面默认有一个my.cnf,直接覆盖即可)  
  12.    
  13. mysql_secure_installation  
  14.    
  15. 回车,根据提示输入Y  
  16.    
  17. 输入2次密码,回车  
  18.    
  19. 根据提示一路输入Y  
  20.    
  21. 最后出现:Thanks for using MySQL!  
  22.    
  23. MySql密码设置完成,重新启动 MySQL:  
  24. #systemctl restart mariadb.service   
  25.   
  26. 允许远程连接 进入mysql    
  27. #mysql -uroot -p  
  28. >use mysql;  
  29. >GRANT ALL ON *.* TO root@'%' IDENTIFIED BY 'MariaDBPassword' WITH GRANT OPTION;  
  30. >quit  
  31. 重启mariadb    
  32. #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
  1. #yum install nginx  
  2.    
  3. 启动    
  4. #systemctl start nginx.service  
  5.    
  6. 自动启动  
  7. #systemctl enable nginx.service  
  8.    
  9. #mkdir /data  
  10. #mkdir /data/logs  
  11. #mkdir /data/logs/nginx  
  12. #chown -R nginx:nginx /data/logs/nginx  
  13.    
  14. 配置  
  15. #vi /etc/nginx/nginx.conf  
  16.    
  17. error_log /data/logs/nginx/error.log;  
  18.    
  19. events {  
  20.     worker_connections 1024;  
  21.     use epoll;  //增加此行 如果你使用Linux 2.6+,你应该使用epoll。  
  22. }  
  23.   
  24. http {  
  25.      access_log  /data/logs/nginx/access.log  main;  
三、安装php

[plain]  view plain  copy
  1. #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    
  2. 启动php-fpm  
  3. #systemctl start php-fpm.service  
  4. 自动启动php-fpm  
  5. #systemctl enable php-fpm.service 
四、配置PHP、NginX
1、配置PHP

[plain]  view plain  copy
  1. # vi /etc/php.ini  
  2.    
  3. 修改如下内容 (可根据情况修改)     
  4.    
  5. memory_limit = 256M    
  6. upload_max_filesize = 256M  
  7. post_max_size = 256M  
  8. ----------------------------------- 
  9.    
  10. 保存,  
  11.    
  12. 然后  
  13. 由于php-fpm中session保存目录为:php_value[session.save_path] = /var/lib/php/session
  14. mkdir /var/lib/php/session   
  15. chmod -R 777 /var/lib/php/session  
  16.  

2、配置NginX 

# 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

fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; 

在最新的 nginx 版本中,使用 fastcgi.conf 代替 fastcgi.params ,因为在 fastcgi.conf 中多了一个 fastcgi_param  配置:

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
所以在

location ~ \.php$ {
...
}
中不再需要配置该参数。

五、防火墙配置

CentOS 7 网络防火墙由 iptables 变为 firewalld,操作方法如下:

# 查看 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站点,正常显示~!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值