Linux -- Centos 下配置LNAMP 服务器环境

时间:2023-03-09 15:50:31
Linux  --  Centos 下配置LNAMP 服务器环境

1.Mysql

centos 7 下mysql被替换掉,如有需要请看另一篇:

centos 6.5下:

yum install mysql mysql-server mysql-devel

启动mysql :

centos 6.5:

/etc/init.d/mysqld start

开机启动:

centos 6.5:

chkconfig mysqld on

2.安装php

yum install php php-mysql php-common php-gd php-mbstring php-mcrypt php-devel php-xml php-fpm php-cli php-ldap php-odbc php-pdo php-pecl-memcache php-pear php-mbstring php-xml php-xmlrpc php-snmp php-soap

启动fpm:

centos 6.5:

service php-fpm start

centos 7:

systemctl enable php-fpm.service
systemctl start php-fpm.service

3.安装apache(可选)

yum install httpd httpd-devel

启动和开机启动:

/etc/init.d/httpd start
chkconfig httpd on

这里需要更改apache的默认端口,使其不为80,避免与nginx冲突,如有需要参见apache配置文件修改

4.Nginx安装

Nginx不是从官方CentOS库安装,我们从 nginx 项目安装库安装,修改源:

vi /etc/yum.repos.d/nginx.repo
修改为:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=
enabled=
yum install nginx

启动和开机启动:

centos 7:

systemctl enable nginx.service
systemctl start nginx.service

centos 6.5:

service nginx start

5.配置nginx

现在我们打开配置文件 

/etc/nginx/nginx.conf

这里没有需要不需更改,如有需要参见nginx详细配置
vi /etc/nginx/conf.d/default.conf
server {
listen ;
server_name localhost; #charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main; location / {
root /usr/share/nginx/html;
index index.html index.htm;
} #error_page /.html; # redirect server error pages to the static page /50x.html
#
error_page /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
} # proxy the PHP scripts to Apache listening on 127.0.0.1:
#
#location ~ .php$ {
# proxy_pass http://127.0.0.1;
#} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:
# location ~ .php$ {
root /usr/share/nginx/html;
#try_files $uri =;
fastcgi_pass 127.0.0.1:;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
} # deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /.ht {
deny all;
}
}

现在保存文件并重新加载nginx:

systemctl restart nginx.service

ystemctl reload nginx.service

6.打开防火墙端口:

注意:有时安装好可能无法访问,很大的原因是防火墙,需要打开80端口

通过

/etc/init.d/iptables status

命令查询是否有打开80端口,如果没有可通过两种方式处理:

1.修改vi /etc/sysconfig/iptables命令添加使防火墙开放80端口

-A RH-Firewall--INPUT -m state --state NEW -m tcp -p tcp --dport  -j ACCEPT

2.关闭防火墙
/etc/init.d/iptables stop 
#start 开启 
#restart 重启
永久性关闭防火墙chkconfig --level 35 iptables off

7.测试php

现在创建的文档根目录下的PHP探针文件 /usr/share/nginx/html

vi /usr/share/nginx/html/info.php
<?php
phpinfo();
?>

测试输入ip,根据nginx的log来调试。

8.使用php-fpm套接字来连接nginx(可选)

默认情况下监听端口 9000 。 另外,也可以使PHP-FPM使用Unix套接字,这避免了TCP的开销。要做到这一点,打开 /etc/php-fpm.d/www.conf…

vi /etc/php-fpm.d/www.conf

… 修改后如下:

[...]
;listen = 127.0.0.1:
listen = /var/run/php-fpm/php5-fpm.sock
[...]

然后重新加载 PHP-FPM:

systemctl restart php-fpm.service

接下来通过你的nginx的配置和所有的虚拟主机和改线 fastcgi_pass 127.0.0.1:9000; to fastcgi_pass unix:/tmp/php5-fpm.sock;,像这样:

vi /etc/nginx/conf.d/default.conf

[...]
location ~ .php$ {
root /usr/share/nginx/html;
try_files $uri =;
fastcgi_pass unix:/var/run/php-fpm/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
[...]

最后重新加载 nginx:

systemctl restart nginx.service