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

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 RDS PostgreSQL,集群系列 2核4GB
简介:

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,如需转载请自行联系原作者

相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
17天前
|
存储 SQL 关系型数据库
服务器数据恢复—云服务器上mysql数据库数据恢复案例
某ECS网站服务器,linux操作系统+mysql数据库。mysql数据库采用innodb作为默认存储引擎。 在执行数据库版本更新测试时,操作人员误误将在本来应该在测试库执行的sql脚本在生产库上执行,导致生产库上部分表被truncate,还有部分表中少量数据被delete。
55 25
|
3天前
|
监控 关系型数据库 MySQL
如何解决 MySQL 数据库服务器 CPU 飙升的情况
大家好,我是 V 哥。当 MySQL 数据库服务器 CPU 飙升时,如何快速定位和解决问题至关重要。本文整理了一套实用的排查和优化套路,包括使用系统监控工具、分析慢查询日志、优化 SQL 查询、调整 MySQL 配置参数、优化数据库架构及检查硬件资源等步骤。通过一个电商业务系统的案例,详细展示了从问题发现到解决的全过程,帮助你有效降低 CPU 使用率,提升系统性能。关注 V 哥,掌握更多技术干货。
|
2月前
|
SQL 存储 关系型数据库
MySQL/SqlServer跨服务器增删改查(CRUD)的一种方法
通过上述方法,MySQL和SQL Server均能够实现跨服务器的增删改查操作。MySQL通过联邦存储引擎提供了直接的跨服务器表访问,而SQL Server通过链接服务器和分布式查询实现了灵活的跨服务器数据操作。这些技术为分布式数据库管理提供了强大的支持,能够满足复杂的数据操作需求。
118 12
|
3月前
|
安全 PHP 开发者
php中配置variables_order详解
`variables_order` 是 PHP 配置中的一个关键指令,它决定了不同来源的变量被导入到全局变量空间的顺序。正确配置 `variables_order` 不仅可以确保变量的正确处理和覆盖顺序,还能提高应用程序的安全性。开发者应根据具体应用的需求,合理配置 `variables_order`,确保应用的稳定和安全运行。
59 5
|
3月前
|
监控 PHP Apache
优化 PHP-FPM 参数配置:实现服务器性能提升
优化PHP-FPM的参数配置可以显著提高服务器的性能和稳定性。通过合理设置 `pm.max_children`、`pm.start_servers`、`pm.min_spare_servers`、`pm.max_spare_servers`和 `pm.max_requests`等参数,并结合监控和调优措施,可以有效应对高并发和负载波动,确保Web应用程序的高效运行。希望本文提供的优化建议和配置示例能够帮助您实现服务器性能的提升。
161 3
|
5月前
|
关系型数据库 MySQL PHP
php wampserver的使用配置
本文介绍了WampServer在Windows系统下的配置和使用方法,包括如何修改PHP时区为中国标准时区PRC、更改Apache服务器端口号以避免冲突、设置起始页以及如何创建和管理虚拟目录。通过这些步骤,用户可以更有效地在本地环境中开发和测试PHP程序。
php wampserver的使用配置
|
4月前
|
SQL 分布式计算 关系型数据库
Hadoop-21 Sqoop 数据迁移工具 简介与环境配置 云服务器 ETL工具 MySQL与Hive数据互相迁移 导入导出
Hadoop-21 Sqoop 数据迁移工具 简介与环境配置 云服务器 ETL工具 MySQL与Hive数据互相迁移 导入导出
150 3
|
4月前
|
缓存 NoSQL 数据处理
原生php实现redis缓存配置和使用方法
通过上述步骤,你可以在PHP项目中配置并使用Redis作为高性能的缓存解决方案。合理利用Redis的各种数据结构和特性,可以有效提升应用的响应速度和数据处理效率。记得在实际应用中根据具体需求选择合适的缓存策略,如设置合理的过期时间,以避免内存过度消耗。
109 0
|
弹性计算 Shell Windows
阿里云学生专享ecs服务器体验及个人配置
阿里云学生优惠专享的云服务器个人使用体验
阿里云学生专享ecs服务器体验及个人配置
|
14天前
|
存储 机器学习/深度学习 人工智能
2025年阿里云GPU服务器租用价格、选型策略与应用场景详解
随着AI与高性能计算需求的增长,阿里云提供了多种GPU实例,如NVIDIA V100、A10、T4等,适配不同场景。2025年重点实例中,V100实例GN6v单月3830元起,适合大规模训练;A10实例GN7i单月3213.99元起,适用于混合负载。计费模式有按量付费和包年包月,后者成本更低。针对AI训练、图形渲染及轻量级推理等场景,推荐不同配置以优化成本和性能。阿里云还提供抢占式实例、ESSD云盘等资源优化策略,支持eRDMA网络加速和倚天ARM架构,助力企业在2025年实现智能计算的效率与成本最优平衡。 (该简介为原文内容的高度概括,符合要求的字符限制。)