Nginx是一款小巧而高效的Web服务器软件,可帮您在Linux系统下快速方便地搭建出LNMP Web服务环境。本教程介绍如何手动在Centos系统上搭建LNMP环境,其中LNMP分别代表Linux、Nginx、MySQL和PHP。
一、准备编译环境
1、临时关闭防火墙
systemctl stop firewalld
2、永久关闭防火墙开机自启
systemctl disable firewalld
3、临时关闭SELinux
setenforce 0
4、永久关闭SELinux
sed -i 's/enforcing/disabled/' /etc/selinux/config
二、安装nginx
1、添加nginx官方源
vim /etc/yum.repos.d/nginx.repo [nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true [nginx-mainline] name=nginx mainline repo baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/ gpgcheck=1 enabled=0 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true
2、运行以下命令安装nginx
yum install nginx -y
3、运行以下命令查看nginx版本
nginx -v
三、安装MySQL
1、运行以下命令更新YUM源
rpm -Uvh http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
2、运行以下命令安装MySQL
yum -y install mysql-community-server
3、运行以下命令查看MySQL版本号
mysql -V
4、运行以下命令启动MySQL
systemctl start mysqld
5、运行以下命令设置开机启动MySQL
systemctl enable mysqld systemctl daemon-reload
6、运行以下命令查看/var/log/mysqld.log文件,获取并记录root用户的初始密码。
grep 'temporary password' /var/log/mysqld.log
7、运行以下命令配置MySQL的安全性
mysql_secure_installation 重置root账号密码。 Enter password for user root: #输入上一步获取的root用户初始密码 The 'validate_password' plugin is installed on the server. The subsequent steps will run with the existing configuration of the plugin. Using existing password for root. Estimated strength of the password: 100 Change the password for root ? (Press y|Y for Yes, any other key for No) : Y #是否更改root用户密码,输入Y New password: #输入新密码,长度为8至30个字符,必须同时包含大小写英文字母、数字和特殊符号。特殊符号可以是()` ~!@#$%^&*-+=|{}[]:;‘<>,.?/ Re-enter new password: #再次输入新密码 Estimated strength of the password: 100 Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y 输入Y删除匿名用户账号。 By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL 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? (Press y|Y for Yes, any other key for No) : Y #是否删除匿名用户,输入Y Success. 输入Y禁止root账号远程登录。 Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y #禁止root远程登录,输入Y Success. 输入Y删除test库以及对test库的访问权限。 Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y #是否删除test库和对它的访问权限,输入Y - Dropping test database... Success. 输入Y重新加载授权表。 Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y #是否重新加载授权表,输入Y Success. All done!
四、安装PHP
1、运行以下命令添加epel源
yum install \ https://repo.ius.io/ius-release-el7.rpm \ https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
2、运行以下命令添加Webtatic源
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
3、运行以下命令安装PHP
yum -y install php70w-devel php70w.x86_64 php70w-cli.x86_64 php70w-common.x86_64 php70w-gd.x86_64 php70w-ldap.x86_64 php70w-mbstring.x86_64 php70w-mcrypt.x86_64 php70w-pdo.x86_64 php70w-mysqlnd php70w-fpm php70w-opcache php70w-pecl-redis php70w-pecl-mongodb
4、运行以下命令查看PHP版本。
php -v
五、配置Nginx
1、运行以下命令备份Nginx配置文件
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
2、修改Nginx配置文件,添加Nginx对PHP的支持
vim /etc/nginx/conf.d/default.conf location / { root /usr/share/nginx/html; index index.html index.htm index.php; } #添加下列信息,配置Nginx通过fastcgi方式处理您的PHP请求。 location ~ .php$ { root /usr/share/nginx/html; #将/usr/share/nginx/html替换为您的网站根目录,本教程使用/usr/share/nginx/html作为网站根目录。 fastcgi_pass 127.0.0.1:9000; #Nginx通过本机的9000端口将PHP请求转发给PHP-FPM进行处理。 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; #Nginx调用fastcgi接口处理PHP请求。 }
3、运行以下命令启动Nginx服务
systemctl start nginx
4、运行以下命令设置Nginx服务开机自启动。
systemctl enable nginx
六、配置PHP
1、新建phpinfo.php文件,用于展示PHP信息
vim /usr/share/nginx/html/phpinfo.php <?php phpinfo();?>
2、运行以下命令启动PHP-FPM
systemctl start php-fpm
3、运行以下命令设置PHP-FPM开机自启动
systemctl enable php-fpm
七、测试访问LNMP平台
1、打开浏览器,在地址栏输入http://ip/phpinfo.php
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容