您的位置:首页 > 运维架构 > Nginx

Linux学习笔记-CentOS7安装nginx、PHP7、MySQL

2018-01-31 22:57 741 查看

安装nginx

使用yum安装nginx

根据官方文档:http://nginx.org/en/linux_packages.html#stable

先添加nginx的yum容器,创建文件
vi /etc/yum.repos.d/nginx.repo
,将下面的内容复制进去:

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/OS/OSRELEASE/$basearch/
gpgcheck=0
enabled=1


Replace “OS” with “rhel” or “centos”, depending on the distribution used, and “OSRELEASE” with “6” or “7”, for 6.x or 7.x versions, respectively.

根据文档提示,因为我是centos7系统,把OS替换成centos,OSRELEASE替换成7,即

baseurl=http://nginx.org/packages/centos/7/$basearch/


使用命令
yum -y install nginx
即可安装

使用命令
rpm -ql nginx
可以发现,安装完后的配置文件在 /etc/nginx 目录下

开机自启动 nginx

这里使用的是官网提供的nginx初始化脚本,地址:

https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/

创建文件
vi /etc/init.d/nginx


文件内容:

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15
# description:  NGINX is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /etc/nginx/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:     /var/run/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/etc/nginx/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -n "$user" ]; then
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
fi
}

start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}

stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}

restart() {
configtest || return $?
stop
sleep 1
start
}

reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}

force_reload() {
restart
}

configtest() {
$nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
status $prog
}

rh_status_q(
4000
) {
rh_status >/dev/null 2>&1
}

case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac


chmod a+x /etc/init.d/nginx #添加执行权限
chkconfig --add nginx   #添加nginx为系统服务
chkconfig nginx on      #开机自启动


关闭系统自带防火墙

service firewalld stop  #停止firewall
chkconfig firewalld off #禁止firewall开机启动
firewall-cmd --state #查看默认防火墙状态(关闭后显示notrunning,开启后显示running)


关闭SELinux

如果不关闭SELinux,nginx可能返回403错误

vi /etc/selinux/config


SELINUX=enforcing 改成 SELINUX=disabled,重启虚拟机

现在可以在浏览器中输入虚拟机ip访问nginx欢迎页了。

安装PHP7

使用源码编译安装php7

先在windows上去官网获得下载地址,然后在linux上用wget命令下载源码压缩包

这里选择的是距离中国较近的日本服务器,当前版本地址为:http://jp2.php.net/distributions/php-7.2.1.tar.bz2

先安装依赖软件

yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel gcc


cd /usr/local/src   #把所有软件安装包都放在src下便于管理
wget http://jp2.php.net/distributions/php-7.2.1.tar.bz2 tar -jxvf php-7.2.1.tar.bz2 #解压
cd  php-7.2.1


生成Makefile

./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-curl \
--with-freetype-dir \
--with-gd \
--with-gettext \
--with-iconv-dir \
--with-kerberos \
--with-libdir=lib64 \
--with-libxml-dir \
--with-mysqli \
--with-openssl \
--with-pcre-regex \
--with-pdo-mysql \
--with-pdo-sqlite \
--with-pear \
--with-png-dir \
--with-xmlrpc \
--with-xsl \
--with-zlib \
--enable-fpm \
--enable-bcmath \
--enable-libxml \
--enable-inline-optimization \
--enable-mbregex \
--enable-mbstring \
--enable-opcache \
--enable-pcntl \
--enable-shmop \
--enable-soap \
--enable-sockets \
--enable-sysvsem \
--enable-xml \
--enable-zip


编译、安装

make && make install


复制配置文件

cp php.ini-production /usr/local/php/etc/php.ini
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf


chkconfig --add php-fpm
chkconfig php-fpm on
chmod a+x /etc/init.d/php-fpm   #添加执行权限


编辑php.ini

vi /usr/local/php/etc/php.ini


找到:;date.timezone =

修改为:date.timezone = PRC #设置时区

找到:short_open_tag = Off

修改为:short_open_tag = On #支持php短标签

配置nginx支持PHP

修改php-fpm配置

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


找到:

user = nobody
group = nobody


修改为:

user = nginx
group = nginx


修改nginx配置

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


找到第一个location中的这一行

index  index.html index.htm;


修改为:

index  index.php index.html index.htm; #添加index.php


把FastCGI server这行下面的location的注释去掉,并修改成下面这样子

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root            /usr/share/nginx/html;  #网站根目录
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
include        fastcgi_params;
}


service nginx restart   #重启nginx
service php-fpm start   #开启php-fpm


在网站根目录新建index.php文件

vim /usr/share/nginx/html/index.php


输入内容:

<?php
phpinfo();


在浏览器中输入虚拟机ip,已经可以看到phpinfo的信息了

在windows上修改hosts文件,添加一行

192.168.6.114   www.test1.com   #配置虚拟机ip对应域名


现在就可以在windows上用www.test1.com访问虚拟机配置的服务器了

安装MySQL

使用yum安装MySQL

wget https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm    #在官网获得下载地址
rpm -ivh mysql57-community-release-el7-11.noarch.rpm


安装这个包后,会获得两个mysql的yum repo源:

/etc/yum.repos.d/mysql-community.repo,/etc/yum.repos.d/mysql-community-source.repo

yum -y install mysql-server mysql-devel


配置mysql

service mysqld start
grep "temporary password" /var/log/mysqld.log   #查找到mysql的初始密码
mysql -u root -p    #登录mysql
SET PASSWORD = PASSWORD('aa123');   #修改当前帐号密码


这里可能会报密码不符合策略的错误,需要修改设置

set global validate_password_policy=0;  #修改密码时验证标准为最低级别
set global validate_password_length=4;  #验证密码长度为4
SET PASSWORD = PASSWORD('aa123');       #再次修改密码


OK!改密成功了。为mysql新建一个root账号用来远程登录mysql

#新建一个root帐号支持任何ip登录,密码为aa123,这样就可以用root远程登录mysql了
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'aa123' WITH GRANT OPTION;
flush privileges;   #刷新MySQL的系统权限相关表­


注意:5.7版本的MySQL很吃内存,虚拟机内存最好大于2G,否则要么安装不了,要么启动不了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  LNMP