欢迎光临小鱼网个人网站!

登录/注册
CentOS 7安装 LNMP(Nginx+PHP+MySQL)

1、编辑vi /etc/yum.repos.d/nginx.repo

修改为:
 [nginx]
 name=nginx repo
 baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
 gpgcheck=0
 enabled=1

2、安装mysql

yum install mariadb mariadb-server net-tools

启动MySQL服务器:

systemctl enable mariadb.service

systemctl start mariadb.service

检查网络启用。运行

netstat -tap | grep mysql

显示出这样的内容:

[root@xy ~]# netstat -tap | grep mysql

tcp 0 0 0.0.0.0:mysql 0.0.0.0:* LISTEN 10623/mysqld

为用户设置根口令:

[root@xy ~]# mysql_secure_installation

/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB

SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we’ll need the current

password for the root user. If you’ve just installed MariaDB, and

you haven’t set the root password yet, the password will be blank,

so you should just press enter here.

Enter current password for root (enter for none):

OK, successfully used password, moving on…

Setting the root password ensures that nobody can log into the MariaDB

root user without the proper authorisation.

Set root password? [Y/n] <– 回车

New password: <– 输入ROOT密码

Re-enter new password: <– 再输入一次ROOT密码

Password updated successfully!

Reloading privilege tables..

… Success!

By default, a MariaDB installation has an anonymous user, allowing anyone

to log into MariaDB without having to have a user account created for

them. This is intended only for testing, and to make the installation

go a bit smoother. You should remove them before moving into a

production environment.

Remove anonymous users? [Y/n] <– 回车

… Success!

Normally, root should only be allowed to connect from ‘localhost’. This

ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] <– 回车

… Success!

By default, MariaDB comes with a database named ‘test’ that anyone can

access. This is also intended only for testing, and should be removed

before moving into a production environment.

Remove test database and access to it? [Y/n] <– 回车

– Dropping test database…

… Success!

– Removing privileges on test database…

… Success!

Reloading the privilege tables will ensure that all changes made so far

will take effect immediately.

Reload privilege tables now? [Y/n] <– 回车

… Success!

Cleaning up…

All done! If you’ve completed all of the above steps, your MariaDB

installation should now be secure.

Thanks for using MariaDB!

[root@xy ~]# mysql_secure_installation

3、安装nginx

yum install nginx

然后我们创建的系统启动nginx的链接和启动它:

systemctl enable nginx.service

systemctl start nginx.service

有时,你会得到一个错误,如80端口已在使用中,错误消息会是这样的

[root@xy ~]# service nginx start

Starting nginx: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

nginx: [emerg] still could not bind()

[FAILED]

[root@xy ~]#

这就意味着有时在运行Apache服务。停止服务,进一步启动服务nginx如下

systemctl stop httpd.service

yum remove httpd

systemctl disable httpd.service

systemctl enable nginx.service

systemctl start nginx.service

开放的HTTP和HTTPS防火墙中的端口

firewall-cmd –permanent –zone=public –add-service=http

firewall-cmd –permanent –zone=public –add-service=https

firewall-cmd –reload

输出的shell结果将看起来像这样:

[root@xy ~]# firewall-cmd –permanent –zone=public –add-service=http

success

[root@xy ~]# firewall-cmd –permanent –zone=public –add-service=https

success

[root@xy ~]# firewall-cmd –reload

success

[root@xy ~]#

4、安装php

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

yum install php-devel

yum groupinstall ‘Development Tools’

5、安装apc

pecl install apc

[root@xy ~]# pecl install apc

downloading APC-3.1.13.tgz …

Starting to download APC-3.1.13.tgz (171,591 bytes)

……………..done: 171,591 bytes

55 source files, building

running: phpize

Configuring for:

PHP Api Version: 20100412

Zend Module Api No: 20100525

Zend Extension Api No: 220100525

Enable internal debugging in APC [no] : <– 回车

Enable per request file info about files used from the APC cache [no] : <– 回车

Enable spin locks (EXPERIMENTAL) [no] : <– 回车

Enable memory protection (EXPERIMENTAL) [no] : <– 回车

Enable pthread mutexes (default) [no] : <–回车

Enable pthread read/write locks (EXPERIMENTAL) [yes] : <– 回车

building in /var/tmp/pear-build-rootVrjsuq/APC-3.1.13

6、编辑 /etc/php.ini 并设置 cgi.fix_pathinfo=0:

[...]
; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI. PHP's
; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
; what PATH_INFO is. For more information on PATH_INFO, see the cgi specs. Setting
; this to 1 will cause PHP CGI to fix its paths to conform to the spec. A setting
; of zero causes PHP to behave as before. Default is 1. You should fix your scripts
; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://www.php.net/manual/en/ini.core.php#ini.cgi.fix-pathinfo
cgi.fix_pathinfo=0
[...]

添加

[...]
extension=apc.so

systemctl enable php-fpm.service

systemctl start php-fpm.service

7、配置nginx

vi /etc/nginx/nginx.conf

[...]
worker_processes 4;
[...]
 keepalive_timeout 2;
[...]

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

[...]
server {
 listen 80;
 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 404 /404.html;
 # redirect server error pages to the static page /50x.html
 #
 error_page 500 502 503 504 /50x.html;
 location = /50x.html {
 root /usr/share/nginx/html;
 }
 # proxy the PHP scripts to Apache listening on 127.0.0.1:80
 #
 #location ~ .php$ {
 # proxy_pass http://127.0.0.1;
 #}
 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 #
 location ~ .php$ {
 root /usr/share/nginx/html;
 try_files $uri =404;
 fastcgi_pass 127.0.0.1:9000;
 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;
 }
}

systemctl restart nginx.service

编辑vi /etc/php-fpm.d/www.conf修改如下

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

systemctl restart php-fpm.service

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

[...]
 location ~ .php$ {
 root /usr/share/nginx/html;
 try_files $uri =404;
 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;
 }
[...]

systemctl restart nginx.service

Copyright © www.xyubing.com All Rights Reserved. 备案号:蜀ICP备19023461号-1

免责声明:本站部分资源搜集整理于互联网或者网友提供,仅供学习与交流使用,如果不小心侵犯到你的权益,请及时联系我们删除该资源。