Nginx (pronounced "engine x") is a free, open-source, high-performance HTTP server. Nginx is known for its stability, rich feature set, simple configuration, and low resource consumption. This tutorial shows how you can install Nginx on a CentOS7.2 server with php support (through PHP-FPM ) and mysql (MariaDB) support.
1 Preliminary NoteIn this tutorial, I will use the hostname server1.example.com with the IP address 192.168.1.100 . These settings might differ for you, so you have to replace them where appropriate.
I will use the nano editor in this tutorial to edit configuration files. Nano can be installed like this.
yum -y install nano
I recommend having a firewall installed. If you do not have firewalld installed yet and want to use a firewall, then install it with these commands:
yum -y installfirewalld
start the firewall and enable it to be started at boot time.
systemctl start firewalld.service
systemctl enable firewalld.service
Next, open your SSH port to ensure that you will be able to connect to the server by SSH.
firewall-cmd --permanent --zone=public --add-service=ssh
firewall-cmd --reload
2 Enabling Additional CentOS RepositoriesThe latest Nginx is not available from the official CentOS repositories, so we include the repository of the Nginx project to install it:
nano /etc/yum.repos.d/nginx.repo
[nginx]name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1 3 Installing MySQL (MariaDB)
First, we install MariaDB as MySQL replacement. MariaDB is a free fork of MySQL. Run this command on the shell to install the MariaDB database server:
yum -y install mariadb mariadb-server net-tools
Then we create the system startup links for MariaDB (so that it starts automatically whenever the system boots) and start the MariaDB server:
systemctl enable mariadb.servicesystemctl start mariadb.service
Now check that networking is enabled. Please note that the MraiDB service is named mysql as it is a compatble database server. Run
netstat -tap | grep mysql
It should show something like this:
[ [emailprotected]~]# netstat -tap | grep mysql
tcp 0 0 0.0.0.0:mysql 0.0.0.0:* LISTEN 19842/mysqld
Run:
mysql_secure_installation
to set a password for the user root (otherwise, anybody can access your MySQL database!):
[ [emailprotected]~]# 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 MariaDBSERVERS 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 MariaDBroot user without the proper authorisation.
Set root password? [Y/n] <--ENTERNew password: <--yourrootsqlpassword
Re-enter new password: <--yourrootsqlpassword
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] <--ENTER... Success!
Normally, root should only be allowed to connect from 'localhost'. Thisensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] <--ENTER... 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] <--ENTER- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so farwill take effect immediately.
Reload privilege tables now? [Y/n] <--ENTER... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDBinstallation should now be secure.
Thanks for using MariaDB!
[ [emailprotected]~]#
[[emailprotected]~]#mysql_secure_installation4 Installing Nginx
Nginx is available as a package from nginx.org which we can install like this:
yum -y install nginx
Then we create the system startup links for nginx and start it:
systemctl enable nginx.servicesystemctl start nginx.service
There are chances that you get an error that port 80 is already in use, the error message will be like this:
[[emailprotected] ~]# service nginx startStarting 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]
[[emailprotected] ~]#
This means that another web server (probably Apache) is already running on this server. Stop the Apache service and then start the service for NGINX:
systemctl stop httpd.service
yum remove httpd
systemctl disable httpd.service
Then try to start Nginx again.
systemctl start nginx.service
Open the HTTP and HTTPS ports in the firewall
firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload
The resulting output on the shell will look like this:
[ [emailprotected]~]# firewall-cmd --permanent --zone=public --add-service=http
success
[[emailprotected] ~]# firewall-cmd --permanent --zone=public --add-service=httpssuccess
[[emailprotected] ~]# firewall-cmd --reloadsuccess
[