linux作为一种开源系统,受到众多开发者的青睐,其中的nginx服务器在web服务器领域中占据着重要的地位。加上php模块的支持,可以在linux服务器上运行php网站和应用程序。本文将向您介绍如何在linux系统上安装nginx和php模块。
一、安装nginx
添加nginx源打开终端,输入以下命令添加nginx的包管理源:
cd /etc/yum.repos.d/touch nginx.repovim nginx.repo
在vim中,复制以下内容:
[nginx]name=nginx repobaseurl=http://nginx.org/packages/centos/$releasever/$basearch/gpgcheck=0enabled=1
*注意:如果您不是在centos系统中安装nginx,需要到nginx官网查找安装指南。
安装nginx更新yum:
yum update
安装nginx:
yum install nginx
启动nginx:
systemctl start nginx
检查nginx状态:
systemctl status nginx
如输出:
nginx.service - the nginx http and reverse proxy server loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled) active: active (running) since sat 2018-06-09 00:02:05 cst; 3s ago process: 5961 execstart=/usr/sbin/nginx (code=exited, status=0/success) process: 5958 execstartpre=/usr/sbin/nginx -t (code=exited, status=0/success) process: 5957 execstartpre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/success) main pid: 5963 (nginx) cgroup: /system.slice/nginx.service ├─5963 nginx: master process /usr/sbin/nginx └─5964 nginx: worker process
这表明nginx已经成功安装并正在运行。
网页访问测试导航到nginx默认页面:
http://服务器ip/
如果看到“welcome to nginx!”的字样,那么您已经成功安装nginx并启用它。
二、配置php
安装php在安装任何php模块之前,必须先安装php。执行以下命令安装:
yum install php
安装php扩展执行以下命令安装必备的php扩展:
yum install php-mysql php-fpm php-gd
配置php-fpm打开php-fpm配置文件:
vim /etc/php-fpm.d/www.conf
添加或修改以下选项:
user = nginxgroup = nginxlisten = /var/run/php-fpm/php-fpm.socklisten.owner = nginxlisten.group = nginx
重启php-fpm执行以下命令以便更新配置:
systemctl restart php-fpm
三、配置nginx
配置nginx支持php打开nginx默认配置文件:
vim /etc/nginx/conf.d/default.conf
查找以下选项:
location / { root /usr/share/nginx/html; index index.html index.htm;}
在“location /”块中添加以下内容:
location ~ \.php$ { root /usr/share/nginx/html; fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_index index.php; fastcgi_param script_filename $document_root$fastcgi_script_name; include fastcgi_params;}
*注意:如果你想更改默认的nginx网站根目录,请改变上述”root”指令的路径。
重新载入nginx配置文件执行以下命令以便更新nginx配置文件:
nginx -s reload
四、测试php环境
创建php测试页面在nginx的默认网站根目录中创建一个名为index.php的文件:
cd /usr/share/nginx/htmlvim index.php
复制以下内容:
<?phpphpinfo();?>
访问php测试页面导航到刚刚创建的php测试页面:
http://服务器ip/index.php
如果能够看到php的配置信息,那么您已经成功配置了nginx和php环境。
五、总结
通过此文,我们学习了如何在linux系统上安装nginx并加载php模块,以便运行php网站和应用程序。这将会极大地方便您的开发工作,并提高服务器性能。
以上就是如何在linux系统上安装nginx和php模块的详细内容。
