LNMP服务器安装配置(Rhel+Nginx+PHP+MySQL)

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介:

1、关闭selinux、配置防火墙,开启80、3306端口

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@localhost ~] # cp /etc/sysconfig/iptables /etc/sysconfig/iptablesbak
[root@localhost ~] # vim /etc/sysconfig/iptables
-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
[root@localhost ~] # /etc/init.d/iptables restart
[root@localhost ~] # cp /etc/sysconfig/selinux /etc/sysconfig/selinuxbak
[root@localhost ~] # cat /etc/sysconfig/selinux|grep -v "#"
SELINUX=disabled
SELINUXTYPE=targeted 
[root@localhost ~] #

2、安装nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@localhost ~] # rpm -e --nodeps `rpm -qa|egrep -i "httpd|php"`  #删除系统自带的软件包
[root@localhost ~] # wget   #下载、安装第三方yum源
[root@localhost ~] # sh ./atomic   #安装
Do you agree to these terms? ( yes /no ) [Default:  yes yes
Configuring the [atomic] yum archive  for  this system 
Installing the Atomic GPG keys: OK
OK
Enable repo by default? ( yes /no ) [Default:  yes ]: 
The Atomic Rocket Turtle archive has now been installed and configured  for  your system
The following channels are available:
[root@localhost ~] # yum check-update   #更新yum源
[root@localhost ~] # yum -y install nginx 
[root@localhost ~] # service nginx start
Starting nginx:                                            [  OK  ]
[root@localhost ~] #

3、安装MySQL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[root@localhost ~] # yum install mysql mysql-server -y
[root@localhost ~] # chkconfig mysqld on
[root@localhost ~] # /etc/init.d/mysqld start
[root@localhost ~] # cp /usr/share/mysql/my-medium.cnf /etc/my.cnf 
cp : overwrite ` /etc/my .cnf'? y
[root@localhost ~] # mysql_secure_installation   #为root账户设置密码
Enter current password  for  root (enter  for  none): 
OK, successfully used password, moving on...
Set root password? [Y /n ] Y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
  ... Success!
  Remove anonymous  users ? [Y /n ] Y  
  ... Success!
  Disallow root login remotely? [Y /n ] Y
  ... Success!
  Remove  test  database and access to it? [Y /n ] Y
  - Dropping  test  database...
  ... Success!
  - Removing privileges on  test  database...
  ... Success!
  Reload privilege tables now? [Y /n ] Y
  ... Success!
  Thanks  for  using MySQL!
  [root@localhost ~] # service mysqld restart

4、安装PHP5

1
2
3
4
5
6
[root@localhost ~] # yum install php php-fpm -y
[root@localhost ~] # 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 -y  #安装PHP组件,使 PHP5 支持 MySQL
[root@localhost ~] # chkconfig php-fpm on
[root@localhost ~] # /etc/init.d/php-fpm start
Starting php-fpm:                                          [  OK  ]
[root@localhost ~] #

5、配置nginx支持php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@localhost ~] # cp /etc/nginx/nginx.conf /etc/nginx/nginx.confbak
[root@localhost ~] # vim /etc/nginx/nginx.conf
user              nginx nginx;     #修改nginx运行账号为:nginx组的nginx用户
worker_processes  1;
[root@localhost ~] # cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.confbak
[root@localhost ~] # vim /etc/nginx/conf.d/default.conf
     location / {
         root    /usr/share/nginx/html ;
         index  index.php index.html index.htm;    #增加index.php
         # example
         #ModSecurityEnabled on;
         #ModSecurityConfig /etc/nginx/modsecurity.conf;
     }
      location ~ \.php$ {     #取location的注释,并将fastcgi_param行的参数,改为$document_root$fastcgi_script_name,或者使用绝对路径
         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;
     }   
  [root@localhost ~] # service nginx restart
Stopping nginx:                                            [  OK  ]
Starting nginx:                                            [  OK  ]
[root@localhost ~] #

6、php配置

1
2
3
4
5
6
7
8
[root@localhost ~] # cp /etc/php.ini /etc/php.inibak
[root@localhost ~] # vim /etc/php.ini
  878  date .timezone = PRC
  314 disable_functions = passthru, exec ,system,chroot,scandir, chgrp , chown ,shell_exec,proc_open,proc_get_status, in      i_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink, symlink ,popepassthru,stream_socket_server,escapesh     ellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_c     termid,posix_get_last_error,posix_getcwd, posix_getegid,posix_geteuid,posix_getgid, posix_getgrgid,posix_ge     tgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid, posix_getppid,posix_getpwna     m,posix_getpwuid, posix_getrlimit, posix_getsid,posix_getuid,posix_isatty, posix_kill,posix_mkfifo,posix_se     tegid,posix_seteuid,posix_setgid, posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_     ttyname,posix_uname    #PHP禁用的函数,如果某些程序需要用到这个函数,可以删除,取消禁用。
   375 expose_php = Off     #禁止显示php版本的信息
   211 short_open_tag = ON    #支持php短标签
   308 open_basedir = .: /tmp/   #设置表示允许访问当前目录(即PHP脚本文件所在之目录)和/tmp/目录,可以防止php木马跨站,如果改了之后安装程序有问题(例如:nginx可能网站根目录下的文件会提示Access Denied),可以注销此行,或者直接写上程序的目录open_basedir = /usr/share/nginx/html/:/tmp/
   [root@localhost ~] #

7、配置php-fpm

1
2
3
4
5
6
[root@localhost ~] # cp /etc/php-fpm.d/www.conf /etc/php-fpm.d/www.confbak
[root@localhost ~] # vim /etc/php-fpm.d/www.conf
user = nginx
; RPM: Keep a group allowed to write  in  log  dir .
group = nginx
[root@localhost ~] #

8、测试

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@localhost ~] # cd /usr/share/nginx/html/
[root@localhost html] # vim index.php
<?php
phpinfo();
?>
~   
[root@localhost html] # service nginx restart
Stopping nginx:                                            [  OK  ]
Starting nginx:                                            [  OK  ]
[root@localhost html] # service php-fpm restart
Stopping php-fpm:                                          [  OK  ]
Starting php-fpm:                                          [  OK  ]
[root@localhost html] #

在客户端浏览器输入服务器IP地址,可以看到相关的配置信息(无法查看可以将php.ini中expose_php设置为on、open_basedir注释掉,或者直接写上程序的目录)! 说明lnmp配置成功!



本文转自 justin_peng 51CTO博客,原文链接:http://blog.51cto.com/ityunwei2017/1733287,如需转载请自行联系原作者

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
5天前
|
存储 Ubuntu 关系型数据库
Ubuntu 20.04 卸载与安装 MySQL 5.7 详细教程
该文档提供了在Ubuntu上卸载和安装MySQL 5.7的步骤。首先,通过`apt`命令卸载所有MySQL相关软件包及配置。然后,下载特定版本(5.7.32)的MySQL安装包,解压并安装所需依赖。接着,按照特定顺序安装解压后的deb包,并在安装过程中设置root用户的密码。安装完成后,启动MySQL服务,连接数据库并验证。最后,提到了开启GTID和二进制日志的配置方法。
|
11天前
|
关系型数据库 MySQL 数据安全/隐私保护
MySQL安装
MySQL安装
34 1
|
5天前
|
关系型数据库 MySQL Linux
【MySQL】如何在Linux上安装MySQL
【MySQL】如何在Linux上安装MySQL
41 1
|
11天前
|
关系型数据库 MySQL Linux
在Linux系统中安装MySQL数据库
在Linux系统中安装MySQL数据库
63 3
|
11天前
|
Ubuntu 关系型数据库 MySQL
|
14天前
|
安全 关系型数据库 MySQL
解决centos7.0安装mysql后出现access defind for user@'localhost'的错误
在使用yum 安装完mariadb, mariadb-server, mariadb-devel后
19 0
|
17天前
|
应用服务中间件 PHP nginx
安装基于docker的php运行环境
安装基于docker的php运行环境
22 0
|
17天前
|
关系型数据库 MySQL Linux
Linux下安装MySQL
Linux下安装MySQL
32 0
|
17天前
|
SQL 分布式计算 关系型数据库
Ubuntu上安装MySQL以及hive
Ubuntu上安装MySQL以及hive
|
17天前
|
关系型数据库 MySQL 数据库
mysqlTools 一分钟部署安装本mysql多个版本,解锁繁琐部署过程
mysqlTools 一分钟部署安装本mysql多个版本,解锁繁琐部署过程
314 2

推荐镜像

更多