CentOS 6 yum安装配置lnmp服务器
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CentOS 6 yum安装配置lnmp服务器相关的知识,希望对你有一定的参考价值。
一、准备:
1.配置防火墙,开启80端口、3306端口
编辑 /etc/sysconfig/iptables,添加如下条目:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT(允许80端口通过防火墙) -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT(允许3306端口通过防火墙)
添加好的规则如下:
[[email protected] ~]# vim /etc/sysconfig/iptables # Firewall configuration written by system-config-firewall # Manual customization of this file is not recommended. *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT -A INPUT -p icmp -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT -A INPUT -j REJECT --reject-with icmp-host-prohibited -A FORWARD -j REJECT --reject-with icmp-host-prohibited COMMIT
#最后重启防火墙使配置生效
[[email protected] ~]#/etc/init.d/iptables restart
或者
[[email protected] ~]# service iptables restart
2.2、关闭SELINUX
vi /etc/selinux/config #SELINUX=enforcing #注释掉 #SELINUXTYPE=targeted #注释掉 SELINUX=disabled #增加 :wq 保存,关闭 shutdown -r now #重启系统
3、配置CentOS 第三方yum源(CentOS默认的标准源里没有nginx软件包)
[[email protected] ~]yum install wget #安装下载工具wget [[email protected] ~]# wget http://www.atomicorp.com/installers/atomic #下载atomic yum源 [[email protected] ~]# sh ./atomic #安装 [[email protected] ~]yum check-update #更新yum软件包
二.安装篇
1、安装nginx
[[email protected] ~]# yum install nginx -y #安装nginx,根据提示,输入Y安装即可成功安装 [[email protected] ~]# service nginx start #启动 Starting nginx: [ OK ]
[[email protected] ~]# chkconfig nginx on #设为开机启动
[[email protected] ~]#rm -rf /usr/share/nginx/html/* #删除ngin默认测试页
2、安装mysql
[[email protected] ~]# yum install mysql mysql-server #询问是否要安装,输入Y即可自动安装,直到安装完成 [[email protected] ~]# /etc/init.d/mysqld start #启动MySQL [[email protected] ~]# chkconfig mysqld on #设为开机启动
[[email protected] ~]# mv /etc/my.cnf{,.bak}
[[email protected] ~]# cp /usr/share/mysql/my-medium.cnf /etc/my.cnf #拷贝配置文件
2.2、为root账户设置密码
[[email protected] ~]# mysql_secure_installation #初始化MySQL
回车,根据提示输入Y
Enter current password for root (enter for none): <---输入现在的root密码,因为我们还没设置,直接回车
Set root password? [Y/n] Y <---是否设定root密码,当然设置了,输入Y回车
New password: <---输入root密码,并回车,输入的过程中不会有任何显示
Re-enter new password: <---再次输入root密码,并回车,输入的过程中不会有任何显示
Remove anonymous users? [Y/n] Y <---是否删除匿名用户,删除,输入Y回车
Disallow root login remotely? [Y/n] Y <---是否删禁止root用户远程登录,当然禁止,输入Y回车
Remove test database and access to it? [Y/n] <---是否删除测试数据库test,看个人喜好
Reload privilege tables now? [Y/n] Y <---刷新权限,输入Y回车
最后出现:Thanks for using MySQL!
MySql密码设置完成,重新启动 MySQL:
[[email protected] ~]# /etc/init.d/mysqld start
其他操作:
[[email protected] ~]# /etc/init.d/mysqld stop #停止 [[email protected] ~]# service mysqld restart #重启
3、安装php
3.1、安装PHP
[[email protected] ~]# yum install php #根据提示输入Y直到安装完成
3.2、安装PHP组件,使PHP支持 MySQL、PHP支持FastCGI模式
[[email protected] ~]# yum install php-mysql php-gd libjpeg* php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-bcmath php-mhash libmcrypt libmcrypt-devel php-fpm -y
[[email protected] ~]#/etc/init.d/mysqld restart #重启MySql
[[email protected] ~]#/etc/init.d/nginx restart #重启nginx
[[email protected] ~]#/etc/rc.d/init.d/php-fpm start #启动php-fpm
[[email protected] ~]#chkconfig php-fpm on #设置开机启动
三.配置篇
1、配置nginx支持php
[[email protected] ~]# cp /etc/nginx/nginx.conf{,.bak}
[[email protected] ~]# vim /etc/nginx/nginx.conf #编辑/etc/nginx/nginx.conf
user nginx nginx; #修改nginx运行账号为:nginx组的nginx用户
[[email protected] ~]# cp /etc/nginx/conf.d/default.conf{,.bak} #备份原有配置文件
[[email protected] ~]# vim /etc/nginx/conf.d/default.conf #编辑/etc/nginx/conf.d/default.conf
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm; #增加index.php
}
# pass the PHPscripts to FastCGI server listening on 127.0.0.1:9000
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;
}
#取消FastCGI server部分location的注释,并要注意fastcgi_param行的参数,改为$document_root$fastcgi_script_name,或者使用绝对路径
2、配置php
[[email protected] ~]# cp /etc/php.ini{,.bak}
date.timezone= PRC
expose_php = Off
3、配置php-fpm
[[email protected] ~]# cp /etc/php-fpm.d/www.conf{,.bak} [[email protected] ~]# vim /etc/php-fpm.d/www.conf
user = nginx #修改用户为nginx
group = nginx #修改组为nginx
[[email protected] ~]# /etc/init.d/mysqld restart
[[email protected] ~]# /etc/init.d/nginx restart
[[email protected] ~]# /etc/rc.d/init.d/php-fpm restart
四.测试篇
[[email protected] ~]# cd /usr/share/nginx/html/ [[email protected] html]# vim index.php <?php phpinfo(); ?>
[[email protected] html]# chown nginx.nginx /usr/share/nginx/html/ -R #设置目录所有者
[[email protected] html]# chmod 700 /usr/share/nginx/html/ -R #设置目录权限
在客户端浏览器输入服务器IP地址,可以看到相关的配置信息!
备注
nginx默认站点目录是:/usr/share/nginx/html/ 权限设置:chown nginx.nginx/usr/share/nginx/html/ -R MySQL数据库目录是:/var/lib/mysql 权限设置:chown mysql.mysql -R /var/lib/mysql
以上是关于CentOS 6 yum安装配置lnmp服务器的主要内容,如果未能解决你的问题,请参考以下文章
centos7编译安装LNMP(nginx-1.16.0,mysql8.0.16,php-7.3.6)常见问题报错及解决方法