- How to start, restart & stop Apache & MySQL server on Ubuntu
- Start Apache on Ubuntu Linux
- Stop Apache server on Ubuntu Linux
- Restart Apache server on Ubuntu Linux
- Stop MySQL on Ubuntu Linux
- Start MySQL on Ubuntu Linux
- Restart MySQL on Ubuntu Linux
- Shell restart mysql service in linux code example
- Ubuntu — mysql start and restart issue on ubuntu 16.04 server
- Restart Mysql by PHP
- How To Write a Bash Script To Restart Server Programs
- Bash Script
- Cron Setup
- See More
- Autorestart Apache2 if it isn’t running
- How to restart network manager linux
- Restart network linux
- how to restart network manager in ubuntu
How to start, restart & stop Apache & MySQL server on Ubuntu
The Apache web server is the light and most popular open source web server use worldwide on Linux based operating system. It can be installed on Ubuntu with a single command to handle both dynamic and static content web pages. Most of the WordPress and other popular CMS uses it as a Web server.
Furthermore, when you want to create some online application or website then you also need a database software to store all your content and here the MySQL comes in. It is also an open source database system software that can install on any Linux operating system with ease. If you are new to both then you can look at our tutorial which let you know how to install Apache, MySQL, and PHP on Ubuntu Linux.
The below-given commands will work on all Ubuntu and Debian based Linux operating systems no matter where they are, it could be on your local PC system or a hosting or cloud services like AWS, DigitalOcean, google cloud and more..
Start Apache on Ubuntu Linux
The command to start the Apache web server on Ubuntu is:
sudo /etc/init.d/apache2 start or sudo service apache2 start
Stop Apache server on Ubuntu Linux
Command on the terminal to stop the web server Apache2
sudo /etc/init.d/apache2 stop OR sudo service apache2 stop
Restart Apache server on Ubuntu Linux
Commands to execute on Ubuntu terminal to restart the Apache web server.
sudo /etc/init.d/apache2 restart OR sudo service apache2 restart
If you are using the Ubuntu 15.04 or above then you can also use the below commands to start/restart and stop the Apache web server.
To start Apache systemctl start apache2.service To stop Apache systemctl stop apache2.service To Restart Apache systemctl restart apache2.service
For start/Stop and restart the MySQL server on Ubuntu use the below mentioned commands:
Stop MySQL on Ubuntu Linux
Terminal commands to stop MySQL
sudo stop mysql or sudo service mysql stop or sudo /etc/inid.d/mysql stop
Start MySQL on Ubuntu Linux
Use any of one given below to start the MySQL, if it in a stop state.
sudo start mysql or sudo service mysql start or sudo /etc/init.d/mysql start
Restart MySQL on Ubuntu Linux
To restart the MySQL Ubuntu Linux server use the commands in terminal.
sudo restart mysql or sudo service mysql restart or sudo /etc/init.d/mysql restart
Shell restart mysql service in linux code example
On *nix platforms you can achieve this by using visudo command and appending However it is not recomended to give web server user right to do such thing and you perhaps should question that ‘solution’ with restarting mysql daemon. If this does not work for you take a look at this question: restart mysql server on windows 7 Another approach might be through SSH using the SSH PHP extension, which needs to be installed first.
Ubuntu — mysql start and restart issue on ubuntu 16.04 server
This problem arrives when your /etc/mysql/mysql.conf.d/mysqld.cnf file is wrongly set. If you recently made changes to this file please revert to the previous one or copy the default one here
# # The MySQL database server configuration file. # # You can copy this to one of: # - "/etc/mysql/my.cnf" to set global options, # - "~/.my.cnf" to set user-specific options. # # One can use all long options that the program supports. # Run program with --help to get a list of available options and with # --print-defaults to see which it would actually understand and use. # # For explanations see # http://dev.mysql.com/doc/mysql/en/server-system-variables.html # This will be passed to all mysql clients # It has been reported that passwords should be enclosed with ticks/quotes # escpecially if they contain "#" chars. # Remember to edit /etc/mysql/debian.cnf when changing the socket location. # Here is entries for some specific programs # The following values assume you have at least 32M ram [mysqld_safe] socket = /var/run/mysqld/mysqld.sock nice = 0 [mysqld] # # * Basic Settings # user = mysql pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock port = 3306 basedir = /usr datadir = /var/lib/mysql tmpdir = /tmp lc-messages-dir = /usr/share/mysql skip-external-locking # # Instead of skip-networking the default is now to listen only on # localhost which is more compatible and is not less secure. bind-address = 0.0.0.0 # # * Fine Tuning # key_buffer_size = 16M max_allowed_packet = 16M thread_stack = 192K thread_cache_size = 8 # This replaces the startup script and checks MyISAM tables if needed # the first time they are touched myisam-recover-options = BACKUP #max_connections = 100 #table_open_cache = 64 #thread_concurrency = 10 # # * Query Cache Configuration # query_cache_limit = 1M query_cache_size = 16M # # * Logging and Replication # # Both location gets rotated by the cronjob. # Be aware that this log type is a performance killer. # As of 5.1 you can enable the log at runtime! #general_log_file = /var/log/mysql/mysql.log #general_log = 1 # # Error log - should be very few entries. # log_error = /var/log/mysql/error.log # # Here you can see queries with especially long duration #slow_query_log = 1 #slow_query_log_file = /var/log/mysql/mysql-slow.log #long_query_time = 2 #log-queries-not-using-indexes # # The following can be used as easy to replay backup logs or for replication. # note: if you are setting up a replication slave, see README.Debian about # other settings you may need to change. #server-id = 1 #log_bin = /var/log/mysql/mysql-bin.log #expire_logs_days = 10 max_binlog_size = 100M #binlog_do_db = include_database_name #binlog_ignore_db = include_database_name # # * InnoDB # # InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/. # Read the manual for more InnoDB related options. There are many! # # * Security Features # # Read the manual, too, if you want chroot! # chroot = /var/lib/mysql/ # # For generating SSL certificates I recommend the OpenSSL GUI "tinyca". # # ssl-ca=/etc/mysql/cacert.pem # ssl-cert=/etc/mysql/server-cert.pem # ssl-key=/etc/mysql/server-key.pem
sudo apt-get remove mysql sudo apt-get purge mysql
Then issue a whereis mysql command in terminal and remove all of the locations that command lists using rm -rf command. For example rm -rf /etc/mysql .
Then reinstall the version of mysql you desire. All should work as expected.
Just check if you db has been given the rights to write the logs give it the logs writing rights and this will get resolved .
Restart MySql Server From The Browser Using, You can grant the PHP user the sudo permission to run the desired command and nothing else (how to do that is covered elsewhere) and invoke the command directly from PHP: sudo systemctl restart mysql. Keep the rst.sh file, get it owned by root, writeable by noone else but root and set SUID bit on that file. This way you can invoke the script
Restart Mysql by PHP
Yes, it is possible. How depends on which OS you’re running. One approach is the PHP exec function to execute a external program.
The command to be executed depend on the OS, as I said. Here are the command (If I’m correct, please tell me if I’m not):
Debian / Ubuntu:
/usr/local/mysql/support-files/mysql.server restart
net stop MySQL net start MySQL
On Windows MySQL might vary. If this does not work for you take a look at this question: restart mysql server on windows 7
Another approach might be through SSH using the SSH PHP extension, which needs to be installed first. Take a look at: http://www.php.net/manual/en/ssh2.installation.php
Then connect to the SSH server and execute the commands:
$con = ssh2_connect("example.com", 22); // Connect to SSH server $exec = ssh2_exec($con, "/etc/init.d/mysql restart"); // Execute command
Hope this is helped you out 😉
Exec will do the job only if system user that runs PHP (usually www-data) has rights to restart mysql daemon.
On *nix platforms you can achieve this by using visudo command and appending www-data ALL = NOPASSWD: /etc/init.d/mysql (platform-specific path to mysql)
However it is not recomended to give web server user right to do such thing and you perhaps should question that ‘solution’ with restarting mysql daemon.
Bash — Script for CentOS to restart MySQL service, Finaly, it’s better to check the status with service mysql status. You could try something like instead : [ [ $ (service mysql status) =~ running ]] || service mysql restart. @user33398. You could try to use this test in a loop or with watch command for example. Or you could use more smart tools like cron or jenkins or …
How To Write a Bash Script To Restart Server Programs
To ensure that the most imperative programs remain online as much as possible (even after a server crash or reboot), one can create a short bash script to check if the program is running, and if it is not, to launch it. By using cron to schedule the script to be executed on a regular basis, we can make sure that program relaunches whenever it goes down.
Bash Script
The first step in this process is to create the script itself. There are a variety of programs such as upstart, supervisor, and monit, that have the capability to start and monitor applications on a virtual private server in a very nuanced way— this bash script will simply provide an on switch.
Below is a sample script that starts apache if it finds it off.
#!/bin/sh ps auxw | grep apache2 | grep -v grep > /dev/null if [ $? != 0 ] then /etc/init.d/apache2 start > /dev/null fi
Once you have saved the script, you must give it executable permissions in order to be able to run it:
Apache can be replaced with any required application. Should you want to set up the script for a variety of applications, you can create a new script for each one, placing it on its own line in the cron file.
Cron Setup
With the script in hand, we need to set up the schedule on which it will run. The cron utility allows us to schedule at what intervals the script should execute. Start by opening up the cron file:
Cron has a detailed explanation of how the timing system works at the beginning.
Once you know how often you want the script to run, you can write in the corresponding line.
The most often that the script can run in cron is every minute. Should you want to set up such a small increment, you can use this template:
Every five minutes would be set up like this:
See More
Setting up this simple script will keep the program starting up after it shuts down for any reason. This is convenient as it will ensure that the longest time that a program will be down is for the interval of time that you specified in the cron configuration.
Should you need a program that is even slightly more subtle, you can set up the details of your startup with one of the several server monitoring programs (Supervisor, Upstart, or Monit).
This one line cron job is how I am doing it :
Autorestart Apache2 if it isn’t running
It hasn’t failed yet, even if apache2 crashes . The/dev/null bit means I don’t get emailed every minute with a success message. It “just works”.
Could this script be modified to make java execute a .jar file whenever a Minecraft server crashes?
How to restart sql server in linux Code Example, follow. grepper; search snippets; faq; usage docs ; install grepper; log in; signup
How to restart network manager linux
Restart network linux
systemctl restart networking
how to restart network manager in ubuntu
sudo service network-manager restart
Mysql start and restart issue on ubuntu 16.04 server, 1)First remove already installed mysql-server using— sudo apt-get remove —purge mysql-server mysql-client mysql-common 2)Then clean all files sudo apt-get autoremove 3)Then install mysql-server — sudo apt-get remove -f mysql-server 4)Start mysql server— sudo systemctl start mysql 5)Check status of mysql …